Skip to content

Commit e4692fa

Browse files
authored
Remove 10 second wait from docker tests (#1188)
1 parent 84dcfbc commit e4692fa

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

tests/ModelContextProtocol.Tests/EverythingSseServerFixture.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Net;
23

34
namespace ModelContextProtocol.Tests;
45

@@ -31,8 +32,30 @@ public async Task StartAsync()
3132
_ = Process.Start(processStartInfo)
3233
?? throw new InvalidOperationException($"Could not start process for {processStartInfo.FileName} with '{processStartInfo.Arguments}'.");
3334

34-
// Wait for the server to start
35-
await Task.Delay(10000);
35+
// Poll until the server is ready (up to 30 seconds)
36+
using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) };
37+
var endpoint = $"http://localhost:{_port}/sse";
38+
var deadline = DateTime.UtcNow.AddSeconds(30);
39+
40+
while (DateTime.UtcNow < deadline)
41+
{
42+
try
43+
{
44+
using var response = await httpClient.GetAsync(endpoint, HttpCompletionOption.ResponseHeadersRead);
45+
if (response.IsSuccessStatusCode || response.StatusCode is HttpStatusCode.MethodNotAllowed)
46+
{
47+
return;
48+
}
49+
}
50+
catch (Exception e) when (e is HttpRequestException or OperationCanceledException)
51+
{
52+
// server not ready
53+
}
54+
55+
await Task.Delay(100);
56+
}
57+
58+
throw new InvalidOperationException($"Docker container failed to start within 30 seconds on port {_port}");
3659
}
3760
public async ValueTask DisposeAsync()
3861
{

0 commit comments

Comments
 (0)