Centralize test timeout constants to fix sporadic CI failures (#1210)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Co-authored-by: Eric StJohn <ericstj@microsoft.com>
This commit is contained in:
Copilot 2026-01-29 22:06:17 -05:00 • committed by GitHub
commit 8e57ac6b0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 35 deletions

View file

@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using System.ComponentModel;
namespace ModelContextProtocol.Tests.Client;
@ -50,7 +51,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Assert
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts = new CancellationTokenSource(TestConstants.DefaultTimeout);
var receivedNotification = await notificationReceived.Task.WaitAsync(cts.Token);
Assert.NotNull(receivedNotification);
Assert.Equal(resourceUri, receivedNotification.Uri);
@ -92,7 +93,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Assert
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts = new CancellationTokenSource(TestConstants.DefaultTimeout);
await correctNotificationReceived.Task.WaitAsync(cts.Token);
// Give a small delay to ensure no other notifications are processed
@ -168,7 +169,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Assert
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts = new CancellationTokenSource(TestConstants.DefaultTimeout);
var receivedNotification = await notificationReceived.Task.WaitAsync(cts.Token);
Assert.NotNull(receivedNotification);
Assert.Equal(resourceUri.AbsoluteUri, receivedNotification.Uri);
@ -263,7 +264,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Assert
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts = new CancellationTokenSource(TestConstants.DefaultTimeout);
var combined = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, TestContext.Current.CancellationToken);
await Task.WhenAll(
notification1Received.Task.WaitAsync(combined.Token),
@ -341,7 +342,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Assert - Both handlers should be invoked
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts = new CancellationTokenSource(TestConstants.DefaultTimeout);
var combined = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, TestContext.Current.CancellationToken);
await Task.WhenAll(
handler1Called.Task.WaitAsync(combined.Token),
@ -360,7 +361,7 @@ public class McpClientResourceSubscriptionTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken);
// Wait for handler2 to be called again
using var cts2 = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cts2 = new CancellationTokenSource(TestConstants.DefaultTimeout);
var combined2 = CancellationTokenSource.CreateLinkedTokenSource(cts2.Token, TestContext.Current.CancellationToken);
await handler2CalledAgain.Task.WaitAsync(combined2.Token);

View file

@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using System.Text.Json;
namespace ModelContextProtocol.Tests.Client;
@ -219,7 +220,7 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
Assert.Equal(McpTaskStatus.Working, mcpTask.Status);
// Wait for sampling to complete
await samplingCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingCompleted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Poll until task is complete
McpTask taskStatus;
@ -375,7 +376,7 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
Assert.Equal(McpTaskStatus.Working, mcpTask.Status);
// Wait for elicitation to complete
await elicitationCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await elicitationCompleted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Poll until task is complete
McpTask taskStatus;
@ -488,7 +489,7 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
TestContext.Current.CancellationToken);
// Wait for sampling to start
await samplingStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingStarted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Act - Cancel the task
var cancelledTask = await Server.CancelTaskAsync(mcpTask.TaskId, TestContext.Current.CancellationToken);
@ -600,8 +601,8 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
// Wait for both Working and Completed notifications to arrive
// The notifications are sent asynchronously so we need to wait for both
await Task.WhenAll(
workingNotificationReceived.Task.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken),
completedNotificationReceived.Task.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken));
workingNotificationReceived.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken),
completedNotificationReceived.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken));
// Assert - Should have received notifications for status transitions
await notificationHandler.DisposeAsync();
@ -653,7 +654,7 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
TestContext.Current.CancellationToken);
// Wait for sampling attempt
await samplingAttempted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingAttempted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Poll until task status changes
McpTask taskStatus;
@ -703,7 +704,7 @@ public class McpClientTaskSamplingElicitationTests : ClientServerTestBase
TestContext.Current.CancellationToken);
// Wait for elicitation attempt
await elicitationAttempted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await elicitationAttempted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Poll until task status changes
McpTask taskStatus;