-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself-loop.test.js
More file actions
38 lines (32 loc) · 1.17 KB
/
Copy pathself-loop.test.js
File metadata and controls
38 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { describe, expect, test } from "bun:test";
import { createNonOverlappingTickRunner } from "./self-loop.js";
describe("createNonOverlappingTickRunner", () => {
test("skips an overlapping timer tick and allows the next one after completion", async () => {
let releaseFirst;
let calls = 0;
const firstGate = new Promise((resolve) => { releaseFirst = resolve; });
const runTick = createNonOverlappingTickRunner(async () => {
calls += 1;
if (calls === 1) await firstGate;
});
const first = runTick();
await Promise.resolve();
expect(await runTick()).toBe(false);
expect(calls).toBe(1);
releaseFirst();
expect(await first).toBe(true);
expect(await runTick()).toBe(true);
expect(calls).toBe(2);
});
test("releases the guard after a failed tick", async () => {
let calls = 0;
const errors = [];
const runTick = createNonOverlappingTickRunner(async () => {
calls += 1;
if (calls === 1) throw new Error("first failed");
}, (error) => errors.push(error.message));
expect(await runTick()).toBe(false);
expect(await runTick()).toBe(true);
expect(errors).toEqual(["first failed"]);
});
});