Skip to content

Commit e2d3ba0

Browse files
committed
Fix flaky test
1 parent 1fae1f6 commit e2d3ba0

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

tests/ModelContextProtocol.AspNetCore.Tests/ResumabilityIntegrationTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public async Task Client_CanResumeUnsolicitedMessageStream_AfterDisconnection()
344344
return default;
345345
});
346346

347+
// Wait for the client's GET SSE stream to be established before sending notifications
348+
await faultingStreamHandler.WaitForStreamAsync(TestContext.Current.CancellationToken);
349+
347350
// Send a custom notification to the client on the unsolicited message stream
348351
await server.SendNotificationAsync(CustomNotificationMethod, new JsonObject { ["message"] = InitialMessage }, cancellationToken: TestContext.Current.CancellationToken);
349352

tests/ModelContextProtocol.AspNetCore.Tests/Utils/FaultingStreamHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ internal sealed class FaultingStreamHandler : DelegatingHandler
1111
{
1212
private FaultingStream? _lastStream;
1313
private TaskCompletionSource? _reconnectTcs;
14+
private TaskCompletionSource _streamAvailableTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
15+
16+
public Task WaitForStreamAsync(CancellationToken cancellationToken = default)
17+
=> _streamAvailableTcs.Task.WaitAsync(cancellationToken);
1418

1519
public async Task<ReconnectAttempt> TriggerFaultAsync(CancellationToken cancellationToken)
1620
{
@@ -24,6 +28,7 @@ public async Task<ReconnectAttempt> TriggerFaultAsync(CancellationToken cancella
2428
throw new InvalidOperationException("Cannot trigger a fault while already waiting for reconnection.");
2529
}
2630

31+
_streamAvailableTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
2732
_reconnectTcs = new();
2833
await _lastStream.TriggerFaultAsync(cancellationToken);
2934

@@ -63,6 +68,8 @@ protected override async Task<HttpResponseMessage> SendAsync(
6368
}
6469

6570
response.Content = newContent;
71+
72+
_streamAvailableTcs.TrySetResult();
6673
}
6774

6875
return response;

0 commit comments

Comments
 (0)