-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.test.ts
More file actions
162 lines (120 loc) · 4.27 KB
/
timer.test.ts
File metadata and controls
162 lines (120 loc) · 4.27 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { assertEquals, assertLess } from "@std/assert";
import { assertSpyCalls, spy } from "@std/testing/mock";
import { createInterval, createTimeout } from "./timer.ts";
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// --- createTimeout Tests ---
Deno.test("createTimeout() - executes callback after delay (number)", async () => {
const startedAt = Date.now();
const spyCallback = spy(() => {
// Check if it ran approximately after 100ms (allow 20ms buffer)
assertLess(Math.abs(Date.now() - startedAt - 100), 20);
});
const timer = createTimeout(spyCallback, 100);
assertEquals(timer.state(), "waiting");
assertSpyCalls(spyCallback, 0);
await sleep(150);
assertEquals(timer.state(), "completed");
assertSpyCalls(spyCallback, 1);
});
Deno.test("createTimeout() - executes callback after delay (Date)", async () => {
const startedAt = Date.now();
const spyCallback = spy(() => {
assertLess(Math.abs(Date.now() - startedAt - 100), 20);
});
const timer = createTimeout(spyCallback, new Date(Date.now() + 100));
assertEquals(timer.state(), "waiting");
assertSpyCalls(spyCallback, 0);
await sleep(150);
assertEquals(timer.state(), "completed");
assertSpyCalls(spyCallback, 1);
});
Deno.test("createTimeout() - can be paused and restarted", async () => {
const spyCallback = spy(() => {});
// Start with 100ms delay, but pause immediately
const timer = createTimeout(spyCallback, 100).pause();
assertEquals(timer.state(), "paused");
// Wait longer than the delay
await sleep(150);
assertSpyCalls(spyCallback, 0);
// Restart
timer.resume();
assertEquals(timer.state(), "waiting");
// Wait for the remaining time
await sleep(150);
assertEquals(timer.state(), "completed");
assertSpyCalls(spyCallback, 1);
});
Deno.test("createTimeout() - can be cleared", async () => {
const spyCallback = spy(() => {});
const timer = createTimeout(spyCallback, 100);
// Clear immediately
timer.clear();
assertEquals(timer.state(), "completed");
// Wait longer than the delay
await sleep(150);
// Should never be called
assertSpyCalls(spyCallback, 0);
});
// --- createInterval Tests ---
Deno.test("createInterval() - executes callback repeatedly", async () => {
const spyCallback = spy(() => {});
// Run every 50ms
const timer = createInterval(spyCallback, 50);
// Wait 225ms (approx 4 calls: 50, 100, 150, 200)
await sleep(225);
timer.clear();
// Check call count (expecting at least 4)
const calls = spyCallback.calls.length;
assertEquals(calls >= 4, true, `Expected >= 4 calls, got ${calls}`);
assertEquals(timer.state(), "completed");
});
Deno.test("createInterval() - executes with initial delay", async () => {
const spyCallback = spy(() => {});
// Delay 100ms, then run every 50ms
const timer = createInterval(spyCallback, 50, 100);
// Wait 80ms (still in delay)
await sleep(80);
assertSpyCalls(spyCallback, 0);
// Wait another 80ms (total 160ms -> delay passed, 1st interval passed)
await sleep(80);
const calls = spyCallback.calls.length;
assertEquals(calls >= 1, true, `Expected >= 1 calls, got ${calls}`);
timer.clear();
});
Deno.test("createInterval() - can be paused and restarted", async () => {
const spyCallback = spy(() => {});
const timer = createInterval(spyCallback, 50);
// Allow 2 calls (~100ms)
await sleep(120);
const callsBeforePause = spyCallback.calls.length;
assertEquals(callsBeforePause >= 2, true);
// Pause
timer.pause();
assertEquals(timer.state(), "paused");
// Wait 200ms (should be no new calls)
await sleep(200);
assertSpyCalls(spyCallback, callsBeforePause);
// Restart
timer.resume();
// Wait another 100ms (expect more calls)
await sleep(120);
const callsAfterRestart = spyCallback.calls.length;
assertEquals(callsAfterRestart > callsBeforePause, true);
timer.clear();
});
Deno.test("createInterval() - can be cleared", async () => {
const spyCallback = spy(() => {});
const timer = createInterval(spyCallback, 50);
// Wait for 1 call
await sleep(60);
assertSpyCalls(spyCallback, 1);
// Clear
timer.clear();
assertEquals(timer.state(), "completed");
// Wait long enough for next interval
await sleep(100);
// Should still be 1 call
assertSpyCalls(spyCallback, 1);
});