Skip to content

Commit 2d4a4d7

Browse files
Copilotericstj
andauthored
Fix flaky test: add synchronization point in ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents
The test was racy on .NET 10 because all 3 events could be written before the readTask entered its polling loop. Add a TaskCompletionSource that the readTask signals when it receives the first event, ensuring events 2 and 3 are written only after the streaming enumerator is confirmed to be active. Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/f39d7d87-181b-4085-837a-7ec97f9c2b50 Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
1 parent 65aaf94 commit 2d4a4d7

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,16 @@ public async Task ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents()
904904
using var cts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken);
905905
cts.CancelAfter(TimeSpan.FromSeconds(10));
906906
var events = new List<SseItem<JsonRpcMessage?>>();
907+
908+
// Use a TCS as a sync point: set when the reader has confirmed receipt of the first event.
909+
// This guarantees the streaming enumerator is definitely active before we write events 2 and 3.
910+
var readerStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
911+
907912
var readTask = Task.Run(async () =>
908913
{
909914
await foreach (var evt in reader.ReadEventsAsync(cts.Token))
910915
{
916+
readerStarted.TrySetResult();
911917
events.Add(evt);
912918
if (events.Count >= 3)
913919
{
@@ -916,8 +922,13 @@ public async Task ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents()
916922
}
917923
}, CancellationToken);
918924

919-
// Write 3 new events - the reader should pick them up since it's in streaming mode
925+
// Write the first event and wait for the reader to confirm it has been received.
926+
// This establishes a synchronization point: once readerStarted is signalled, we know
927+
// the streaming loop is running and will reliably observe any subsequently written events.
920928
var event1 = await writer.WriteEventAsync(new SseItem<JsonRpcMessage?>(null), CancellationToken);
929+
await readerStarted.Task.WaitAsync(cts.Token);
930+
931+
// Write the remaining 2 events now that the reader is confirmed active
921932
var event2 = await writer.WriteEventAsync(new SseItem<JsonRpcMessage?>(null), CancellationToken);
922933
var event3 = await writer.WriteEventAsync(new SseItem<JsonRpcMessage?>(null), CancellationToken);
923934

0 commit comments

Comments
 (0)