diff options
author | 2023-07-05 03:46:10 -0700 | |
---|---|---|
committer | 2023-07-05 03:46:10 -0700 | |
commit | 3aaec120e7ac26b3904895d2783a08352b63201a (patch) | |
tree | 7cc846f9ed6f12fcf4384d6794591a9506d01a76 /test/js/web/timers/setTimeout.test.js | |
parent | c864976da6140c1c92dae8472b1813a1eca8a78c (diff) | |
download | bun-3aaec120e7ac26b3904895d2783a08352b63201a.tar.gz bun-3aaec120e7ac26b3904895d2783a08352b63201a.tar.zst bun-3aaec120e7ac26b3904895d2783a08352b63201a.zip |
Fixes #3512 (#3526)
* Fixes #3512
* Fix `clearTimeout` and `clearInterval` not cancelling jobs same-tick
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'test/js/web/timers/setTimeout.test.js')
-rw-r--r-- | test/js/web/timers/setTimeout.test.js | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/test/js/web/timers/setTimeout.test.js b/test/js/web/timers/setTimeout.test.js index dbe89dea8..eef6bbae0 100644 --- a/test/js/web/timers/setTimeout.test.js +++ b/test/js/web/timers/setTimeout.test.js @@ -1,5 +1,7 @@ +import { spawnSync } from "bun"; import { it, expect } from "bun:test"; - +import { bunEnv, bunExe } from "harness"; +import path from "node:path"; it("setTimeout", async () => { var lastID = -1; const result = await new Promise((resolve, reject) => { @@ -172,11 +174,56 @@ it.skip("order of setTimeouts", done => { Promise.resolve().then(maybeDone(() => nums.push(1))); }); +it("setTimeout -> refresh", () => { + const { exitCode, stdout } = spawnSync({ + cmd: [bunExe(), path.join(import.meta.dir, "setTimeout-unref-fixture.js")], + env: bunEnv, + }); + expect(exitCode).toBe(0); + expect(stdout.toString()).toBe("SUCCESS\n"); +}); + +it("setTimeout -> unref -> ref works", () => { + const { exitCode, stdout } = spawnSync({ + cmd: [bunExe(), path.join(import.meta.dir, "setTimeout-unref-fixture-4.js")], + env: bunEnv, + }); + expect(exitCode).toBe(0); + expect(stdout.toString()).toBe("TEST PASSED!\n"); +}); + +it("setTimeout -> ref -> unref works, even if there is another timer", () => { + const { exitCode, stdout } = spawnSync({ + cmd: [bunExe(), path.join(import.meta.dir, "setTimeout-unref-fixture-2.js")], + env: bunEnv, + }); + expect(exitCode).toBe(0); + expect(stdout.toString()).toBe(""); +}); + +it("setTimeout -> ref -> unref works", () => { + const { exitCode, stdout } = spawnSync({ + cmd: [bunExe(), path.join(import.meta.dir, "setTimeout-unref-fixture-5.js")], + env: bunEnv, + }); + expect(exitCode).toBe(0); + expect(stdout.toString()).toBe(""); +}); + +it("setTimeout -> unref doesn't keep event loop alive forever", () => { + const { exitCode, stdout } = spawnSync({ + cmd: [bunExe(), path.join(import.meta.dir, "setTimeout-unref-fixture-3.js")], + env: bunEnv, + }); + expect(exitCode).toBe(0); + expect(stdout.toString()).toBe(""); +}); + it("setTimeout should refresh N times", done => { let count = 0; let timer = setTimeout(() => { count++; - timer.refresh(); + expect(timer.refresh()).toBe(timer); }, 50); setTimeout(() => { |