-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtimer-tests.ts
More file actions
271 lines (221 loc) · 6.38 KB
/
timer-tests.ts
File metadata and controls
271 lines (221 loc) · 6.38 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import * as TKUnit from '../tk-unit';
import * as timer from '@nativescript/core/timer';
// >> timer-require
// require("globals");
//// OR
// var timer = require("timer");
// << timer-require
export function test_setTimeout_isDefined() {
TKUnit.assertNotEqual(timer.setTimeout, undefined, 'Method timer.setTimeout() should be defined!');
}
export function test_clearTimeout_isDefined() {
TKUnit.assertNotEqual(timer.clearTimeout, undefined, 'Method timer.clearTimeout() should be defined!');
}
export function test_setInsDefined() {
TKUnit.assertNotEqual(timer.setInterval, undefined, 'Method timer.setInterval() should be defined!');
}
export function test_clear_isDefined() {
TKUnit.assertNotEqual(timer.clearInterval, undefined, 'Method timer.clearInterval() should be defined!');
}
export function test_setTimeout() {
let completed: boolean;
// >> timer-set-zero
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
});
// << timer-set-zero
TKUnit.waitUntilReady(() => completed, 0.5, false);
timer.clearTimeout(id);
TKUnit.assert(completed, 'Callback should be called!');
}
export function test_setTimeout_extraArgs() {
let completed: boolean;
let rnd: number = Math.random();
// >> timer-set-zero-args
const id = timer.setTimeout(
(arg) => {
// >> (hide)
completed = rnd === arg;
// << (hide)
},
0,
rnd
);
// << timer-set-zero-args
TKUnit.waitUntilReady(() => completed, 0.5, false);
timer.clearTimeout(id);
TKUnit.assert(completed, 'Callback called with expected argument!');
}
export function test_setTimeout_callbackCalledAfterSpecifiedTime() {
let completed = false;
// >> timer-set-ten
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
}, 10);
// << timer-set-ten
TKUnit.waitUntilReady(() => completed, 1);
timer.clearTimeout(id);
TKUnit.assert(completed, 'Callback should be called after the specified time!');
}
export function test_setTimeout_callbackCalledWithBooleanPeriod() {
let completed = false;
// >> timer-set-false
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
// @ts-ignore
}, false);
// << timer-set-false
TKUnit.waitUntilReady(() => completed, 1);
timer.clearTimeout(id);
TKUnit.assert(completed, 'Callback should be called in 0 seconds!');
}
export function test_setTimeout_callbackNotCalled() {
let completed = false;
const id = timer.setTimeout(() => (completed = true), 10);
timer.clearTimeout(id);
TKUnit.wait(30 / 1000);
TKUnit.assert(!completed, 'Callback should not be called after the specified time!');
}
export function test_setTimeout_shouldReturnNumber() {
let id = timer.setTimeout(() => {
//
});
timer.clearTimeout(id);
TKUnit.assertTrue(typeof id === 'number', 'Callback should return number!');
}
export function test_setTimeout_callbackShouldBeCleared() {
let completed = false;
// >> timer-set-fifty
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
}, 50);
//// Clear timeout with specified id.
timer.clearTimeout(id);
// << timer-set-fifty
TKUnit.wait(0.06);
timer.clearTimeout(id);
TKUnit.assert(!completed, 'Callback should be cleared when clearTimeout() is executed for specified id!');
}
export function test_setInterval_callbackCalledDuringPeriod(done) {
let counter = 0;
const expected = 4;
const timeLimit = 300;
const start = TKUnit.time();
// >> timer-set-expression
const id = timer.setInterval(() => {
// >> (hide)
counter++;
if (counter === expected) {
const end = TKUnit.time();
timer.clearInterval(id);
const time = end - start;
done(time > timeLimit ? new Error(`setInterval too slow. Actual time: ${time} timelimit: ${timeLimit}`) : null);
}
// << (hide)
}, 50);
// << timer-set-expression
}
export function test_setInterval_callbackCalledWithExtraArgs(done) {
let counter: number = 0;
const rnd: number = Math.random();
const threshold = 100;
const start = TKUnit.time();
const id = timer.setInterval(
(arg) => {
counter += arg === rnd ? 1 : -1;
if (counter === 4) {
const end = TKUnit.time();
timer.clearInterval(id);
done(end - start > 1000 + threshold ? new Error('setInterval too slow.') : null);
}
},
250,
rnd
);
}
export function test_setInterval_callbackNotDelayedByBusyWork() {
let calls = 0;
let firstCall = true;
const id = timer.setInterval(() => {
calls++;
if (firstCall) {
firstCall = false;
TKUnit.wait(0.125);
}
}, 250);
TKUnit.wait(0.55);
timer.clearInterval(id);
TKUnit.assertEqual(calls, 2, 'Callback should be called multiple times with busy wait');
}
export function test_setInterval_callbackSkippedByBusyWork() {
let calls = 0;
let firstCall = true;
const id = timer.setInterval(() => {
calls++;
if (firstCall) {
firstCall = false;
TKUnit.wait(0.051);
}
}, 50);
TKUnit.wait(0.16);
timer.clearInterval(id);
TKUnit.assertEqual(calls, 2, 'Callback should be called skipped when it takes too long to process');
}
export function test_setInterval_callbackShouldBeCleared(done) {
const start = TKUnit.time();
// >> timer-set-interval
const id = timer.setInterval(() => {
// >> (hide)
const end = TKUnit.time();
timer.clearInterval(id);
done(end - start > 150 ? new Error('setInterval too slow.') : null);
// << (hide)
timer.clearInterval(id);
}, 50);
// << timer-set-interval
}
export function test_clearTimeout_multipleTimes_afterTick() {
let completed = false;
const id = timer.setTimeout(() => {
completed = true;
});
TKUnit.waitUntilReady(() => completed, 0.5);
TKUnit.assert(completed, 'Callback should be called');
timer.clearTimeout(id);
timer.clearTimeout(id);
}
export function test_clearTimeout_immediatelyAfterCreate() {
let completed = false;
const id = timer.setTimeout(() => {
completed = true;
});
timer.clearTimeout(id);
TKUnit.wait(0.02);
TKUnit.assert(!completed, 'Callback should not be called');
}
export function test_clearInterval_immediatelyAfterCreate() {
let completed = false;
const id = timer.setInterval(() => {
completed = true;
});
timer.clearInterval(id);
TKUnit.wait(0.05);
TKUnit.assert(!completed, 'Callback should not be called');
}
export function test_clearTimeout_insideCallback() {
let completed = false;
let id = timer.setTimeout(() => {
completed = true;
timer.clearTimeout(id);
});
TKUnit.waitUntilReady(() => completed, 0.5);
TKUnit.assert(completed, 'Callback should be called');
}