@@ -81,7 +81,7 @@ public async Task ClientStateAsync()
81
81
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ConnectionWriterTask . Status ) ;
82
82
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ConnectionReaderTask . Status ) ;
83
83
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ReceivedPacketsHandlerTask . Status ) ;
84
- Assert . True ( client . Connection . ConnectionMonitorThread . IsAlive ) ;
84
+ Assert . False ( client . Connection . ConnectionMonitorThread . IsCompleted ) ;
85
85
86
86
// Queues
87
87
Assert . NotNull ( client . Connection . SendQueue ) ;
@@ -118,7 +118,7 @@ public async Task ClientStateAsync()
118
118
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ConnectionWriterTask . Status ) ;
119
119
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ConnectionReaderTask . Status ) ;
120
120
Assert . Equal ( TaskStatus . WaitingForActivation , client . Connection . ReceivedPacketsHandlerTask . Status ) ;
121
- Assert . True ( client . Connection . ConnectionMonitorThread . IsAlive ) ;
121
+ Assert . False ( client . Connection . ConnectionMonitorThread . IsCompleted ) ;
122
122
123
123
// Queues
124
124
Assert . NotNull ( client . Connection . SendQueue ) ;
@@ -136,7 +136,10 @@ public async Task ClientStateAsync()
136
136
Assert . Null ( client . Connection . ConnectionWriterTask ) ;
137
137
Assert . Null ( client . Connection . ConnectionReaderTask ) ;
138
138
Assert . Null ( client . Connection . ReceivedPacketsHandlerTask ) ;
139
- Assert . True ( client . Connection . ConnectionMonitorThread . IsAlive ) ;
139
+
140
+ // The task should be completed and null at this point since with every new call to ConnectAsync
141
+ // a new task is started and during DisconnectAsync this task should be stopped and removed
142
+ Assert . Null ( client . Connection . ConnectionMonitorThread ) ;
140
143
141
144
// Queues
142
145
Assert . NotNull ( client . Connection . SendQueue ) ;
@@ -145,4 +148,23 @@ public async Task ClientStateAsync()
145
148
// State
146
149
Assert . Equal ( ConnectState . Disconnected , client . Connection . State ) ;
147
150
}
151
+
152
+ [ Fact ]
153
+ public async Task AfterDisconnect_ConnectionMonitorThread_ShouldBe_StoppedAsync ( )
154
+ {
155
+ using var client = new HiveMQClient ( ) ;
156
+ await client . ConnectAsync ( ) . ConfigureAwait ( false ) ;
157
+
158
+ // Hold the reference to the task since it's removed in the client
159
+ // after DisconnectAsync
160
+ var monitorThread = client . Connection . ConnectionMonitorThread ;
161
+
162
+ Assert . True ( monitorThread is not null && ! monitorThread . IsCompleted ) ;
163
+
164
+ await client . DisconnectAsync ( ) . ConfigureAwait ( false ) ;
165
+
166
+ // Task should be completed at this point. During DisconnectAsync the cancellation token
167
+ // should be cancelled and the task should stop
168
+ Assert . True ( monitorThread is not null && monitorThread . IsCompleted ) ;
169
+ }
148
170
}
0 commit comments