-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest-runner.ts
More file actions
56 lines (53 loc) · 1.72 KB
/
Copy pathtest-runner.ts
File metadata and controls
56 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Test-runner utilities for server-conformance scenarios.
*
* Used by `*.test.ts` runner files that auto-spawn a fixture binary
* before running scenarios. These helpers are language-agnostic and
* harness-only — they don't touch MCP protocol, so they don't belong
* in the SDK.
*
* Single responsibility today: TCP readiness polling. Spawn / cleanup
* scaffolding stays inline in each runner so the file reads top-to-bottom
* without indirection (per AGENTS.md "repetitive check blocks are fine").
*/
import { connect } from 'net';
/**
* Poll the host/port of the given URL until a TCP connection succeeds
* or the timeout elapses. Language-agnostic readiness check — works
* for any server that binds before serving requests.
*/
export async function waitForServerReady(
url: string,
timeoutMs: number
): Promise<void> {
const u = new URL(url);
const port = parseInt(u.port || (u.protocol === 'https:' ? '443' : '80'), 10);
const host = u.hostname;
const deadline = Date.now() + timeoutMs;
let lastErr: Error | null = null;
while (Date.now() < deadline) {
try {
await new Promise<void>((resolve, reject) => {
const socket = connect({ host, port }, () => {
socket.end();
resolve();
});
socket.once('error', (err) => {
socket.destroy();
reject(err);
});
socket.setTimeout(1_000, () => {
socket.destroy();
reject(new Error('connect timeout'));
});
});
return;
} catch (err) {
lastErr = err as Error;
await new Promise((r) => setTimeout(r, 200));
}
}
throw new Error(
`${host}:${port} did not accept TCP connections (last: ${lastErr?.message ?? 'unknown'})`
);
}