Skip to content

Commit

Permalink
Merge pull request #19 from seqsense/add-con-lost-cb
Browse files Browse the repository at this point in the history
Add connection lost handler
  • Loading branch information
at-wat authored Nov 8, 2018
2 parents ed744be + 5cb1483 commit c9969f3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "sample"
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func connectionHandler(c *DeviceClient) {
c.reconnectPeriod = c.opt.MaximumReconnectTime
}
log.Printf("Trying to reconnect (%d ms)\n", c.reconnectPeriod/time.Millisecond)
if c.opt.OnConnectionLost != nil {
c.opt.OnConnectionLost(c.opt)
}

go func() {
time.Sleep(c.reconnectPeriod)
Expand Down
43 changes: 43 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,49 @@ func TestDeviceClient(t *testing.T) {
}
}

func TestConnectionLostHandler(t *testing.T) {
newClient = func(opt *mqtt.ClientOptions) mqtt.Client {
return &MockClient{}
}
connectionLostHandled := false
o := &Options{
BaseReconnectTime: time.Millisecond * 10,
MaximumReconnectTime: time.Millisecond * 50,
MinimumConnectionTime: time.Millisecond * 50,
Keepalive: time.Second * 2,
Url: "mock://",
OfflineQueueing: true,
OfflineQueueMaxSize: 100,
OfflineQueueDropBehavior: "oldest",
AutoResubscribe: true,
OnConnectionLost: func(opt *Options) {
connectionLostHandled = true
},
}
cli := New(o)
cli.Connect()

// Establish connection
cli.mqttOpt.OnConnect(cli.cli)
time.Sleep(time.Millisecond * 100)

if connectionLostHandled != false {
t.Fatal("ConnectionLostHandler is called before connection lost")
}

// Disconnect
cli.mqttOpt.OnConnectionLost(cli.cli, errors.New("Disconnect test"))
time.Sleep(time.Millisecond * 100)

// Establish connection
cli.mqttOpt.OnConnect(cli.cli)
time.Sleep(time.Millisecond * 100)

if connectionLostHandled != true {
t.Fatal("ConnectionLostHandler was not called on connection lost")
}
}

type MockMessage struct {
topic string
payload []byte
Expand Down
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type TopicPayload struct {
Payload string
}

type ConnectionLostHandler func(*Options)

type Options struct {
KeyPath string
CertPath string
Expand All @@ -44,4 +46,8 @@ type Options struct {
OfflineQueueMaxSize int
OfflineQueueDropBehavior string
AutoResubscribe bool

// OnConnectionLost is called if the MQTT connection is lost.
// Pointer to the Options passed as the argument can be modified for the next reconnect.
OnConnectionLost ConnectionLostHandler
}
3 changes: 3 additions & 0 deletions sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func main() {
OfflineQueueMaxSize: 100,
OfflineQueueDropBehavior: "oldest",
AutoResubscribe: true,
OnConnectionLost: func(opt *awsiot.Options) {
fmt.Printf("Connection lost handler function called\n")
},
}
cli := awsiot.New(o)
cli.Connect()
Expand Down

0 comments on commit c9969f3

Please sign in to comment.