Skip to content

Commit 4c1adf4

Browse files
authored
[Event Hubs] Flaky Test Fix (Azure#29740)
The focus of these changes is to fix a test believed to be flaky due to timing issues around callback execution and ignoring of a cancellation token.
1 parent 2ed2046 commit 4c1adf4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

sdk/eventhub/Azure.Messaging.EventHubs/tests/Primitives/EventProcessorTests.StartStop.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,18 @@ public async Task StopProcessingStopsProcessing(bool async)
637637

638638
mockConsumer
639639
.Setup(consumer => consumer.ReceiveAsync(It.IsAny<int>(), It.IsAny<TimeSpan?>(), It.IsAny<CancellationToken>()))
640-
.ReturnsAsync(new List<EventData> { new EventData(new BinaryData(Array.Empty<byte>())), new EventData(new BinaryData(Array.Empty<byte>())) });
640+
.Returns<int, object, CancellationToken>((_, _, cancelToken) =>
641+
{
642+
cancelToken.ThrowIfCancellationRequested<TaskCanceledException>();
643+
return Task.FromResult((IReadOnlyList<EventData>)new List<EventData> { new EventData(new BinaryData(Array.Empty<byte>())), new EventData(new BinaryData(Array.Empty<byte>())) });
644+
});
641645

642646
mockProcessor
643647
.Setup(processor => processor.CreateConnection())
644648
.Returns(mockConnection.Object);
645649

646650
mockProcessor
647651
.Setup(processor => processor.ProcessEventBatchAsync(It.IsAny<EventProcessorPartition>(), It.IsAny<IReadOnlyList<EventData>>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
648-
.Returns(Task.CompletedTask)
649652
.Callback(() =>
650653
{
651654
if ((stopInvoked) && (!cancellationSource.Token.IsCancellationRequested))
@@ -654,7 +657,8 @@ public async Task StopProcessingStopsProcessing(bool async)
654657
}
655658

656659
completionSource.TrySetResult(true);
657-
});
660+
})
661+
.Returns(Task.CompletedTask);
658662

659663
mockConnection
660664
.Setup(connection => connection.CreateTransportConsumer(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<EventPosition>(), It.IsAny<EventHubsRetryPolicy>(), It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<long>(), It.IsAny<uint?>(), It.IsAny<long?>()))

0 commit comments

Comments
 (0)