-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_test.ts
468 lines (390 loc) · 11.9 KB
/
async_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import {
assert,
assertEquals,
assertThrowsAsync,
} from "https://deno.land/std/testing/asserts.ts";
import {
spy,
Spy,
} from "https://deno.land/x/[email protected]/mod.ts";
import * as scientist from "./mod.ts";
const sleep = (ms: number): Promise<void> =>
new Promise((resolve: () => void) => setTimeout(resolve, ms));
async function sum(a: number, b: number): Promise<number> {
await sleep(250);
return a + b;
}
async function sum2(a: number, b: number): Promise<number> {
await sleep(125);
return b + a;
}
Deno.test("when async functions are equivalent it should await result", async () => {
const experiment = scientist.experimentAsync({
name: "async equivalent1",
control: sum,
candidate: sum2,
options: {
publish: () => {},
},
});
const result: number = await experiment(1, 2);
assertEquals(result, 3);
});
Deno.test("when async functions are equivalent it should publish results", async () => {
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async equivalent2",
control: sum,
candidate: sum2,
options: {
publish: publishMock,
},
});
await experiment(1, 2);
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assertEquals(results.experimentName, "async equivalent2");
assertEquals(results.experimentArguments, [1, 2]);
assertEquals(results.controlResult, 3);
assertEquals(results.candidateResult, 3);
assertEquals(results.controlError, undefined);
assertEquals(results.candidateError, undefined);
assert(results.controlTimeMs !== undefined);
assert(results.controlTimeMs > 0);
assert(results.candidateTimeMs !== undefined);
assert(results.candidateTimeMs > 0);
});
async function ctrl(s: string): Promise<string> {
await sleep(250);
return `Ctrl+${s}`;
}
async function candi(s: string): Promise<string> {
await sleep(125);
return s;
}
Deno.test("when async function results differ it should await result of control", async () => {
const experiment = scientist.experimentAsync({
name: "async differ1",
control: ctrl,
candidate: candi,
options: {
publish: () => {},
},
});
const result: string = await experiment("C");
assertEquals(result, "Ctrl+C");
});
Deno.test("when async function results differ it should publish results", async () => {
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async differ2",
control: ctrl,
candidate: candi,
options: {
publish: publishMock,
},
});
await experiment("C");
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assertEquals(results.experimentName, "async differ2");
assertEquals(results.experimentArguments, ["C"]);
assertEquals(results.controlResult, "Ctrl+C");
assertEquals(results.candidateResult, "C");
assertEquals(results.controlError, undefined);
assertEquals(results.candidateError, undefined);
assert(results.controlTimeMs !== undefined);
assert(results.controlTimeMs > 0);
assert(results.candidateTimeMs !== undefined);
assert(results.candidateTimeMs > 0);
});
async function ctrlSimple(): Promise<string> {
await sleep(125);
return "Everything is under control";
}
async function candiReject(): Promise<string> {
return Promise.reject(new Error("Candy I can't let you go"));
}
Deno.test("when async candidate rejects it should await result of control", async () => {
const experiment = scientist.experimentAsync({
name: "async throw1",
control: ctrlSimple,
candidate: candiReject,
options: {
publish: () => {},
},
});
const result: string = await experiment();
assertEquals(result, "Everything is under control");
});
Deno.test("when async candidate rejects it should publish results", async () => {
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async throw2",
control: ctrlSimple,
candidate: candiReject,
options: {
publish: publishMock,
},
});
await experiment();
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assertEquals(results.experimentName, "async throw2");
assertEquals(results.experimentArguments, []);
assertEquals(results.controlResult, "Everything is under control");
assertEquals(results.candidateResult, undefined);
assertEquals(results.controlError, undefined);
assert(results.candidateError !== undefined);
assertEquals(results.candidateError.message, "Candy I can't let you go");
assert(results.controlTimeMs !== undefined);
assert(results.controlTimeMs > 0);
assertEquals(results.candidateTimeMs, undefined);
});
async function ctrlThrower(): Promise<string> {
throw new Error("Kaos!");
}
async function candiSimple(): Promise<string> {
await sleep(125);
return "Kane";
}
Deno.test("when async control rejects it should reject", async () => {
const experiment = scientist.experimentAsync({
name: "async cthrow1",
control: ctrlThrower,
candidate: candiSimple,
options: {
publish: () => {},
},
});
await assertThrowsAsync(
async (): Promise<void> => {
await experiment();
},
Error,
"Kaos!",
);
});
Deno.test("when async control rejects it should publish results", async () => {
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async cthrow2",
control: ctrlThrower,
candidate: candiSimple,
options: {
publish: publishMock,
},
});
try {
await experiment();
} catch {
// swallow error
}
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assertEquals(results.experimentName, "async cthrow2");
assertEquals(results.experimentArguments, []);
assertEquals(results.controlResult, undefined);
assertEquals(results.candidateResult, "Kane");
assert(results.controlError !== undefined);
assertEquals(results.controlError.message, "Kaos!");
assertEquals(results.candidateError, undefined);
assertEquals(results.controlTimeMs, undefined);
assert(results.candidateTimeMs !== undefined);
assert(results.candidateTimeMs > 0);
});
Deno.test("when async both reject it should reject with control error", async () => {
const experiment = scientist.experimentAsync({
name: "async bothrow1",
control: ctrlThrower,
candidate: candiReject,
options: {
publish: () => {},
},
});
await assertThrowsAsync(
async (): Promise<void> => {
await experiment();
},
Error,
"Kaos!",
);
});
Deno.test("when async both reject it should publish results", async () => {
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async bothrow2",
control: ctrlThrower,
candidate: candiReject,
options: {
publish: publishMock,
},
});
try {
await experiment();
} catch {
// swallow error
}
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assertEquals(results.experimentName, "async bothrow2");
assertEquals(results.experimentArguments, []);
assertEquals(results.controlResult, undefined);
assertEquals(results.candidateResult, undefined);
assert(results.controlError !== undefined);
assertEquals(results.controlError.message, "Kaos!");
assert(results.candidateError !== undefined);
assertEquals(results.candidateError.message, "Candy I can't let you go");
assertEquals(results.controlTimeMs, undefined);
assertEquals(results.candidateTimeMs, undefined);
});
function enabledFalse(): boolean {
return false;
}
Deno.test("when async and enabled returns false and control resolves it should not run candidate", async () => {
const candidateMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async disabled1",
control: ctrl,
candidate: candidateMock,
options: {
publish: () => {},
enabled: enabledFalse,
},
});
await experiment("C");
assertEquals(candidateMock.calls.length, 0);
});
Deno.test("when async and enabled returns false and control resolves it should await result of control", async () => {
const candidateMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async disabled2",
control: ctrl,
candidate: candidateMock,
options: {
publish: () => {},
enabled: enabledFalse,
},
});
const result: string = await experiment("C");
assertEquals(result, "Ctrl+C");
});
Deno.test("when async and enabled returns false and control resolves it should not publish results", async () => {
const candidateMock: Spy<void> = spy();
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async disabled3",
control: ctrl,
candidate: candidateMock,
options: {
publish: publishMock,
enabled: enabledFalse,
},
});
await experiment("C");
assertEquals(publishMock.calls.length, 0);
});
Deno.test("when async and enabled returns false and control rejects it should reject", async () => {
const candidateMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async cthrow1",
control: ctrlThrower,
candidate: candidateMock,
options: {
publish: () => {},
enabled: enabledFalse,
},
});
await assertThrowsAsync(
async (): Promise<void> => {
await experiment();
},
Error,
"Kaos!",
);
});
Deno.test("when async and enabled returns false and control rejects it should not run candidate", async () => {
const candidateMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async disabledthrow2",
control: ctrlThrower,
candidate: candidateMock,
options: {
publish: () => {},
enabled: enabledFalse,
},
});
try {
await experiment();
} catch {
// swallow error
}
assertEquals(candidateMock.calls.length, 0);
});
Deno.test("when async and enabled returns false and control rejects it should not publish results", async () => {
const candidateMock: Spy<void> = spy();
const publishMock: Spy<void> = spy();
const experiment = scientist.experimentAsync({
name: "async disabledthrow3",
control: ctrlThrower,
candidate: candidateMock,
options: {
publish: publishMock,
enabled: enabledFalse,
},
});
try {
await experiment();
} catch {
// swallow error
}
assertEquals(publishMock.calls.length, 0);
});
const msPerFunction = 1000;
async function ctrlSlow(): Promise<string> {
await sleep(msPerFunction);
return "Control";
}
async function candiSlow(): Promise<string> {
await sleep(msPerFunction);
return "Candidate";
}
Deno.test("when async functions are slow it should run functions in parallel", async () => {
const allowedOverhead = 125;
const experiment = scientist.experimentAsync({
name: "async parallel1",
control: ctrlSlow,
candidate: candiSlow,
options: {
publish: () => {},
},
});
const start = performance.now();
await experiment();
const end = performance.now();
const elapsedMs = end - start;
assert(elapsedMs < msPerFunction + allowedOverhead);
});
Deno.test("when async functions are slow it should publish individual timings", async () => {
const publishMock: Spy<void> = spy();
const allowedVarianceMs = 125;
const minMs = msPerFunction - allowedVarianceMs;
const maxMs = msPerFunction + allowedVarianceMs;
const experiment = scientist.experimentAsync({
name: "async parallel2",
control: ctrlSlow,
candidate: candiSlow,
options: {
publish: publishMock,
},
});
await experiment();
assertEquals(publishMock.calls.length, 1);
const results = publishMock.calls[0].args[0];
assert(results.controlTimeMs !== undefined);
assert(results.controlTimeMs > minMs);
assert(results.controlTimeMs < maxMs);
assert(results.candidateTimeMs !== undefined);
assert(results.candidateTimeMs > minMs);
assert(results.candidateTimeMs < maxMs);
});