@@ -49,6 +49,34 @@ public ServerMonitorTests(ITestOutputHelper output) : base(output)
49
49
{
50
50
}
51
51
52
+ [ Fact ]
53
+ public void CancelCurrentCheck_should_dispose_connection ( )
54
+ {
55
+ using var subject = CreateSubject ( out _ , out _ , out _ ) ;
56
+
57
+ subject . CancelCurrentCheck ( ) ;
58
+
59
+ subject . _connection ( ) . Should ( ) . BeNull ( ) ;
60
+
61
+ var logMessages = Logs . Where ( l => l . Category . Contains ( nameof ( IServerMonitor ) ) ) . ToArray ( ) ;
62
+ logMessages . Length . Should ( ) . Be ( 1 ) ;
63
+ logMessages [ 0 ] . Message . Contains ( "Heartbeat cancellation check requested" ) . Should ( ) . BeTrue ( ) ;
64
+ }
65
+
66
+ [ Fact ]
67
+ public void CancelCurrentCheck_should_do_nothing_if_disposed ( )
68
+ {
69
+ using var subject = CreateSubject ( out _ , out _ , out _ ) ;
70
+
71
+ subject . Dispose ( ) ;
72
+ subject . CancelCurrentCheck ( ) ;
73
+
74
+ var logMessages = Logs . Where ( l => l . Category . Contains ( nameof ( IServerMonitor ) ) ) . ToArray ( ) ;
75
+ logMessages . Length . Should ( ) . Be ( 2 ) ;
76
+ logMessages [ 0 ] . Message . Contains ( "Disposing" ) . Should ( ) . BeTrue ( ) ;
77
+ logMessages [ 1 ] . Message . Contains ( "Disposed" ) . Should ( ) . BeTrue ( ) ;
78
+ }
79
+
52
80
[ Fact ]
53
81
public void Constructor_should_throw_when_serverId_is_null ( )
54
82
{
@@ -100,7 +128,7 @@ public void Constructor_should_throw_when_serverMonitorSettings_is_null()
100
128
[ Fact ]
101
129
public void Description_should_return_default_when_uninitialized ( )
102
130
{
103
- var subject = CreateSubject ( out _ , out _ , out _ ) ;
131
+ using var subject = CreateSubject ( out _ , out _ , out _ ) ;
104
132
105
133
var description = subject . Description ;
106
134
@@ -112,7 +140,7 @@ public void Description_should_return_default_when_uninitialized()
112
140
[ Fact ]
113
141
public void Description_should_return_default_when_disposed ( )
114
142
{
115
- var subject = CreateSubject ( out _ , out _ , out _ ) ;
143
+ using var subject = CreateSubject ( out _ , out _ , out _ ) ;
116
144
117
145
subject . Dispose ( ) ;
118
146
@@ -129,7 +157,7 @@ public void DescriptionChanged_should_be_raised_during_initial_handshake()
129
157
var capturedEvents = new EventCapturer ( ) ;
130
158
131
159
var changes = new List < ServerDescriptionChangedEventArgs > ( ) ;
132
- var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
160
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
133
161
subject . DescriptionChanged += ( o , e ) => changes . Add ( e ) ;
134
162
135
163
SetupHeartbeatConnection ( mockConnection ) ;
@@ -157,7 +185,7 @@ public void Description_should_be_connected_after_successful_heartbeat()
157
185
{
158
186
var capturedEvents = new EventCapturer ( ) ;
159
187
160
- var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
188
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
161
189
SetupHeartbeatConnection ( mockConnection ) ;
162
190
subject . Initialize ( ) ;
163
191
SpinWait . SpinUntil ( ( ) => subject . Description . State == ServerState . Connected , TimeSpan . FromSeconds ( 5 ) ) . Should ( ) . BeTrue ( ) ;
@@ -175,7 +203,7 @@ public void Dispose_should_clear_all_resources_only_once()
175
203
{
176
204
var capturedEvents = new EventCapturer ( ) ;
177
205
178
- var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , captureConnectionEvents : true ) ;
206
+ using var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , captureConnectionEvents : true ) ;
179
207
180
208
SetupHeartbeatConnection ( mockConnection ) ;
181
209
subject . Initialize ( ) ;
@@ -198,7 +226,7 @@ public void Heartbeat_should_make_immediate_next_attempt_for_streaming_protocol(
198
226
. Capture < ServerHeartbeatSucceededEvent > ( )
199
227
. Capture < ServerHeartbeatFailedEvent > ( )
200
228
. Capture < ServerDescriptionChangedEvent > ( ) ;
201
- var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTimeTripMonitor , capturedEvents ) ;
229
+ using var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTimeTripMonitor , capturedEvents ) ;
202
230
203
231
subject . DescriptionChanged +=
204
232
( o , e ) =>
@@ -276,7 +304,7 @@ void AssertHeartbeatAttempt()
276
304
[ InlineData ( true , true , "hello" ) ]
277
305
public void InitializeHelloProtocol_should_use_streaming_protocol_when_available ( bool isStreamable , bool helloOk , string expectedCommand )
278
306
{
279
- var subject = CreateSubject ( out var mockConnection , out _ , out _ ) ;
307
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ ) ;
280
308
SetupHeartbeatConnection ( mockConnection , isStreamable , autoFillStreamingResponses : true ) ;
281
309
282
310
mockConnection . WasReadTimeoutChanged . Should ( ) . Be ( null ) ;
@@ -304,7 +332,7 @@ public void RoundTripTimeMonitor_should_be_started_only_once_if_using_streaming_
304
332
{
305
333
var capturedEvents = new EventCapturer ( ) . Capture < ServerHeartbeatSucceededEvent > ( ) ;
306
334
var serverMonitorSettings = new ServerMonitorSettings ( TimeSpan . FromSeconds ( 5 ) , TimeSpan . FromMilliseconds ( 10 ) ) ;
307
- var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
335
+ using var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
308
336
309
337
SetupHeartbeatConnection ( mockConnection , isStreamable : true , autoFillStreamingResponses : false ) ;
310
338
mockConnection . EnqueueCommandResponseMessage ( CreateHeartbeatCommandResponseMessage ( ) , null ) ;
@@ -328,7 +356,7 @@ public void RoundTripTimeMonitor_should_not_be_started_if_using_polling_protocol
328
356
serverMonitoringMode : ServerMonitoringMode . Poll ) ;
329
357
330
358
var capturedEvents = new EventCapturer ( ) . Capture < ServerHeartbeatSucceededEvent > ( ) ;
331
- var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
359
+ using var subject = CreateSubject ( out var mockConnection , out _ , out var mockRoundTripTimeMonitor , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
332
360
333
361
SetupHeartbeatConnection ( mockConnection , isStreamable : true , autoFillStreamingResponses : false ) ;
334
362
mockConnection . EnqueueCommandResponseMessage ( CreateHeartbeatCommandResponseMessage ( ) ) ;
@@ -346,7 +374,7 @@ public void RoundTripTimeMonitor_should_not_be_started_if_using_polling_protocol
346
374
public void RequestHeartbeat_should_force_another_heartbeat ( )
347
375
{
348
376
var capturedEvents = new EventCapturer ( ) ;
349
- var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
377
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents ) ;
350
378
351
379
SetupHeartbeatConnection ( mockConnection ) ;
352
380
subject . Initialize ( ) ;
@@ -364,6 +392,26 @@ public void RequestHeartbeat_should_force_another_heartbeat()
364
392
capturedEvents . Any ( ) . Should ( ) . BeFalse ( ) ;
365
393
}
366
394
395
+ [ Fact ]
396
+ public void RequestHeartbeat_should_throw_if_disposed ( )
397
+ {
398
+ var subject = CreateSubject ( out var _ , out _ , out _ ) ;
399
+
400
+ subject . Dispose ( ) ;
401
+
402
+ var exception = Record . Exception ( ( ) => subject . RequestHeartbeat ( ) ) ;
403
+ exception . Should ( ) . BeOfType < ObjectDisposedException > ( ) ;
404
+ }
405
+
406
+ [ Fact ]
407
+ public void RequestHeartbeat_should_throw_if_not_open ( )
408
+ {
409
+ using var subject = CreateSubject ( out var _ , out _ , out _ ) ;
410
+
411
+ var exception = Record . Exception ( ( ) => subject . RequestHeartbeat ( ) ) ;
412
+ exception . Should ( ) . BeOfType < InvalidOperationException > ( ) ;
413
+ }
414
+
367
415
[ Fact ]
368
416
public void ServerHeartBeatEvents_should_not_be_awaited_if_using_polling_protocol ( )
369
417
{
@@ -372,7 +420,7 @@ public void ServerHeartBeatEvents_should_not_be_awaited_if_using_polling_protoco
372
420
. Capture < ServerHeartbeatSucceededEvent > ( ) ;
373
421
374
422
var serverMonitorSettings = new ServerMonitorSettings ( TimeSpan . FromSeconds ( 5 ) , TimeSpan . FromMilliseconds ( 10 ) , serverMonitoringMode : ServerMonitoringMode . Poll ) ;
375
- var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
423
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents , serverMonitorSettings : serverMonitorSettings ) ;
376
424
377
425
SetupHeartbeatConnection ( mockConnection , isStreamable : true , autoFillStreamingResponses : false ) ;
378
426
mockConnection . EnqueueCommandResponseMessage ( CreateHeartbeatCommandResponseMessage ( ) ) ;
@@ -473,7 +521,7 @@ public void Should_use_polling_protocol_if_running_in_FaaS_platform(string envir
473
521
. Capture < ServerHeartbeatSucceededEvent > ( ) ;
474
522
475
523
var serverMonitorSettings = new ServerMonitorSettings ( TimeSpan . FromSeconds ( 5 ) , TimeSpan . FromMilliseconds ( 10 ) ) ;
476
- var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents , serverMonitorSettings : serverMonitorSettings , environmentVariableProviderMock : environmentVariableProviderMock ) ;
524
+ using var subject = CreateSubject ( out var mockConnection , out _ , out _ , capturedEvents , serverMonitorSettings : serverMonitorSettings , environmentVariableProviderMock : environmentVariableProviderMock ) ;
477
525
478
526
SetupHeartbeatConnection ( mockConnection , isStreamable : true , autoFillStreamingResponses : false ) ;
479
527
mockConnection . EnqueueCommandResponseMessage ( CreateHeartbeatCommandResponseMessage ( ) ) ;
0 commit comments