Skip to content

Commit f023e6c

Browse files
committed
flaky test fix
1 parent 712eb66 commit f023e6c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

test/AWS.Messaging.UnitTests/SQSMessagePollerTests.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ public async Task SQSMessagePoller_PollingControlStopped_DoesNotPollSQS()
7878
public async Task SQSMessagePoller_PollingControlRestarted_PollsSQS()
7979
{
8080
var client = new Mock<IAmazonSQS>();
81+
var messageReceived = new TaskCompletionSource<bool>();
8182
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+
8386
var pollingControlToken = new PollingControlToken
8487
{
8588
PollingWaitTime = TimeSpan.FromMilliseconds(25)
@@ -90,12 +93,24 @@ public async Task SQSMessagePoller_PollingControlRestarted_PollsSQS()
9093
var pump = BuildMessagePumpService(client, options => { options.WaitTimeSeconds = 1; }, pollingControlToken: pollingControlToken);
9194
var task = pump.StartAsync(source.Token);
9295

96+
// Verify no messages are received while polling is stopped
9397
client.Verify(x => x.ReceiveMessageAsync(It.IsAny<ReceiveMessageRequest>(), It.IsAny<CancellationToken>()), Times.Never);
9498

99+
// Start polling and wait for a message to be received
95100
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+
}
96112

97-
SpinWait.SpinUntil(() => false, pollingControlToken.PollingWaitTime * 5);
98-
113+
// Verify that messages were received
99114
client.Verify(x => x.ReceiveMessageAsync(It.IsAny<ReceiveMessageRequest>(), It.IsAny<CancellationToken>()), Times.AtLeastOnce());
100115

101116
source.Cancel();

0 commit comments

Comments
 (0)