Skip to content

Commit 057f983

Browse files
authored
perf(test): synchronize MCP lifecycle tests (#124389)
1 parent 4823b23 commit 057f983

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

‎src/agents/agent-bundle-mcp-runtime.test.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,22 @@ async function waitForPredicate(
448448
throw new Error(`Timed out waiting for ${description}`);
449449
}
450450

451+
async function waitForFileTextCount(
452+
filePath: string,
453+
expectedText: string,
454+
expectedCount: number,
455+
timeoutMs: number,
456+
): Promise<void> {
457+
await waitForPredicate(
458+
async () => {
459+
const text = await fs.readFile(filePath, "utf8").catch(() => "");
460+
return text.split(expectedText).length - 1 >= expectedCount;
461+
},
462+
`${expectedCount} occurrences of ${expectedText} in ${filePath}`,
463+
timeoutMs,
464+
);
465+
}
466+
451467
/** Waits for a replacement child to register a pid different from the one that died. */
452468
async function waitForChangedPid(
453469
pidPath: string,
@@ -1169,6 +1185,7 @@ describe("session MCP runtime", () => {
11691185
const tempDir = tempDirTracker.make("bundle-mcp-catalog-retry-");
11701186
const retryServerPath = path.join(tempDir, "retry-list-tools.mjs");
11711187
const retryLogPath = path.join(tempDir, "retry-server.log");
1188+
const retryReleasePath = path.join(tempDir, "retry.release");
11721189
const healthyServerPath = path.join(tempDir, "healthy-list-tools.mjs");
11731190
const healthyLogPath = path.join(tempDir, "healthy-server.log");
11741191
let nowMs = 10_000;
@@ -1237,7 +1254,7 @@ describe("session MCP runtime", () => {
12371254
await writeListToolsMcpServer({
12381255
filePath: retryServerPath,
12391256
logPath: retryLogPath,
1240-
initializeDelayMs: 500,
1257+
listToolsReleasePath: retryReleasePath,
12411258
});
12421259
await expect(runtime.getCatalog()).resolves.toBe(failedCatalog);
12431260

@@ -1250,9 +1267,16 @@ describe("session MCP runtime", () => {
12501267
);
12511268
expect(staleCatalog).toBe(failedCatalog);
12521269
expect(staleCatalog.diagnostics?.[0]?.serverName).toBe("retryServer");
1270+
await waitForFileTextCount(
1271+
retryLogPath,
1272+
"recv tools/list",
1273+
2,
1274+
LIST_TOOLS_SERVER_LOG_TIMEOUT_MS,
1275+
);
12531276
await expect(runtime.callTool("healthyServer", "healthy_tool", {})).resolves.toMatchObject({
12541277
isError: false,
12551278
});
1279+
await fs.writeFile(retryReleasePath, "release", "utf8");
12561280

12571281
await waitForPredicate(
12581282
() => staticRuntime.peekCatalog()?.servers.retryServer !== undefined,
@@ -1678,13 +1702,15 @@ process.on("SIGINT", shutdown);`,
16781702
const serverPath = path.join(tempDir, "server.mjs");
16791703
const logPath = path.join(tempDir, "server.log");
16801704
const pidPath = path.join(tempDir, "server.pid");
1705+
const listToolsReleasePath = path.join(tempDir, "list-tools.release");
16811706
const healthyServerPath = path.join(tempDir, "healthy.mjs");
16821707
const healthyLogPath = path.join(tempDir, "healthy.log");
1708+
await fs.writeFile(listToolsReleasePath, "release", "utf8");
16831709
await writeListToolsMcpServer({
16841710
filePath: serverPath,
16851711
logPath,
16861712
pidPath,
1687-
initializeDelayMs: 750,
1713+
listToolsReleasePath,
16881714
});
16891715
await writeListToolsMcpServer({ filePath: healthyServerPath, logPath: healthyLogPath });
16901716

@@ -1708,6 +1734,7 @@ process.on("SIGINT", shutdown);`,
17081734
});
17091735
await waitForFileText(pidPath, "", LIST_TOOLS_SERVER_LOG_TIMEOUT_MS);
17101736
const pid = Number.parseInt((await fs.readFile(pidPath, "utf8")).trim(), 10);
1737+
await fs.rm(listToolsReleasePath, { force: true });
17111738
// SIGKILL rather than the default SIGTERM: this test is about what happens once the
17121739
// child is actually gone, so the kill must not race the assertions below.
17131740
process.kill(pid, "SIGKILL");
@@ -1719,13 +1746,16 @@ process.on("SIGINT", shutdown);`,
17191746
"closed transport to schedule a catalog retry",
17201747
LIST_TOOLS_SERVER_LOG_TIMEOUT_MS,
17211748
);
1749+
await expect(runtime.callTool("child", "slow_tool", {})).rejects.toThrow("is not connected");
1750+
await waitForFileTextCount(logPath, "recv tools/list", 2, LIST_TOOLS_SERVER_LOG_TIMEOUT_MS);
17221751
await expect(
17231752
withTestTimeout(
17241753
runtime.callTool("healthy", "slow_tool", {}),
17251754
400,
17261755
"healthy sibling stalled during reconnect",
17271756
),
17281757
).resolves.toMatchObject({ isError: false });
1758+
await fs.writeFile(listToolsReleasePath, "release", "utf8");
17291759
await waitForPredicate(
17301760
async () => {
17311761
try {

0 commit comments

Comments
 (0)