Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions test/js/node/worker_threads/worker_destruction.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, bunRun } from "harness";
import { bunEnv, bunExe, bunRun, isDebug } from "harness";
import { join } from "path";

// worker_thread_check.ts runs RUN_COUNT rounds of CONCURRENCY Workers, terminating each as it comes
// online. On a debug+ASAN build one worker_threads spawn + terminate costs ~2.4s of CPU (~30ms on
// release), and the three cases run concurrently with each other and with the test below them, so the
// file needs roughly 3 x CONCURRENCY x 2.4s + ~8s of CPU, spread over the available cores, to fit the
// 5s default timeout: 5 x 10 takes ~30s per case, and even one round of 3 per case (~29s of CPU) times
// out on 4 to 6 cores. Debug builds therefore take each teardown path once (~15s of CPU, 3 to 4s per
// test on 4 cores); release builds, which is all CI runs (ASAN lanes included), keep the full count.
const RUN_COUNT = isDebug ? 1 : 5;
const CONCURRENCY = isDebug ? 1 : 10;

describe("Worker destruction", () => {
const method = ["Bun.connect", "Bun.listen", "fetch"];
describe.each(method)("bun when %s is used in a Worker that is terminating", method => {
test.concurrent("exits cleanly", async () => {
expect(await bunRun([join(import.meta.dir, "worker_thread_check.ts"), method])).toSpawn();
const result = await bunRun([join(import.meta.dir, "worker_thread_check.ts"), method], {
RUN_COUNT: String(RUN_COUNT),
CONCURRENCY: String(CONCURRENCY),
});
expect(result).toSpawn();
// One line per completed round, so a misread count cannot turn this into a no-op run.
expect(result.stdout.split("\n").map(line => line.replace(/ RSS \d+ MB$/, ""))).toEqual(
Array(RUN_COUNT).fill(`Spawned ${CONCURRENCY} workers`),
);
});
});

Expand Down
5 changes: 3 additions & 2 deletions test/js/node/worker_threads/worker_thread_check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const CONCURRENCY = 10;
const RUN_COUNT = 5;
// worker_destruction.test.ts lowers these on debug builds; see the note there.
const CONCURRENCY = Number(process.env.CONCURRENCY ?? 10);
const RUN_COUNT = Number(process.env.RUN_COUNT ?? 5);

import { Worker, isMainThread, workerData } from "worker_threads";

Expand Down