Skip to content

Commit e8e4b9a

Browse files
Copilotstephentoub
andcommitted
Fix test to properly track second handler invocation
- Moved handler2CalledAgain TaskCompletionSource to top of test - Modified handler2 to set handler2CalledAgain on second invocation - Replaced Task.Delay with proper WaitAsync on handler2CalledAgain - Test now correctly validates handler2 is invoked after handler1 disposal Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent c46f73f commit e8e4b9a

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

tests/ModelContextProtocol.Tests/Client/McpClientResourceSubscriptionTests.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public async Task SubscribeToResourceAsync_MultipleHandlersSameUri_BothReceiveNo
302302
const string resourceUri = "test://resource/1";
303303
var handler1Called = new TaskCompletionSource<bool>();
304304
var handler2Called = new TaskCompletionSource<bool>();
305+
var handler2CalledAgain = new TaskCompletionSource<bool>();
305306
var handler1Count = 0;
306307
var handler2Count = 0;
307308

@@ -320,8 +321,15 @@ public async Task SubscribeToResourceAsync_MultipleHandlersSameUri_BothReceiveNo
320321
resourceUri,
321322
(notification, ct) =>
322323
{
323-
Interlocked.Increment(ref handler2Count);
324-
handler2Called.TrySetResult(true);
324+
var count = Interlocked.Increment(ref handler2Count);
325+
if (count == 1)
326+
{
327+
handler2Called.TrySetResult(true);
328+
}
329+
else if (count == 2)
330+
{
331+
handler2CalledAgain.TrySetResult(true);
332+
}
325333
return default;
326334
},
327335
cancellationToken: TestContext.Current.CancellationToken);
@@ -345,17 +353,16 @@ await Task.WhenAll(
345353
// Dispose one subscription
346354
await subscription1.DisposeAsync();
347355

348-
// Reset the second handler's task completion
349-
var handler2CalledAgain = new TaskCompletionSource<bool>();
350-
351356
// Send another notification
352357
await Server.SendNotificationAsync(
353358
NotificationMethods.ResourceUpdatedNotification,
354359
new ResourceUpdatedNotificationParams { Uri = resourceUri },
355360
cancellationToken: TestContext.Current.CancellationToken);
356361

357-
// Wait a bit to see if handler2 gets called again
358-
await Task.Delay(100, TestContext.Current.CancellationToken);
362+
// Wait for handler2 to be called again
363+
using var cts2 = new CancellationTokenSource(TimeSpan.FromSeconds(5));
364+
var combined2 = CancellationTokenSource.CreateLinkedTokenSource(cts2.Token, TestContext.Current.CancellationToken);
365+
await handler2CalledAgain.Task.WaitAsync(combined2.Token);
359366

360367
// Assert - Only the second handler should still receive notifications
361368
// Handler1 should not have been called again (still 1)

0 commit comments

Comments
 (0)