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
7 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;

View file

@ -2,6 +2,7 @@ using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Tests.Utils;
namespace ModelContextProtocol.Tests.Configuration;
@ -275,7 +276,7 @@ public partial class UrlElicitationTests(ITestOutputHelper testOutputHelper) : C
Assert.NotNull(capturedMessage);
Assert.Contains(capturedElicitationId, capturedUrl);
var notifiedElicitationId = await completionNotification.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
var notifiedElicitationId = await completionNotification.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
Assert.Equal(capturedElicitationId, notifiedElicitationId);
}

View file

@ -95,7 +95,7 @@ public class AutomaticInputRequiredStatusTests : LoggedTest
cancellationToken: TestContext.Current.CancellationToken);
// Wait for the sampling request to be received by the client
await samplingRequestReceived.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingRequestReceived.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Check the task status while sampling is in progress
var statusDuringSampling = await taskStore.GetTaskAsync(
@ -194,7 +194,7 @@ public class AutomaticInputRequiredStatusTests : LoggedTest
cancellationToken: TestContext.Current.CancellationToken);
// Wait for the elicitation request to be received
await elicitationRequestReceived.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await elicitationRequestReceived.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Check the task status while elicitation is in progress
var statusDuringElicitation = await taskStore.GetTaskAsync(
@ -301,7 +301,7 @@ public class AutomaticInputRequiredStatusTests : LoggedTest
cancellationToken: TestContext.Current.CancellationToken);
// Wait for sampling to complete inside the tool
await samplingCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingCompleted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Small delay to ensure status update is processed
await Task.Delay(50, TestContext.Current.CancellationToken);
@ -380,7 +380,7 @@ public class AutomaticInputRequiredStatusTests : LoggedTest
arguments: new Dictionary<string, object?> { ["prompt"] = "Hello" },
cancellationToken: TestContext.Current.CancellationToken);
await samplingCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await samplingCompleted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Assert - No task should exist (tool was not called as a task)
var tasks = await taskStore.ListTasksAsync(cancellationToken: TestContext.Current.CancellationToken);

View file

@ -755,7 +755,7 @@ public class McpServerTaskMethodsTests : LoggedTest
await transport.SendClientMessageAsync(initializeRequest, cancellationToken);
// Wait for the initialize response to be sent
await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
await tcs.Task.WaitAsync(TestConstants.DefaultTimeout, cancellationToken);
}
#endregion

View file

@ -692,7 +692,7 @@ public class McpServerTests : LoggedTest
TestContext.Current.CancellationToken
);
var error = await receivedMessage.Task.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
var error = await receivedMessage.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
Assert.NotNull(error);
Assert.NotNull(error.Error);
Assert.Equal((int)errorCode, error.Error.Code);
@ -744,7 +744,7 @@ public class McpServerTests : LoggedTest
TestContext.Current.CancellationToken
);
var error = await receivedMessage.Task.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
var error = await receivedMessage.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
Assert.NotNull(error);
Assert.NotNull(error.Error);
Assert.Equal((int)ErrorCode, error.Error.Code);
@ -786,7 +786,7 @@ public class McpServerTests : LoggedTest
}
);
var response = await receivedMessage.Task.WaitAsync(TimeSpan.FromSeconds(10));
var response = await receivedMessage.Task.WaitAsync(TestConstants.DefaultTimeout);
Assert.NotNull(response);
assertResult(server, response.Result);
@ -887,7 +887,7 @@ public class McpServerTests : LoggedTest
await transport.SendClientMessageAsync(initializeRequest, cancellationToken);
// Wait for the initialize response to be sent
await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
await tcs.Task.WaitAsync(TestConstants.DefaultTimeout, cancellationToken);
}
private sealed class TestServerForIChatClient(bool supportsSampling) : McpServer

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.Server;
@ -77,10 +78,10 @@ public class TaskCancellationIntegrationTests : ClientServerTestBase
Assert.NotNull(callResult.Task);
// Wait for the tool to start executing
await _toolStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await _toolStarted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Assert - Wait for the cancellation to fire (should happen when TTL expires)
var cancelled = await _toolCancellationFired.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
var cancelled = await _toolCancellationFired.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
Assert.True(cancelled, "Tool's CancellationToken should have been triggered when TTL expired");
// Note: TTL-based expiration does not explicitly set task status to Cancelled.
@ -108,13 +109,13 @@ public class TaskCancellationIntegrationTests : ClientServerTestBase
string taskId = callResult.Task.TaskId;
// Wait for the tool to start executing
await _toolStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
await _toolStarted.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
// Act - Explicitly cancel the task
var cancelledTask = await client.CancelTaskAsync(taskId, cancellationToken: TestContext.Current.CancellationToken);
// Assert - Wait for the cancellation to propagate to the tool
var cancelled = await _toolCancellationFired.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
var cancelled = await _toolCancellationFired.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
Assert.True(cancelled, "Tool's CancellationToken should have been triggered by explicit cancellation");
// Verify task status
@ -255,7 +256,7 @@ public class TaskCancellationConcurrencyTests : ClientServerTestBase
{
lock (_lock)
{
return _toolStarts[marker].Task.WaitAsync(TimeSpan.FromSeconds(5), ct);
return _toolStarts[marker].Task.WaitAsync(TestConstants.DefaultTimeout, ct);
}
}
@ -263,7 +264,7 @@ public class TaskCancellationConcurrencyTests : ClientServerTestBase
{
lock (_lock)
{
return _toolCancellations[marker].Task.WaitAsync(TimeSpan.FromSeconds(5), ct);
return _toolCancellations[marker].Task.WaitAsync(TestConstants.DefaultTimeout, ct);
}
}