-
Notifications
You must be signed in to change notification settings - Fork 2
/
koroutine.test.js
500 lines (415 loc) · 13.1 KB
/
koroutine.test.js
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
'use strict';
const koroutine = require('..');
// koroutine.enableBreadcrumbs(false);
// koroutine.setErrorHandler(() => {});
function asyncCallSuccess (input, callback, delay) {
setTimeout(function () {
callback(null, input, 'arg-1', 'arg-2');
}, delay);
}
function asyncCallError (input, callback, delay) {
setTimeout(function () {
callback(new Error(input));
}, delay);
}
function* sequentialCallSuccess (input, delay) {
return yield asyncCallSuccess(input, koroutine.callback(), delay);
}
function* sequentialCallError (input, delay) {
return yield asyncCallError(input, koroutine.callback(), delay);
}
function* testSequentialCalls (test) {
test.expect(4);
let result = yield* sequentialCallSuccess('input-1', 200);
test.deepEqual(result, ['input-1', 'arg-1', 'arg-2']);
result = yield* sequentialCallSuccess('input-2', 100);
test.deepEqual(result, ['input-2', 'arg-1', 'arg-2']);
try {
yield* sequentialCallError('error-1', 150);
} catch (e) {
test.equal(e.message, 'error-1');
test.equal(e.cause, 'Exception');
}
test.done();
}
function* alwaysThrows () {
throw new Error('ERROR');
}
exports['Test koroutine error handler function'] = function (test) {
koroutine.run(alwaysThrows(), {errorHandler: () => test.done()});
};
exports['Test koroutine.callback() fail when koroutine not started'] = function (test) {
try {
asyncCallSuccess('input-1', koroutine.callback(), 100);
test.equals(1, 2);
} catch (ex) {
test.done();
}
};
function testNotGeneratorFunction () {
return function () {
console.log('####################### SHOULD NEVER GET PRINTED!!! #######################');
};
}
exports['Test koroutine.run() fail without iterator'] = function (test) {
try {
koroutine.run(testNotGeneratorFunction());
test.equals(1, 2);
} catch (ex) {
test.done();
}
};
function* dummy () {
yield true;
}
function* reentrantCoroutine () {
koroutine.run(dummy());
}
exports['Test reentrant koroutine '] = function (test) {
koroutine.setErrorHandler((e) => test.done());
try {
koroutine.run(reentrantCoroutine());
} catch (ex) {
test.done();
}
};
exports['Test sequential async calls'] = function (test) {
koroutine.run(testSequentialCalls(test));
};
function* testParallelCalls (test) {
test.expect(5);
const f1 = koroutine.future();
asyncCallSuccess('input-1', f1, 200);
const f2 = koroutine.future();
asyncCallSuccess('input-2', f2, 300);
const f3 = koroutine.future();
asyncCallError('error-1', f3, 100);
const numErrors = yield* koroutine.join(f1, f2, f3);
test.deepEqual(f1.data, ['input-1', 'arg-1', 'arg-2']);
test.deepEqual(f2.data, ['input-2', 'arg-1', 'arg-2']);
test.equal(f3.error.message, 'error-1');
test.equal(f3.error.cause, 'Exception');
test.equal(numErrors, 1);
test.done();
}
exports['Test parallel async calls'] = function (test) {
koroutine.run(testParallelCalls(test));
};
function* testMixedCalls (test) {
test.expect(8);
const f1 = koroutine.future();
asyncCallSuccess('input-1', f1, 10);
const f2 = koroutine.future();
asyncCallSuccess('input-2', f2, 100);
const f3 = koroutine.future();
asyncCallSuccess('input-3', f3, 200);
const f4 = koroutine.future();
asyncCallSuccess('input-4', f4, 300);
const f5 = koroutine.future();
asyncCallSuccess('input-5', f5, 800);
let numErrors = yield* koroutine.join(f1, f2);
test.equal(numErrors, 0);
test.deepEqual(f1.data, ['input-1', 'arg-1', 'arg-2']);
test.deepEqual(f2.data, ['input-2', 'arg-1', 'arg-2']);
let result = yield* sequentialCallSuccess('input-4', 400);
test.deepEqual(result, ['input-4', 'arg-1', 'arg-2']);
numErrors = yield* koroutine.join(f3, f4, f5);
test.equal(numErrors, 0);
test.deepEqual(f3.data, ['input-3', 'arg-1', 'arg-2']);
test.deepEqual(f4.data, ['input-4', 'arg-1', 'arg-2']);
test.deepEqual(f5.data, ['input-5', 'arg-1', 'arg-2']);
test.done();
}
exports['Test futures mixed with yield'] = function (test) {
koroutine.run(testMixedCalls(test));
};
function* callbackTimeout (test) {
try {
yield asyncCallSuccess('input-1', koroutine.callback(10), 100);
test.equal(1, 2);
} catch (e) {
test.done();
}
}
exports['Test callback timeout'] = function (test) {
koroutine.run(callbackTimeout(test));
};
function* callbackTimeoutsRace (test) {
test.expect(2);
const result1 = yield asyncCallSuccess('input-1', koroutine.callback(50), 10);
test.deepEqual(result1, ['input-1', 'arg-1', 'arg-2']);
const result2 = yield asyncCallSuccess('input-2', koroutine.callback(), 400);
test.deepEqual(result2, ['input-2', 'arg-1', 'arg-2']);
test.done();
}
exports['Test multiple callbacks timeout race condition'] = function (test) {
koroutine.run(callbackTimeoutsRace(test));
};
exports['Test futures mixed with yield'] = function (test) {
koroutine.run(testMixedCalls(test));
};
exports['Test future timeout'] = function (test) {
koroutine.run((function* (test) {
test.expect(4);
const f1 = koroutine.future(100);
asyncCallSuccess('input-1', f1, 8000);
const f2 = koroutine.future(2000);
asyncCallSuccess('input-2', f2, 300);
const f3 = koroutine.future(200);
asyncCallError('error-1', f3, 8000);
const numErrors = yield* koroutine.join(f1, f2, f3);
test.equal(numErrors, 2);
test.equal(f1.error.cause, 'TimedOut');
test.deepEqual(f2.data, ['input-2', 'arg-1', 'arg-2']);
test.equal(f3.error.cause, 'TimedOut');
test.done();
})(test));
};
exports['Test koroutine timeout with future'] = function (test) {
koroutine.run((function* (test) {
test.expect(1);
try {
const ft = koroutine.future(100);
asyncCallSuccess('input-2', ft, 300);
yield* koroutine.join(ft);
test.equals(1, 2);
} catch (e) {
test.equal(e.cause, 'TimedOut');
test.done();
}
})(test), {timeout: 10});
};
function* testSequentialCallTimeout (test) {
test.expect(1);
try {
yield* sequentialCallSuccess('input-1', 2000);
} catch (e) {
test.equal(e.cause, 'TimedOut');
}
test.done();
}
exports['Test sequential call timeout'] = function (test) {
koroutine.run(testSequentialCallTimeout(test), {timeout: 10});
};
function* testParallelCallsTimeout (test) {
test.expect(2);
const f1 = koroutine.future();
asyncCallSuccess('input-1', f1, 200);
const f2 = koroutine.future();
asyncCallSuccess('input-2', f2, 600);
try {
yield* koroutine.join(f1, f2);
} catch (e) {
test.equal(e.cause, 'TimedOut');
test.deepEqual(f1.data, ['input-1', 'arg-1', 'arg-2']);
}
test.done();
}
exports['Test parallel calls timeout'] = function (test) {
koroutine.run(testParallelCallsTimeout(test), {timeout: 400});
};
exports['Test continue after error'] = function (test) {
koroutine.run((function* (test) {
test.expect(3);
try {
yield* sequentialCallError('error-1', 150);
test.equals(1, 2);
} catch (e) {
test.equal(e.message, 'error-1');
test.equal(e.cause, 'Exception');
}
let result = yield* sequentialCallSuccess('input-2', 100);
test.deepEqual(result, ['input-2', 'arg-1', 'arg-2']);
test.done();
})(test));
};
function* testSleep (test) {
test.expect(1);
const start = Date.now();
yield koroutine.sleep(100);
const end = Date.now();
test.ok((end - start) >= 90, (end - start));
test.done();
};
exports['Test sleep'] = function (test) {
koroutine.run(testSleep(test));
};
function* testDefer (test) {
test.expect(3);
let count = 0;
yield koroutine.defer();
test.equal(count, 0);
setImmediate(function () {
count++;
});
setImmediate(function () {
count++;
});
test.equal(count, 0);
yield koroutine.defer();
test.equal(count, 2);
test.done();
}
exports['Test defer'] = function (test) {
koroutine.run(testDefer(test));
};
function testDoneLatch (test, limit) {
let count = 0;
const origDone = test.done;
return function () {
count += 1;
if (count >= limit) {
origDone();
}
};
}
function testExpectAccumulator (test) {
let accCount = 0;
const origExpect = test.expect;
return function (count) {
accCount += count;
origExpect(accCount);
};
}
exports['Test sequential async calls in multiple simultaneous coroutines'] = function (test) {
test.expect = testExpectAccumulator(test);
test.done = testDoneLatch(test, 3);
koroutine.run(testSequentialCalls(test));
koroutine.run(testSequentialCalls(test));
koroutine.run(testSequentialCalls(test));
};
exports['Test parallel async calls in multiple simultaneous coroutines'] = function (test) {
test.expect = testExpectAccumulator(test);
test.done = testDoneLatch(test, 3);
koroutine.run(testParallelCalls(test), {timeout: 8000});
koroutine.run(testParallelCalls(test), {timeout: 8000});
koroutine.run(testParallelCalls(test), {timeout: 8000});
};
exports['Test mixed calls in multiple simultaneous coroutines'] = function (test) {
test.expect = testExpectAccumulator(test);
test.done = testDoneLatch(test, 6);
koroutine.run(testSequentialCalls(test), {timeout: 2000});
koroutine.run(testParallelCalls(test), {timeout: 1000});
koroutine.run(testSequentialCalls(test), {timeout: 2000});
koroutine.run(testParallelCalls(test), {timeout: 1000});
koroutine.run(testParallelCalls(test), {timeout: 1000});
koroutine.run(testSequentialCalls(test), {timeout: 2000});
};
function* testCoroutineCurrentState (test, delay, input) {
test.expect(1);
koroutine.state.my_var = input;
yield koroutine.sleep(delay);
test.equal(koroutine.state.my_var, input);
test.done();
};
exports['Test koroutine current context'] = function (test) {
test.expect = testExpectAccumulator(test);
test.done = testDoneLatch(test, 3);
koroutine.run(testCoroutineCurrentState(test, 100, 'first'));
koroutine.run(testCoroutineCurrentState(test, 200, 'second'));
koroutine.run(testCoroutineCurrentState(test, 150, 'third'));
};
function* testExceptionThrower (test) {
throw new Error('Uncaught Exception!');
}
exports['Test coroutine throwing uncaught exception'] = function (test) {
koroutine.setErrorHandler((e) => {
test.ok(e);
test.done();
});
koroutine.run(testExceptionThrower(test));
};
exports['Test coroutine throwing error with timeout'] = function (test) {
koroutine.setErrorHandler((e) => {
test.ok(e);
test.done();
});
koroutine.run(testExceptionThrower(test), {timeout: 10});
};
exports['Test future() fail without active koroutine'] = function (test) {
try {
koroutine.future();
test.equals(1, 2);
} catch (e) {
test.done();
}
};
exports['Test sleep() fail without active koroutine'] = function (test) {
try {
koroutine.sleep(10);
test.equals(1, 2);
} catch (e) {
test.done();
}
};
exports['Test defer() fail without active koroutine'] = function (test) {
try {
koroutine.defer();
test.equals(1, 2);
} catch (e) {
test.done();
}
};
function* testStackTracing (mesg) {
yield* sequentialCallSuccess('input-1', 100);
yield* sequentialCallSuccess('input-2', 100);
throw new Error(mesg);
}
exports['Test with stack tracing disabled'] = function (test) {
const errHandlerFn = function (err) {
const log = err.stack;
test.expect(5);
test.ok(err.message.startsWith('Unhandled exception in koroutine'));
test.ok(log.includes('next (native)'));
test.equal((log.match(/testStackTracing/g) || []).length, 1);
test.ok(!log.includes('Resumed'));
test.ok(log.split('\n').length > 5);
test.done();
};
koroutine.setErrorHandler(errHandlerFn);
koroutine.run(testStackTracing('test stack tracing disabled'));
};
exports['Test with stack tracing enabled globally'] = function (test) {
const errHandlerFn = function (err) {
const log = err.stack;
// console.log(log);
test.expect(5);
test.ok(err.message.startsWith('Unhandled exception in koroutine'));
test.ok(!log.includes('next (native)'));
test.equal((log.match(/testStackTracing/g) || []).length, 3);
test.ok(log.includes('test-stack-tracing suspended at'));
test.ok(log.split('\n').length > 5);
test.done();
};
koroutine.setErrorHandler(errHandlerFn);
koroutine.enableBreadcrumbs(true);
koroutine.run(testStackTracing('test stack tracing enabled'), {name: 'test-stack-tracing'});
};
exports['Test with stack tracing enabled locally'] = function (test) {
const errHandlerFn = function (err) {
const log = err.stack;
// console.log(log);
test.expect(5);
test.ok(err.message.startsWith('Unhandled exception in koroutine'));
test.ok(!log.includes('next (native)'));
test.equal((log.match(/testStackTracing/g) || []).length, 3);
test.ok(log.includes('test-stack-tracing suspended at'));
test.ok(log.split('\n').length > 5);
test.done();
};
koroutine.setErrorHandler(errHandlerFn);
koroutine.enableBreadcrumbs(false);
koroutine.run(testStackTracing('test stack tracing enabled'), {enableBreadcrumbs: true, name: 'test-stack-tracing'});
};
function* multipleLiveCallback () {
koroutine.callback();
yield* sequentialCallSuccess('input-1', 100);
}
exports['Test multiple live callback fail'] = function (test) {
try {
koroutine.run(multipleLiveCallback(), {errorHandler: (err) => { throw err; }});
test.equals(1, 2);
} catch (e) {
test.done();
}
};