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
4 changed files with 56 additions and 35 deletions

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