File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed
tests/ModelContextProtocol.Tests Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change 11using System . Diagnostics ;
2+ using System . Net ;
23
34namespace 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 {
You can’t perform that action at this time.
0 commit comments