-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathtimers_test.ts
110 lines (92 loc) · 2.45 KB
/
timers_test.ts
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, fail } from "@std/assert";
import * as timers from "node:timers";
import * as timersPromises from "node:timers/promises";
Deno.test("[node/timers setTimeout]", () => {
{
const { clearTimeout, setTimeout } = timers;
const id = setTimeout(() => {});
clearTimeout(id);
}
{
const id = timers.setTimeout(() => {});
timers.clearTimeout(id);
}
});
Deno.test("[node/timers setInterval]", () => {
{
const { clearInterval, setInterval } = timers;
const id = setInterval(() => {});
clearInterval(id);
}
{
const id = timers.setInterval(() => {});
timers.clearInterval(id);
}
});
Deno.test("[node/timers setImmediate]", async () => {
{
const { clearImmediate, setImmediate } = timers;
const imm = setImmediate(() => {});
clearImmediate(imm);
}
{
const imm = timers.setImmediate(() => {});
timers.clearImmediate(imm);
}
{
const deffered = Promise.withResolvers<void>();
const imm = timers.setImmediate(
(a, b) => {
assert(a === 1);
assert(b === 2);
deffered.resolve();
},
1,
2,
);
await deffered;
timers.clearImmediate(imm);
}
});
Deno.test("[node/timers/promises setTimeout]", () => {
const { setTimeout } = timersPromises;
const p = setTimeout(0);
assert(p instanceof Promise);
return p;
});
Deno.test("[node/timers/promises scheduler.wait]", async () => {
const { scheduler } = timersPromises;
let resolved = false;
timers.setTimeout(() => (resolved = true), 20);
const p = scheduler.wait(20);
assert(p instanceof Promise);
await p;
assert(resolved);
});
Deno.test("[node/timers/promises scheduler.yield]", async () => {
const { scheduler } = timersPromises;
let resolved = false;
timers.setImmediate(() => resolved = true);
const p = scheduler.yield();
assert(p instanceof Promise);
await p;
assert(resolved);
});
// Regression test for https://github.com/denoland/deno/issues/17981
Deno.test("[node/timers refresh cancelled timer]", () => {
const { setTimeout, clearTimeout } = timers;
const p = setTimeout(() => {
fail();
}, 1);
clearTimeout(p);
p.refresh();
});
Deno.test("[node/timers setImmediate returns Immediate object]", () => {
const { clearImmediate, setImmediate } = timers;
const imm = setImmediate(() => {});
imm.unref();
imm.ref();
imm.hasRef();
clearImmediate(imm);
});