Files
bun.sh/test/js/bun/util/sleep.test.ts
Jarred Sumner 2e02d9de28 Use ReadableStream.prototype.* in tests instead of new Response(...).* (#20937)
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Alistair Smith <hi@alistair.sh>
Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
2025-07-14 00:47:53 -07:00

75 lines
1.9 KiB
TypeScript

import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
test("sleep should saturate timeout values", async () => {
const fixturesThatShouldTimeout = [
"Infinity",
"999999999999999",
"999999999999999.999999999999999",
"999999999999999",
];
const fixturesThatSHouldCompleteInstantly = [
"0",
"0.0",
"-0",
"-0.1",
"-0.0000000000000001",
"-999999999999999",
"-999999999999999.999999999999999",
];
const toKill = fixturesThatShouldTimeout.map(timeout => {
const proc = Bun.spawn({
cmd: [bunExe(), "sleep-4ever.js", timeout],
stderr: "inherit",
stdout: "inherit",
stdin: "inherit",
env: bunEnv,
cwd: import.meta.dir,
});
return proc;
});
const start = performance.now();
const toWait = fixturesThatSHouldCompleteInstantly.map(async timeout => {
const proc = Bun.spawn({
cmd: [bunExe(), "sleep-4ever.js", timeout],
stderr: "inherit",
stdout: "inherit",
stdin: "inherit",
env: bunEnv,
cwd: import.meta.dir,
});
expect(await proc.exited).toBe(0);
expect(performance.now() - start).toBeLessThan(1000);
});
await Promise.all(toWait);
for (let i = 0; i < toKill.length; i++) {
const proc = toKill[i];
if (proc.exitCode === 0) {
console.warn("warn: Expected process #", i, "to timeout, but it didn't");
}
expect(proc.exitCode).toBe(null);
}
const allExited = Promise.all(toKill.map(proc => proc.exited));
toKill.forEach(proc => proc.kill());
await allExited;
});
test("sleep should keep the event loop alive", async () => {
const proc = Bun.spawn({
cmd: [bunExe(), "sleep-keepalive.ts"],
stderr: "inherit",
stdout: "pipe",
stdin: "inherit",
env: bunEnv,
cwd: import.meta.dir,
});
await proc.exited;
expect(proc.exitCode).toBe(0);
expect(await proc.stdout.text()).toContain("event loop was not killed");
});