This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_test.ts
301 lines (279 loc) · 8.75 KB
/
time_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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import {
assert,
assertEquals,
assertNotEquals,
assertStrictEquals,
} from "./deps/std/testing/asserts.ts";
import {
NativeDate,
FakeDate,
FakeTime,
delay,
} from "./time.ts";
import { spy, Spy, SpyCall } from "./spy.ts";
import { assertPassthrough } from "./asserts.ts";
import { fromNow } from "./callbacks.ts";
Deno.test("FakeDate parse and UTC behave the same", () => {
assertEquals(
FakeDate.UTC(96, 1, 2, 3, 4, 5),
NativeDate.UTC(96, 1, 2, 3, 4, 5),
);
assertEquals(
FakeDate.parse("04 Dec 1995 00:12:00 GMT"),
NativeDate.parse("04 Dec 1995 00:12:00 GMT"),
);
});
Deno.test("Date unchanged if FakeTime is uninitialized", () => {
FakeTime.restore();
assertNotEquals(Date, FakeDate);
assertStrictEquals(Date, NativeDate);
});
Deno.test("Date is FakeDate if FakeTime is initialized", () => {
const time: FakeTime = new FakeTime(9001);
try {
assertNotEquals(Date, NativeDate);
assertStrictEquals(Date, FakeDate);
} finally {
time.restore();
}
assertNotEquals(Date, FakeDate);
assertStrictEquals(Date, NativeDate);
});
Deno.test("FakeDate.now uses Date.now if FakeTime is uninitialized", () => {
FakeTime.restore();
const now: Spy<DateConstructor> = spy(NativeDate, "now");
try {
const result = FakeDate.now();
assertEquals(now.calls.length, 1);
assertEquals(now.calls[0].self, NativeDate);
assertEquals(now.calls[0].args, []);
assertEquals(result, now.calls[0].returned);
} finally {
now.restore();
}
});
Deno.test("FakeDate.now returns current fake time if FakeTime initialized", () => {
const time: FakeTime = new FakeTime(9001);
const now: Spy<DateConstructor> = spy(NativeDate, "now");
try {
assertEquals(FakeDate.now(), 9001);
assertEquals(now.calls.length, 0);
time.tick(1523);
assertEquals(FakeDate.now(), 10524);
assertEquals(now.calls.length, 0);
} finally {
now.restore();
time.restore();
}
});
Deno.test("FakeDate instance methods passthrough to Date instance methods", () => {
const now: FakeDate = new FakeDate("2020-05-25T05:00:00.12345Z");
assertEquals(now.toISOString(), "2020-05-25T05:00:00.123Z");
Object.getOwnPropertyNames(Date.prototype).forEach((name: string) => {
const propName: keyof Date = name as keyof Date;
assertPassthrough({
instance: now,
method: propName,
target: {
instance: now.date,
self: now.date,
},
});
});
assertPassthrough({
instance: now,
method: Symbol.toPrimitive,
target: {
instance: now.date,
self: now.date,
},
});
});
Deno.test("timeout functions unchanged if FakeTime is uninitialized", () => {
FakeTime.restore();
assertNotEquals(setTimeout, FakeTime.setTimeout);
assertNotEquals(clearTimeout, FakeTime.clearTimeout);
});
Deno.test("timeout functions are fake if FakeTime is initialized", () => {
const time: FakeTime = new FakeTime();
try {
assertStrictEquals(setTimeout, FakeTime.setTimeout);
assertStrictEquals(clearTimeout, FakeTime.clearTimeout);
} finally {
time.restore();
}
assertNotEquals(setTimeout, FakeTime.setTimeout);
assertNotEquals(clearTimeout, FakeTime.clearTimeout);
});
Deno.test("FakeTime only ticks forward when setting now or calling tick", async () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
try {
assertEquals(Date.now(), start);
time.tick(5);
assertEquals(Date.now(), start + 5);
time.now = start + 1000;
assertEquals(Date.now(), start + 1000);
assert(NativeDate.now() < start + 1000);
} finally {
time.restore();
}
});
Deno.test("FakeTime controls timeouts", () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
const cb: Spy<void> = spy(fromNow());
const expected: SpyCall[] = [];
try {
setTimeout(cb, 1000);
time.tick(250);
assertEquals(cb.calls, expected);
time.tick(250);
assertEquals(cb.calls, expected);
time.tick(500);
expected.push({ args: [], returned: 1000 });
assertEquals(cb.calls, expected);
time.tick(2500);
assertEquals(cb.calls, expected);
setTimeout(cb, 1000, "a");
setTimeout(cb, 2000, "b");
setTimeout(cb, 1500, "c");
assertEquals(cb.calls, expected);
time.tick(2500);
expected.push({ args: ["a"], returned: 4500 });
expected.push({ args: ["c"], returned: 5000 });
expected.push({ args: ["b"], returned: 5500 });
assertEquals(cb.calls, expected);
setTimeout(cb, 1000, "a");
setTimeout(cb, 1500, "b");
const timeout: number = setTimeout(cb, 1750, "c");
setTimeout(cb, 2000, "d");
time.tick(1250);
expected.push({ args: ["a"], returned: 7000 });
assertEquals(cb.calls, expected);
assertEquals(Date.now(), start + 7250);
clearTimeout(timeout);
time.tick(500);
expected.push({ args: ["b"], returned: 7500 });
assertEquals(cb.calls, expected);
assertEquals(Date.now(), start + 7750);
time.tick(250);
expected.push({ args: ["d"], returned: 8000 });
assertEquals(cb.calls, expected);
} finally {
time.restore();
}
});
Deno.test("interval functions unchanged if FakeTime is uninitialized", () => {
FakeTime.restore();
assertNotEquals(setInterval, FakeTime.setInterval);
assertNotEquals(clearInterval, FakeTime.clearInterval);
});
Deno.test("interval functions are fake if FakeTime is initialized", () => {
const time: FakeTime = new FakeTime();
try {
assertStrictEquals(setInterval, FakeTime.setInterval);
assertStrictEquals(clearInterval, FakeTime.clearInterval);
} finally {
time.restore();
}
assertNotEquals(setInterval, FakeTime.setInterval);
assertNotEquals(clearInterval, FakeTime.clearInterval);
});
Deno.test("FakeTime controls intervals", () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
const cb: Spy<void> = spy(fromNow());
const expected: SpyCall[] = [];
try {
const interval: number = setInterval(cb, 1000);
time.tick(250);
assertEquals(cb.calls, expected);
time.tick(250);
assertEquals(cb.calls, expected);
time.tick(500);
expected.push({ args: [], returned: 1000 });
assertEquals(cb.calls, expected);
time.tick(2500);
expected.push({ args: [], returned: 2000 });
expected.push({ args: [], returned: 3000 });
assertEquals(cb.calls, expected);
} finally {
time.restore();
}
});
Deno.test("FakeTime calls timeout and interval callbacks in correct order", () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
const cb: Spy<void> = spy(fromNow());
const timeoutCb: Spy<void> = spy(cb);
const intervalCb: Spy<void> = spy(cb);
const expected: SpyCall[] = [];
const timeoutExpected: SpyCall[] = [];
const intervalExpected: SpyCall[] = [];
try {
const interval: number = setInterval(intervalCb, 1000);
setTimeout(timeoutCb, 500);
time.tick(250);
assertEquals(intervalCb.calls, intervalExpected);
time.tick(250);
setTimeout(timeoutCb, 1000);
let expect: SpyCall = { args: [], returned: 500 };
expected.push(expect);
timeoutExpected.push(expect);
assertEquals(cb.calls, expected);
assertEquals(timeoutCb.calls, timeoutExpected);
assertEquals(cb.calls, expected);
assertEquals(intervalCb.calls, intervalExpected);
time.tick(500);
expect = { args: [], returned: 1000 };
expected.push(expect);
intervalExpected.push(expect);
assertEquals(cb.calls, expected);
assertEquals(intervalCb.calls, intervalExpected);
time.tick(2500);
expect = { args: [], returned: 1500 };
expected.push(expect);
timeoutExpected.push(expect);
expect = { args: [], returned: 2000 };
expected.push(expect);
intervalExpected.push(expect);
expect = { args: [], returned: 3000 };
expected.push(expect);
intervalExpected.push(expect);
assertEquals(cb.calls, expected);
assertEquals(timeoutCb.calls, timeoutExpected);
assertEquals(intervalCb.calls, intervalExpected);
} finally {
time.restore();
}
});
Deno.test("FakeTime restoreFor restores real time temporarily", async () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
try {
assertEquals(Date.now(), start);
time.tick(1000);
assertEquals(Date.now(), start + 1000);
assert(NativeDate.now() < start + 1000);
await FakeTime.restoreFor(async () => {
assert(Date.now() < start + 1000);
});
assertEquals(Date.now(), start + 1000);
assert(NativeDate.now() < start + 1000);
} finally {
time.restore();
}
});
Deno.test("delay uses real time", async () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
try {
assertEquals(Date.now(), start);
await delay(20);
assert(NativeDate.now() >= start + 20);
assertEquals(Date.now(), start);
} finally {
time.restore();
}
});