Skip to content

Commit 349e7e4

Browse files
perf(tests): cut remaining fixed waits from slowest test files
1 parent 7c721bd commit 349e7e4

10 files changed

Lines changed: 314 additions & 297 deletions

File tree

‎scripts/ci-timings.json‎

Lines changed: 281 additions & 281 deletions
Large diffs are not rendered by default.

‎scripts/test-parallel.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ function processAlive(pid: number): boolean {
1515
}
1616

1717
// These probes are `bun -e` one-liners, so they never import project modules
18-
// and finish in seconds. The tick test keeps a ~1.5s stall window: under the
18+
// and finish in seconds. The tick test keeps a ~0.9s stall window: under the
1919
// `--parallel` load this runner exists to enable, a delayed output tick can
2020
// look like a stall against a tighter window, so the window keeps several
2121
// ticks of scheduling headroom while a broken never-resetting timer still
2222
// fires mid-run (the tick test's total runtime exceeds the window). The
2323
// remaining tests drive children that never emit output, so their stall is
2424
// declared deterministically on the watchdog's first tick; they use a short
2525
// window so the suite does not pay the tick test's headroom in wall clock.
26-
const TEST_STALL_MS = 1_500;
26+
const TEST_STALL_MS = 900;
2727
const SILENT_CHILD_STALL_MS = 400;
2828

2929
describe("runWithWatchdog", () => {
@@ -56,13 +56,13 @@ describe("runWithWatchdog", () => {
5656
});
5757

5858
test("output resets the stall timer", async () => {
59-
// Prints every 250ms for ~2.5s against a 1.5s stall window: a broken
59+
// Prints every 150ms for ~1.2s against a 0.9s stall window: a broken
6060
// timer that never reset would fire mid-run.
6161
const result = await runWithWatchdog({
6262
command: process.execPath,
6363
args: [
6464
"-e",
65-
"for (let i = 0; i < 10; i++) { console.log('tick', i); await new Promise(r => setTimeout(r, 200)); }",
65+
"for (let i = 0; i < 8; i++) { console.log('tick', i); await new Promise(r => setTimeout(r, 150)); }",
6666
],
6767
stallMs: TEST_STALL_MS,
6868
});
@@ -104,7 +104,7 @@ describe("runWithWatchdog", () => {
104104
command: process.execPath,
105105
args: [
106106
"-e",
107-
'process.on("SIGTERM", () => {}); setTimeout(() => process.exit(3), 1_500);',
107+
'process.on("SIGTERM", () => {}); setTimeout(() => process.exit(3), 800);',
108108
],
109109
stallMs: SILENT_CHILD_STALL_MS,
110110
onStall: () => {

‎src/agent/tool-search.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ describe("createToolSearchTool", () => {
333333
new Promise<number>(() => {
334334
// Never settles: simulates a hung authorization handshake.
335335
}),
336+
// The production bound is 1s; a short override exercises the same
337+
// bounded-wait contract without paying that in wall clock.
338+
pendingWaitMs: 50,
336339
});
337340
const out = await call(tool, { query: "linear" });
338341
expect(out).toContain("No tools matched");

‎src/agent/tool-search.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ export interface ToolSearchDeps {
287287
// below so even a stuck dependency can never hang the call. Omitted callers
288288
// (tests, ad-hoc indexes) have no pending handshakes to wait for.
289289
awaitPendingConnections?: (timeoutMs?: number) => Promise<number>;
290+
// Bounded wait before answering a miss with late-mounting tools, in ms.
291+
// Defaults to TOOL_SEARCH_PENDING_WAIT_MS; tests override this so the
292+
// bounded-wait contract is exercised without paying the production 1s.
293+
pendingWaitMs?: number;
290294
}
291295

292296
// Brief bound a tool_search miss waits for in-flight MCP handshakes before
@@ -319,16 +323,14 @@ function indent(text: string, pad: string): string {
319323
// Resolves undefined when this race itself times out.
320324
async function racePendingCount(
321325
awaitPending: (timeoutMs?: number) => Promise<number>,
326+
waitMs: number,
322327
): Promise<number | undefined> {
323328
let timer: ReturnType<typeof setTimeout> | undefined;
324329
try {
325330
return await Promise.race([
326-
awaitPending(TOOL_SEARCH_PENDING_WAIT_MS),
331+
awaitPending(waitMs),
327332
new Promise<undefined>((resolve) => {
328-
timer = setTimeout(
329-
() => resolve(undefined),
330-
TOOL_SEARCH_PENDING_WAIT_MS,
331-
);
333+
timer = setTimeout(() => resolve(undefined), waitMs);
332334
}),
333335
]);
334336
} finally {
@@ -354,6 +356,7 @@ export function createToolSearchTool(deps: ToolSearchDeps): AgentTool {
354356
// (hung OAuth) — undefined means the wait itself timed out.
355357
const stillPending = await racePendingCount(
356358
deps.awaitPendingConnections,
359+
deps.pendingWaitMs ?? TOOL_SEARCH_PENDING_WAIT_MS,
357360
);
358361
names = deps.search(query);
359362
if (names.length === 0 && (stillPending ?? 1) > 0) {

‎src/plugins/shell-guard-plugin.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe("background run_shell (shellGuardPlugin)", () => {
286286
const result = await runWith(registry, {
287287
id: "bg1",
288288
name: "run_shell",
289-
arguments: { command: "sleep 0.5; echo finished", background: true },
289+
arguments: { command: "sleep 0.2; echo finished", background: true },
290290
});
291291
expect(result.isError).toBeUndefined();
292292
const parsed = JSON.parse(String(result.content)) as {

‎src/shell/background-shell.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("background shell registry", () => {
2525
test("start returns a handle immediately while the process runs", async () => {
2626
const registry = createBackgroundShellRegistry();
2727
const started = registry.start({
28-
command: "sleep 0.5; echo done",
28+
command: "sleep 0.2; echo done",
2929
cwd: tmpCwd,
3030
});
3131
if ("error" in started) throw new Error(started.error);

‎src/telemetry/index.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ export interface CreateTelemetryOptions {
296296
host?: string;
297297
apiKey?: string;
298298
batch?: BatchTuning;
299+
// Upper bound on how long flush() may hold up process exit, in ms.
300+
// Defaults to FLUSH_DEADLINE_MS; tests override this so the give-up
301+
// contract is exercised without paying the production 500ms.
302+
flushDeadlineMs?: number;
299303
}
300304

301305
interface QueuedEvent {
@@ -373,6 +377,7 @@ export function createTelemetry(options: CreateTelemetryOptions): Telemetry {
373377
const batchSize = options.batch?.size ?? DEFAULT_BATCH_SIZE;
374378
const batchIntervalMs =
375379
options.batch?.intervalMs ?? DEFAULT_BATCH_INTERVAL_MS;
380+
const flushDeadlineMs = options.flushDeadlineMs ?? FLUSH_DEADLINE_MS;
376381
const queueLimit = options.batch?.queueLimit ?? DEFAULT_QUEUE_LIMIT;
377382

378383
const queue: QueuedEvent[] = [];
@@ -502,7 +507,7 @@ export function createTelemetry(options: CreateTelemetryOptions): Telemetry {
502507
await Promise.race([
503508
drain(),
504509
new Promise<void>((resolve) => {
505-
const deadline = setTimeout(resolve, FLUSH_DEADLINE_MS);
510+
const deadline = setTimeout(resolve, flushDeadlineMs);
506511
deadline.unref?.();
507512
}),
508513
]);

‎tests/fixtures/rawmode-sigint/probe.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ process.stdin.resume();
1212
process.stdin.on("data", (chunk: Buffer) => {
1313
if (chunk.includes(0x03)) process.stdout.write("GOT_CTRL_C_BYTE\n");
1414
});
15+
// A SIGINT, if the kernel delivered one, would arrive synchronously with the
16+
// keypress, so a short grace after GOT_CTRL_C_BYTE still proves none is
17+
// delivered without paying a fixed 3s in wall clock.
1518
setTimeout(() => {
1619
process.stdout.write("NO_SIGINT_ON_CTRL_C\n");
1720
process.exit(0);
18-
}, 3000);
21+
}, 800);

‎tests/fixtures/rawmode-sigint/pty_probe.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ def main() -> int:
1616
os.execvp("bun", ["bun", "run", probe_path])
1717
os._exit(127)
1818

19-
time.sleep(1)
19+
time.sleep(0.4)
2020
os.write(fd, b"\x03")
2121

2222
out = b""
23-
deadline = time.time() + 4
23+
deadline = time.time() + 2.5
2424
while time.time() < deadline:
2525
ready, _, _ = select.select([fd], [], [], 0.5)
2626
if fd not in ready:

‎tests/unit/telemetry.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ test("flush gives up after its deadline when a request never settles", async ()
357357
env: {},
358358
fetchFn: impl,
359359
apiKey: "test-key",
360+
// The production deadline is 500ms; a short override exercises the same
361+
// give-up contract without paying that in wall clock.
362+
flushDeadlineMs: 30,
360363
});
361364
telemetry.capture("cli_start");
362365
const start = Date.now();

0 commit comments

Comments
 (0)