@@ -78,8 +78,11 @@ public async Task SQSMessagePoller_PollingControlStopped_DoesNotPollSQS()
78
78
public async Task SQSMessagePoller_PollingControlRestarted_PollsSQS ( )
79
79
{
80
80
var client = new Mock < IAmazonSQS > ( ) ;
81
+ var messageReceived = new TaskCompletionSource < bool > ( ) ;
81
82
client . Setup ( x => x . ReceiveMessageAsync ( It . IsAny < ReceiveMessageRequest > ( ) , It . IsAny < CancellationToken > ( ) ) )
82
- . ReturnsAsync ( new ReceiveMessageResponse ( ) , TimeSpan . FromMilliseconds ( 50 ) ) ;
83
+ . ReturnsAsync ( new ReceiveMessageResponse ( ) , TimeSpan . FromMilliseconds ( 50 ) )
84
+ . Callback ( ( ) => messageReceived . TrySetResult ( true ) ) ;
85
+
83
86
var pollingControlToken = new PollingControlToken
84
87
{
85
88
PollingWaitTime = TimeSpan . FromMilliseconds ( 25 )
@@ -90,12 +93,24 @@ public async Task SQSMessagePoller_PollingControlRestarted_PollsSQS()
90
93
var pump = BuildMessagePumpService ( client , options => { options . WaitTimeSeconds = 1 ; } , pollingControlToken : pollingControlToken ) ;
91
94
var task = pump . StartAsync ( source . Token ) ;
92
95
96
+ // Verify no messages are received while polling is stopped
93
97
client . Verify ( x => x . ReceiveMessageAsync ( It . IsAny < ReceiveMessageRequest > ( ) , It . IsAny < CancellationToken > ( ) ) , Times . Never ) ;
94
98
99
+ // Start polling and wait for a message to be received
95
100
pollingControlToken . StartPolling ( ) ;
101
+
102
+ // Wait for a message to be received with a timeout
103
+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 5 ) ) ;
104
+ try
105
+ {
106
+ await messageReceived . Task . WaitAsync ( cts . Token ) ;
107
+ }
108
+ catch ( OperationCanceledException )
109
+ {
110
+ Assert . Fail ( "Timed out waiting for message to be received after polling was restarted" ) ;
111
+ }
96
112
97
- SpinWait . SpinUntil ( ( ) => false , pollingControlToken . PollingWaitTime * 5 ) ;
98
-
113
+ // Verify that messages were received
99
114
client . Verify ( x => x . ReceiveMessageAsync ( It . IsAny < ReceiveMessageRequest > ( ) , It . IsAny < CancellationToken > ( ) ) , Times . AtLeastOnce ( ) ) ;
100
115
101
116
source . Cancel ( ) ;
0 commit comments