-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_clock_test.js
More file actions
272 lines (215 loc) · 8.11 KB
/
_clock_test.js
File metadata and controls
272 lines (215 loc) · 8.11 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
272
// Copyright Titanium I.T. LLC.
import assert from "util/assert.js";
import { Clock } from "infrastructure/clock.js";
const UNRESOLVABLE_PROMISE = new Promise(() => {});
describe("Clock", () => {
describe("waiting", () => {
it("waits N milliseconds", async () => {
const clock = Clock.create();
const start = clock.now();
await clock.waitAsync(10);
const elapsedTime = clock.now() - start;
assert.isAtLeast(elapsedTime, 9);
});
});
describe("current time", () => {
it("provides current timestamp", () => {
const clock = Clock.create();
let expected = Date.now();
const actual = clock.now();
if (actual !== expected) expected = Date.now();
assert.equal(actual, expected);
});
it("outputs current time using computer's language and time zone", () => {
const format = {
dateStyle: "medium",
timeStyle: "short",
timeZone: "local",
};
const formatCopy = { ...format };
const jsFormat = {
dateStyle: "medium",
timeStyle: "short",
};
checkToFormattedString(format, jsFormat, "local", undefined);
assert.deepEqual(format, formatCopy, "should not modify original format object");
});
it("outputs current time using configured time zone and locale", () => {
const format = {
timeZone: "Europe/Paris",
dateStyle: "medium",
timeStyle: "short",
};
const locale = "fr";
checkToFormattedString(format, format, locale, locale);
});
it("fails fast if time zone isn't specified", () => {
assert.throws(
() => Clock.create().toFormattedString({}, "en-US"),
"Must specify options.timeZone (use 'local' for computer's time zone)"
);
});
it("fails fast if locale isn't specified", () => {
assert.throws(
() => Clock.create().toFormattedString({ timeZone: "UTC" }),
"Must specify locale (use 'local' for computer's default locale)"
);
});
function checkToFormattedString(ourFormat, jsFormat, ourLocale, jsLocale) {
const clock = Clock.create();
let expected = new Date().toLocaleString(jsLocale, jsFormat);
const actual = clock.toFormattedString(ourFormat, ourLocale);
if (expected !== actual) expected = new Date().toLocaleString(jsLocale, jsFormat);
assert.equal(actual, expected);
}
});
describe("timeouts", () => {
it("resolves if promise resolves before timeout", async () => {
const { resultPromise, msElapsed, timeoutRan } = await timeoutAsync({
promise: Promise.resolve("my promise")
});
assert.equal(msElapsed, 0, "should resolve immediately");
assert.equal(timeoutRan, false, "should not run timeout function");
assert.equal(await resultPromise, "my promise", "should return resolved promise");
});
it("rejects if promise rejects before timeout", async () => {
const { resultPromise, msElapsed, timeoutRan } = await timeoutAsync({
promise: Promise.reject(new Error("my error"))
});
assert.equal(msElapsed, 0, "should resolve immediately");
assert.equal(timeoutRan, false, "should not run timeout function");
await assert.throwsAsync(() => resultPromise, "my error", "should return rejected promise");
});
it("resolves via timeout function if promise times out", async () => {
const { resultPromise, msElapsed, timeoutRan } = await timeoutAsync({
timeoutInMs: 10000,
promise: UNRESOLVABLE_PROMISE,
timeoutResult: Promise.resolve("timeout result"),
});
assert.equal(msElapsed, 10000, "should wait for timeout");
assert.equal(timeoutRan, true, "should run timeout function");
assert.equal(await resultPromise, "timeout result", "should return result of timeout function");
});
it("rejects via timeout function if promise times out and timeout rejects", async () => {
const { resultPromise, msElapsed, timeoutRan } = await timeoutAsync({
timeoutInMs: 10000,
promise: UNRESOLVABLE_PROMISE,
timeoutResult: Promise.reject(new Error("my error")),
});
assert.equal(msElapsed, 10000, "should wait for timeout");
assert.equal(timeoutRan, true, "should run timeout function");
await assert.throwsAsync(() => resultPromise, "my error", "should reject because timeout function rejected");
});
it("ignores promise resolution after timeout", async () => {
const clock = Clock.createNull();
const { resultPromise } = await timeoutAsync({
clock,
timeoutInMs: 10000,
promise: new Promise(async (resolve) => {
await clock.waitAsync(20000);
resolve("this result should be ignored");
}),
timeoutResult: Promise.resolve("timeout result"),
});
assert.equal(await resultPromise, "timeout result");
});
it("ignores promise rejection after timeout", async () => {
const clock = Clock.createNull();
const { resultPromise } = await timeoutAsync({
clock,
timeoutInMs: 10000,
promise: new Promise(async (resolve, reject) => {
await clock.waitAsync(20000);
reject(new Error("this error should be ignored"));
}),
timeoutResult: Promise.resolve("timeout result"),
});
assert.equal(await resultPromise, "timeout result");
});
});
describe("nullability", () => {
it("defaults 'now' to zero", () => {
const clock = Clock.createNull();
assert.equal(clock.now(), 0);
});
it("allows 'now' to be configured", () => {
const clock = Clock.createNull({ now: 42 });
assert.equal(clock.now(), 42);
});
it("renders to formatted string", () => {
const clock = Clock.createNull({ now: 0 });
const format = {
timeZone: "Europe/Paris",
dateStyle: "medium",
timeStyle: "short",
};
assert.equal(clock.toFormattedString(format, "fr"), "1 janv. 1970, 01:00");
});
it("defaults time zone and locale to little-used values to prevent false successes", () => {
const clock = Clock.createNull({ now: 0 });
const format = { dateStyle: "medium", timeStyle: "long", timeZone: "local" };
const formatCopy = { ...format };
assert.equal(clock.toFormattedString(format, "local"), "1970 J-guer 1 10:00:00 GMT+10");
assert.deepEqual(format, formatCopy, "should not modify original format object");
});
it("allows local time zone and locale to be configured", () => {
const clock = Clock.createNull({ now: 0, timeZone: "America/New_York", locale: "uk" });
const format = { dateStyle: "medium", timeStyle: "long", timeZone: "local" };
assert.equal(clock.toFormattedString(format, "local"), "31 груд. 1969 р., 19:00:00 GMT-5");
});
it("can advance the clock", async () => {
const clock = Clock.createNull();
await clock.advanceNulledClockAsync(10);
assert.equal(clock.now(), 10);
});
it("can advance the clock until all timers expire", async () => {
const clock = Clock.createNull();
clock.waitAsync(999);
await clock.advanceNulledClockUntilTimersExpireAsync();
assert.equal(clock.now(), 999);
});
it("fails fast when attempting to advance the system clock", async () => {
const clock = Clock.create();
await assert.throwsAsync(
() => clock.advanceNulledClockAsync(10),
"Can't advance the clock because it isn't a nulled clock"
);
await assert.throwsAsync(
() => clock.advanceNulledClockUntilTimersExpireAsync(),
"Can't advance the clock because it isn't a nulled clock"
);
});
it("can wait", async () => {
const clock = Clock.createNull();
let wait = "waiting";
clock.waitAsync(10).then(() => {
wait = clock.now();
});
assert.equal(wait, "waiting");
await clock.advanceNulledClockAsync(20);
assert.equal(wait, 10);
});
it("can timeout", async () => {
const clock = Clock.createNull();
await clock.timeoutAsync(10, Promise.resolve(), () => {});
});
});
});
async function timeoutAsync({
clock = Clock.createNull({ now: 0 }),
timeoutInMs = 42,
promise = Promise.resolve("irrelevant promise"),
timeoutResult = Promise.resolve("irrelevant timeout result"),
} = {}) {
let timeoutRan = false;
const timeoutFn = () => {
timeoutRan = true;
return timeoutResult;
};
const resultPromise = clock.timeoutAsync(timeoutInMs, promise, timeoutFn);
// prevent 'unhandled rejection' errors
resultPromise.catch(() => {});
timeoutResult.catch(() => {});
await clock.advanceNulledClockUntilTimersExpireAsync();
return { resultPromise, timeoutRan, msElapsed: clock.now() };
}