From a02d97ea614bcba8d73d0ebf547e57d2a0fb4765 Mon Sep 17 00:00:00 2001 From: lyra-setkernel <341047397+lyra-setkernel@users.noreply.github.com> Date: Wed, 2 Dec 2026 21:48:27 +0000 Subject: [PATCH] test: run the test files in parallel with shorter test-only waits (#2) Each test file gets its own two local ports, from the number its name starts with, so the files no longer queue on port 18435 and run in parallel. A test-only RUNCODER_TIME_SCALE (default 1) scales the keep-alive retry gap, the delete retry gap and the minute behind the idle, time-limit and download timers; the tests set it to 0.2 with a half-second tick. pnpm check goes from 316s to 75s on Linux, with the same 38 tests. Co-Authored-By: Claude Opus 5.5 --- package.json | 2 +- scripts/check.mjs | 5 +++-- src/cloud.mjs | 4 ++-- src/config.mjs | 3 ++- src/session.mjs | 6 +++--- src/tunnel.mjs | 2 +- tests/2-recovery.test.mjs | 8 +++++--- tests/helpers.mjs | 12 +++++++++--- 8 files changed, 26 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index efc7df7..e9ccc9b 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "os": ["darwin", "linux", "win32"], "packageManager": "pnpm@12.25.1", "scripts": { - "test": "node --test --test-concurrency=1 --test-force-exit --test-timeout=300000 \"tests/*.test.mjs\"", + "test": "node --test --test-concurrency=8 --test-force-exit --test-timeout=300000 \"tests/*.test.mjs\"", "check": "node scripts/check.mjs" }, "dependencies": { diff --git a/scripts/check.mjs b/scripts/check.mjs index 7ecdc45..0e98dcf 100644 --- a/scripts/check.mjs +++ b/scripts/check.mjs @@ -1,5 +1,6 @@ // The repo's whole check, the same on macOS, Linux and Windows: a syntax pass over every -// module, then the test suite (fake gcloud, fake Google APIs, fake Ollama; nothing reaches Google). +// module, then the test suite (fake gcloud, fake Google APIs, fake Ollama; nothing reaches Google), +// with the test files running in parallel. // pnpm check import { readdirSync } from "node:fs"; import { join, dirname } from "node:path"; @@ -25,5 +26,5 @@ for (const f of files) { console.log(`syntax: ${files.length - bad}/${files.length} files ok`); if (bad) process.exit(1); -const t = spawnSync(process.execPath, ["--test", "--test-concurrency=1", "--test-force-exit", "--test-timeout=300000", "tests/*.test.mjs"], { stdio: "inherit", cwd: root }); +const t = spawnSync(process.execPath, ["--test", "--test-concurrency=8", "--test-force-exit", "--test-timeout=300000", "tests/*.test.mjs"], { stdio: "inherit", cwd: root }); process.exit(t.status ?? 1); diff --git a/src/cloud.mjs b/src/cloud.mjs index d904b5f..ec9f32d 100644 --- a/src/cloud.mjs +++ b/src/cloud.mjs @@ -52,7 +52,7 @@ export async function deleteEverything(s) { regions = await serviceRegions(s); } catch { warn(`Could not list Cloud Run services (attempt ${attempt} of 5); retrying.`); - await sleep(10); + await sleep(10 * s.timeScale); continue; } if (!regions.length) return true; @@ -61,7 +61,7 @@ export async function deleteEverything(s) { const r = await gcloud(s, ["run", "services", "delete", s.service, `--region=${region}`]); if (r.code !== 0) { warn(`Delete in ${region} failed (attempt ${attempt} of 5); retrying.`); // e.g. a deploy still in progress - await sleep(10); + await sleep(10 * s.timeScale); } } } diff --git a/src/config.mjs b/src/config.mjs index 5aca70e..4a347ac 100644 --- a/src/config.mjs +++ b/src/config.mjs @@ -90,7 +90,8 @@ export function loadSettings() { currency: get("CURRENCY", "USD"), // the billing account's currency (setup detects it) freeTier: get("FREE_TIER", "1") !== "0", // off if other Cloud Run use on the account takes it allowPaid: get("ALLOW_PAID", "0") === "1", // lets a run without a keyboard go past the credit - // For tests: where Google's APIs are, and keys typed through a pipe count as a keyboard. + // For tests: shorter waits, where Google's APIs are, and keys typed through a pipe count as a keyboard. + timeScale: num("TIME_SCALE", 1), // multiplies the retry gaps and the length of a minute for the timers monitoringApi: get("MONITORING_API", "https://monitoring.googleapis.com"), billingApi: get("BILLING_API", "https://cloudbilling.googleapis.com"), interactive: process.stdin.isTTY === true || env.RUNCODER_INTERACTIVE === "1", diff --git a/src/session.mjs b/src/session.mjs index a36a154..862ffaf 100644 --- a/src/session.mjs +++ b/src/session.mjs @@ -87,7 +87,7 @@ async function keepAlive(s, tunnel, region) { // One failed check is not enough: a tunnel hiccup ("connection reset") looks the same for a moment. for (let i = 0; i < 3; i++) { if (await modelReady(s)) return; - await sleep(5); + await sleep(5 * s.timeScale); } const code = await httpCode(s, "/api/tags"); if (code === 200) { @@ -124,7 +124,7 @@ async function session(s, workdir, tunnel, region) { qwenGoneAt = 0; } await keepAlive(s, tunnel, region); - const minutes = Math.floor((Date.now() - started) / 60e3); + const minutes = Math.floor((Date.now() - started) / (60e3 * s.timeScale)); if (qwenAlive()) { qwenSeen = true; @@ -134,7 +134,7 @@ async function session(s, workdir, tunnel, region) { say(`Qwen Code was closed. Press q to delete everything, or r to reopen it where you left off. Shutting down by itself in ${s.qwenIdleMinutes} minutes.`); notify("Qwen Code closed: press q in the runcoder window to delete everything"); } - if (qwenGoneAt && Date.now() - qwenGoneAt >= s.qwenIdleMinutes * 60e3) { + if (qwenGoneAt && Date.now() - qwenGoneAt >= s.qwenIdleMinutes * 60e3 * s.timeScale) { say(`Qwen Code has been closed for ${s.qwenIdleMinutes} minutes; shutting down.`); return; } diff --git a/src/tunnel.mjs b/src/tunnel.mjs index b6bce4c..b002033 100644 --- a/src/tunnel.mjs +++ b/src/tunnel.mjs @@ -103,7 +103,7 @@ async function pullProgress(s, region) { } export async function waitForModel(s, region, tunnel, costSoFar) { - const deadline = Date.now() + s.pullTimeoutMinutes * 60e3; + const deadline = Date.now() + s.pullTimeoutMinutes * 60e3 * s.timeScale; let last = ""; say(`Downloading ${s.model} to the server (about 10 minutes for 30 GB)...`); while (!(await modelReady(s))) { diff --git a/tests/2-recovery.test.mjs b/tests/2-recovery.test.mjs index ed9518b..b12e0ac 100644 --- a/tests/2-recovery.test.mjs +++ b/tests/2-recovery.test.mjs @@ -35,7 +35,7 @@ test("the server lost the model: detected after retries, downloaded again", asyn const ctx = sandbox("t6"); const r = start(ctx); assert.ok(await r.waitFor("Press q here", 60), r.out()); - setReady(ctx, 40); // the restarted fake stays empty longer than the three retries + setReady(ctx, 10); // the restarted fake stays empty longer than the three retries (1s apart in tests) await killProxy(ctx); assert.ok(await r.waitFor("lost the model", 40), r.out()); assert.ok(await r.waitFor("runcoder is ready again", 90), r.out()); @@ -48,10 +48,12 @@ test("a brief hiccup is not reported as a lost model", async () => { const ctx = sandbox("t24"); const r = start(ctx); assert.ok(await r.waitFor("Press q here", 60), r.out()); + // In tests the keep-alive checks every half second and retries 1s apart: an empty model for 1.5s + // is seen, but is back before the third retry, and a lost model would be reported within 4s. touch(ctx, "empty"); - await sleep(7); + await sleep(1.5); rmSync(fake(ctx, "empty")); - await sleep(12); + await sleep(5); assert.ok(!r.has("lost the model"), r.out()); r.press("q"); await r.done(); diff --git a/tests/helpers.mjs b/tests/helpers.mjs index f9761d2..954fff4 100644 --- a/tests/helpers.mjs +++ b/tests/helpers.mjs @@ -5,13 +5,16 @@ import { spawn } from "node:child_process"; import { createServer } from "node:http"; import { mkdirSync, rmSync, writeFileSync, readFileSync, existsSync } from "node:fs"; -import { join, delimiter, dirname } from "node:path"; +import { join, delimiter, dirname, basename } from "node:path"; import { fileURLToPath } from "node:url"; import { killTree } from "../src/proc.mjs"; export const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); export const BIN = join(ROOT, "bin", "runcoder.mjs"); -export const PORT = 18435; +// The test files run in parallel, each in its own process, so each has its own two local ports +// (PORT and PORT + 1) from the number its name starts with: 1-run.test.mjs gets 18437 and 18438. +const fileNumber = Number(basename(process.argv[1] ?? "").match(/^(\d+)-/)?.[1] ?? 0); +export const PORT = 18435 + 2 * fileNumber; export const windows = process.platform === "win32"; const TMP = join(ROOT, "tests", "tmp"); @@ -82,7 +85,10 @@ export function sandbox(name, { config = true, billed = [], googleDown = false, XDG_STATE_HOME: join(dir, "state"), XDG_DATA_HOME: join(dir, "data"), RUNCODER_PORT: String(PORT), - RUNCODER_TICK: "2", + // A keep-alive check every half second, its retries 1s apart (5s by default), deletes retried + // 2s apart (10s), and a minute of 12s for the idle, time-limit and download timers. + RUNCODER_TICK: "0.5", + RUNCODER_TIME_SCALE: "0.2", RUNCODER_OPEN: "none", RUNCODER_MONITORING_API: google.url, RUNCODER_BILLING_API: google.url,