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
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assertEqual } from "./test_util.ts";
function deferred() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject
};
}
function waitForMs(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
test(async function timeoutSuccess() {
const { promise, resolve } = deferred();
let count = 0;
setTimeout(() => {
count++;
resolve();
}, 500);
await promise;
// count should increment
assertEqual(count, 1);
});
test(async function timeoutArgs() {
const { promise, resolve } = deferred();
const arg = 1;
setTimeout(
(a, b, c) => {
assertEqual(a, arg);
assertEqual(b, arg.toString());
assertEqual(c, [arg]);
resolve();
},
10,
arg,
arg.toString(),
[arg]
);
await promise;
});
test(async function timeoutCancelSuccess() {
let count = 0;
const id = setTimeout(() => {
count++;
}, 500);
// Cancelled, count should not increment
clearTimeout(id);
// Wait a bit longer than 500ms
await waitForMs(600);
assertEqual(count, 0);
});
test(async function timeoutCancelMultiple() {
// Set timers and cancel them in the same order.
const t1 = setTimeout(uncalled, 10);
const t2 = setTimeout(uncalled, 10);
const t3 = setTimeout(uncalled, 10);
clearTimeout(t1);
clearTimeout(t2);
clearTimeout(t3);
// Set timers and cancel them in reverse order.
const t4 = setTimeout(uncalled, 20);
const t5 = setTimeout(uncalled, 20);
const t6 = setTimeout(uncalled, 20);
clearTimeout(t6);
clearTimeout(t5);
clearTimeout(t4);
// Sleep until we're certain that the cancelled timers aren't gonna fire.
await waitForMs(50);
function uncalled() {
throw new Error("This function should not be called.");
}
});
test(async function timeoutCancelInvalidSilentFail() {
// Expect no panic
const { promise, resolve } = deferred();
let count = 0;
const id = setTimeout(() => {
count++;
// Should have no effect
clearTimeout(id);
resolve();
}, 500);
await promise;
assertEqual(count, 1);
// Should silently fail (no panic)
clearTimeout(2147483647);
});
test(async function intervalSuccess() {
const { promise, resolve } = deferred();
let count = 0;
const id = setInterval(() => {
count++;
if (count === 2) {
// TODO: clearInterval(id) here alone seems not working
// causing unit_tests.ts to block forever
// Requires further investigation...
clearInterval(id);
resolve();
}
}, 200);
await promise;
// Clear interval
clearInterval(id);
// count should increment twice
assertEqual(count, 2);
});
test(async function intervalCancelSuccess() {
let count = 0;
const id = setInterval(() => {
count++;
}, 500);
// Cancelled, count should not increment
clearInterval(id);
// Wait a bit longer than 500ms
await waitForMs(600);
assertEqual(count, 0);
});
test(async function intervalOrdering() {
const timers = [];
let timeouts = 0;
for (let i = 0; i < 10; i++) {
timers[i] = setTimeout(onTimeout, 20);
}
function onTimeout() {
++timeouts;
for (let i = 1; i < timers.length; i++) {
clearTimeout(timers[i]);
}
}
await waitForMs(100);
assertEqual(timeouts, 1);
});
test(async function intervalCancelInvalidSilentFail() {
// Should silently fail (no panic)
clearInterval(2147483647);
});
|