Skip to content

Commit b0d1caf

Browse files
committed
Disconnect() needs to cause messageid cleanUp()
Resolves eclipse-paho#169 Signed-off-by: Daniel Price <daniel.price@gmail.com>
1 parent cd4337d commit b0d1caf

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

client.go

+1
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ func (c *client) disconnect() {
470470
c.closeStop()
471471
c.closeConn()
472472
c.workers.Wait()
473+
c.messageIds.cleanUp()
473474
close(c.stopRouter)
474475
DEBUG.Println(CLI, "disconnected")
475476
c.persist.Close()

fvt_client_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,38 @@ func Test_cleanUpMids(t *testing.T) {
10401040

10411041
c.Disconnect(250)
10421042
}
1043+
1044+
// Test that cleanup happens properly on explicit Disconnect()
1045+
func Test_cleanUpMids_2(t *testing.T) {
1046+
ops := NewClientOptions()
1047+
ops.AddBroker(FVTTCP)
1048+
ops.SetClientID("auto_reconnect")
1049+
ops.SetCleanSession(true)
1050+
ops.SetAutoReconnect(true)
1051+
ops.SetKeepAlive(10 * time.Second)
1052+
1053+
c := NewClient(ops)
1054+
1055+
if token := c.Connect(); token.Wait() && token.Error() != nil {
1056+
t.Fatalf("Error on Client.Connect(): %v", token.Error())
1057+
}
1058+
1059+
token := c.Publish("/test/cleanUP", 2, false, "cleanup test 2")
1060+
if len(c.(*client).messageIds.index) == 0 {
1061+
t.Fatalf("Should be a token in the messageIDs, none found")
1062+
}
1063+
fmt.Println("Disconnecting", len(c.(*client).messageIds.index))
1064+
c.Disconnect(0)
1065+
1066+
fmt.Println("Wait on Token")
1067+
// We should be able to wait on this token without any issue
1068+
token.Wait()
1069+
1070+
if len(c.(*client).messageIds.index) > 0 {
1071+
t.Fatalf("Should have cleaned up messageIDs, have %d left", len(c.(*client).messageIds.index))
1072+
}
1073+
if token.Error() == nil {
1074+
t.Fatal("token should have received an error on connection loss")
1075+
}
1076+
fmt.Println(token.Error())
1077+
}

messageids.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (mids *messageIds) cleanUp() {
5252
}
5353
mids.index = make(map[uint16]tokenCompletor)
5454
mids.Unlock()
55+
DEBUG.Println(MID, "cleaned up")
5556
}
5657

5758
func (mids *messageIds) freeID(id uint16) {

0 commit comments

Comments
 (0)