-
Notifications
You must be signed in to change notification settings - Fork 3
/
reentrant_guard.test.js
626 lines (625 loc) · 24.4 KB
/
reentrant_guard.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
'use strict';
const assert = require('assert');
const reentrantGuard = require('../../src/reentrant_guard.js');
const { TestUtil } = require('./test_util.js');
describe('reentrantGuard', () => {
describe('makeGuardedCommand', () => {
const makeGuardedCommand = reentrantGuard.makeGuardedCommand;
const logs = [];
let oldPrintError;
beforeEach(() => {
logs.length = 0;
oldPrintError = reentrantGuard.setPrintError(error => {
logs.push('error: ' + error);
});
});
afterEach(() => {
reentrantGuard.setPrintError(oldPrintError);
});
it('should return an async function', async () => {
const func = makeGuardedCommand(() => {});
assert.strictEqual(typeof func, 'function');
assert.strictEqual(func.constructor.name, 'AsyncFunction');
});
it('should return a function that invokes the given function', async () => {
const target = function() {
logs.push('hello');
};
const func = makeGuardedCommand(target);
assert.deepStrictEqual(logs, []);
await func();
assert.deepStrictEqual(logs, [ 'hello' ]);
await func();
assert.deepStrictEqual(logs, [ 'hello', 'hello' ]);
});
it('should return a function that passes arguments to the given function', async () => {
const target = function(arg) {
logs.push('hello ' + arg);
};
const func = makeGuardedCommand(target);
await func('world');
assert.deepStrictEqual(logs, [ 'hello world' ]);
});
it('should return a function that invokes the given async function', async () => {
const asyncTarget = async function() {
logs.push('hello');
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeGuardedCommand(asyncTarget);
logs.push('before call');
await func();
logs.push('after call');
assert.deepStrictEqual(logs, [
'before call',
'hello',
'bye',
'after call'
]);
});
it('should prevent the function from reentry', async () => {
const asyncTarget = async function() {
logs.push('hello');
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeGuardedCommand(asyncTarget);
logs.push('before concurrent call');
const promise1 = func();
const promise2 = func();
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
await func();
logs.push('after third call');
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello',
'bye',
'after concurrent call',
'hello',
'bye',
'after third call'
]);
});
it('should prevent other functions from calling concurrently', async () => {
const asyncTarget1 = async function() {
logs.push('hello 1');
await TestUtil.sleep(10);
logs.push('bye 1');
};
const asyncTarget2 = async function() {
logs.push('hello 2');
await TestUtil.sleep(10);
logs.push('bye 2');
};
const func1 = makeGuardedCommand(asyncTarget1);
const func2 = makeGuardedCommand(asyncTarget2);
logs.push('before concurrent call');
const promise1 = func1();
const promise2 = func2();
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
await func2();
logs.push('after third call');
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello 1',
'bye 1',
'after concurrent call',
'hello 2',
'bye 2',
'after third call'
]);
});
it('should prevent the function from leaking exceptions', async () => {
const target = function(arg) {
if (arg === 0) {
logs.push('will throw');
throw 'error';
} else {
logs.push('will not throw');
}
};
const func = makeGuardedCommand(target);
logs.push('before call');
await func(0);
await func(1);
logs.push('after call');
assert.deepStrictEqual(logs, [
'before call',
'will throw',
'error: error',
'will not throw',
'after call'
]);
});
});
describe('makeGuardedCommandSync', () => {
const makeGuardedCommandSync = reentrantGuard.makeGuardedCommandSync;
const logs = [];
let oldPrintError;
beforeEach(() => {
logs.length = 0;
oldPrintError = reentrantGuard.setPrintError(error => {
logs.push('error: ' + error);
});
});
afterEach(() => {
reentrantGuard.setPrintError(oldPrintError);
});
it('should return a non-async function', async () => {
const func = makeGuardedCommandSync(() => {});
assert.strictEqual(typeof func, 'function');
assert.strictEqual(func.constructor.name, 'Function');
});
it('should return a function that invokes the given function', async () => {
const target = function() {
logs.push('hello');
};
const func = makeGuardedCommandSync(target);
assert.deepStrictEqual(logs, []);
func();
assert.deepStrictEqual(logs, [ 'hello' ]);
func();
assert.deepStrictEqual(logs, [ 'hello', 'hello' ]);
});
it('should return a function that passes arguments to the given function', async () => {
const target = function(arg) {
logs.push('hello ' + arg);
};
const func = makeGuardedCommandSync(target);
func('world');
assert.deepStrictEqual(logs, [ 'hello world' ]);
});
it('should prevent the function from interrupting other guarded functions', async () => {
const asyncTarget = async function() {
logs.push('hello async');
await TestUtil.sleep(10);
logs.push('bye async');
};
const syncTarget = async function() {
logs.push('hello sync');
};
const func1 = reentrantGuard.makeGuardedCommand(asyncTarget);
const func2 = makeGuardedCommandSync(syncTarget);
logs.push('before concurrent call');
const promise1 = func1();
func2();
await promise1;
logs.push('after concurrent call');
func2();
logs.push('after third call');
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello async',
'bye async',
'after concurrent call',
'hello sync',
'after third call'
]);
});
it('should prevent the function from leaking exceptions', async () => {
const target = function(arg) {
if (arg === 0) {
logs.push('will throw');
throw 'error';
} else {
logs.push('will not throw');
}
};
const func = makeGuardedCommandSync(target);
logs.push('before call');
func(0);
func(1);
logs.push('after call');
assert.deepStrictEqual(logs, [
'before call',
'will throw',
'error: error',
'will not throw',
'after call'
]);
});
});
describe('makeQueueableCommand', () => {
const makeQueueableCommand = reentrantGuard.makeQueueableCommand;
const logs = [];
beforeEach(() => {
logs.length = 0;
});
it('should return an async function', async () => {
const func = makeQueueableCommand(() => {});
assert.strictEqual(typeof func, 'function');
assert.strictEqual(func.constructor.name, 'AsyncFunction');
});
it('should return a function that invokes the given function', async () => {
const target = function() {
logs.push('hello');
};
const func = makeQueueableCommand(target);
assert.deepStrictEqual(logs, []);
await func();
assert.deepStrictEqual(logs, [ 'hello' ]);
await func();
assert.deepStrictEqual(logs, [ 'hello', 'hello' ]);
});
it('should return a function that passes arguments to the given function', async () => {
const target = function(arg) {
logs.push('hello ' + arg);
};
const func = makeQueueableCommand(target);
await func('world');
assert.deepStrictEqual(logs, [ 'hello world' ]);
});
it('should return a function that invokes the given async function', async () => {
const asyncTarget = async function() {
logs.push('hello');
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeQueueableCommand(asyncTarget);
logs.push('before call');
await func();
logs.push('after call');
assert.deepStrictEqual(logs, [
'before call',
'hello',
'bye',
'after call'
]);
});
it('should enqueue and serialize invocation of the function', async () => {
const asyncTarget = async function(arg) {
logs.push('hello ' + arg);
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeQueueableCommand(asyncTarget);
logs.push('before concurrent call');
const promise1 = func('1');
const promise2 = func('2');
assert.strictEqual(reentrantGuard.getQueueLength(), 1);
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
await func('3');
logs.push('after third call');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello 1',
'bye',
'hello 2',
'bye',
'after concurrent call',
'hello 3',
'bye',
'after third call'
]);
});
it('should not enqueue over queueSize if specified', async () => {
const asyncTarget = async function(arg) {
logs.push('hello ' + arg);
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeQueueableCommand(asyncTarget, { queueSize: 3 });
logs.push('before concurrent call');
const promise1 = func('1');
const promise2 = func('2');
const promise3 = func('3');
const promise4 = func('4');
const promise5 = func('5');
assert.strictEqual(reentrantGuard.getQueueLength(), 3);
await Promise.all([promise1, promise2, promise3, promise4, promise5]);
logs.push('after concurrent call');
await func('6');
logs.push('after last call');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello 1',
'bye',
'hello 2',
'bye',
'hello 3',
'bye',
'hello 4',
'bye',
'after concurrent call',
'hello 6',
'bye',
'after last call'
]);
});
it('should prevent queueable function from interrupting guarded functions', async () => {
const asyncTarget = async function(arg) {
logs.push('hello ' + arg);
await TestUtil.sleep(10);
logs.push('bye');
};
const guarded = reentrantGuard.makeGuardedCommand(asyncTarget);
const queueable = makeQueueableCommand(asyncTarget);
logs.push('before concurrent call');
const promise1 = guarded('guarded');
const promise2 = queueable('queueable');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello guarded',
'bye',
'after concurrent call'
]);
});
it('should prevent guarded function from interrupting queueable functions', async () => {
const asyncTarget = async function(arg) {
logs.push('hello ' + arg);
await TestUtil.sleep(10);
logs.push('bye');
};
const guarded = reentrantGuard.makeGuardedCommand(asyncTarget);
const queueable = makeQueueableCommand(asyncTarget);
logs.push('before concurrent call');
const promise1 = queueable('queueable');
const promise2 = guarded('guarded');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello queueable',
'bye',
'after concurrent call'
]);
});
it('should prevent the function from leaking exceptions', async () => {
const old = reentrantGuard.setPrintError(error => {
logs.push('error: ' + error);
});
try {
const willThrow = makeQueueableCommand(function() {
logs.push('will throw');
throw 'error';
});
const wontThrow = makeQueueableCommand(function() {
logs.push('will not throw');
});
logs.push('before call');
await willThrow();
await wontThrow();
logs.push('after call');
assert.deepStrictEqual(logs, [
'before call',
'will throw',
'error: error',
'will not throw',
'after call'
]);
} finally {
reentrantGuard.setPrintError(old);
}
});
it('should invoke queued function even if another queued function threw exception (1)', async () => {
const old = reentrantGuard.setPrintError(error => {
logs.push('error: ' + error);
});
try {
const willThrow = makeQueueableCommand(async function() {
logs.push('will throw...');
await TestUtil.sleep(10);
logs.push('will throw now');
throw 'error';
});
const wontThrow = makeQueueableCommand(async function() {
logs.push('will not throw');
});
logs.push('before concurrent call');
const promise1 = willThrow();
const promise2 = wontThrow();
await Promise.all([promise1, promise2]);
logs.push('after concurrent call');
assert.deepStrictEqual(logs, [
'before concurrent call',
'will throw...',
'will throw now',
'error: error',
'will not throw',
'after concurrent call'
]);
} finally {
reentrantGuard.setPrintError(old);
}
});
it('should invoke queued function even if another queued function threw exception (2)', async () => {
const old = reentrantGuard.setPrintError(error => {
logs.push('error: ' + error);
});
try {
const wontThrow1 = makeQueueableCommand(async function() {
logs.push('will not throw (1) begin');
await TestUtil.sleep(10);
logs.push('will not throw (1) end');
});
const willThrow = makeQueueableCommand(async function() {
logs.push('will throw...');
await TestUtil.sleep(10);
logs.push('will throw now');
throw 'error';
});
const wontThrow2 = makeQueueableCommand(async function() {
logs.push('will not throw (2)');
});
logs.push('before concurrent call');
const promise1 = wontThrow1();
const promise2 = willThrow();
await promise1;
const promise3 = wontThrow2();
await Promise.all([promise2, promise3]);
logs.push('after concurrent call');
assert.deepStrictEqual(logs, [
'before concurrent call',
'will not throw (1) begin',
'will not throw (1) end',
'will throw...',
'will throw now',
'error: error',
'will not throw (2)',
'after concurrent call'
]);
} finally {
reentrantGuard.setPrintError(old);
}
});
it('should finish each call when it ends its actual execution', async () => {
const asyncTarget = async function(arg) {
await TestUtil.sleep(10);
logs.push('hello ' + arg);
await TestUtil.sleep(10);
logs.push('bye');
};
const func = makeQueueableCommand(asyncTarget);
logs.push('before concurrent call');
const promise1 = func('1');
const promise2 = func('2');
const promise3 = func('3');
assert.strictEqual(reentrantGuard.getQueueLength(), 2);
await promise1;
logs.push('after await 1');
await promise2;
logs.push('after await 2');
await promise3;
logs.push('after await 3');
assert.strictEqual(reentrantGuard.getQueueLength(), 0);
assert.deepStrictEqual(logs, [
'before concurrent call',
'hello 1',
'bye',
'after await 1',
'hello 2',
'bye',
'after await 2',
'hello 3',
'bye',
'after await 3'
]);
});
});
describe('callExclusively', () => {
const logs = [];
beforeEach(() => {
logs.length = 0;
});
it('should call given function', async () => {
await reentrantGuard.callExclusively(
() => {
logs.push('exclusive');
}
);
assert.deepStrictEqual(logs, ['exclusive']);
});
it('should not await end of calling given async function', async () => {
let done = null;
const cleanup = new Promise(resolve => { done = resolve; });
await reentrantGuard.callExclusively(
async () => {
logs.push('hello');
await TestUtil.sleep(20);
logs.push('bye');
done();
}
);
assert.deepStrictEqual(logs, ['hello']);
await cleanup;
});
it('should defer calling given function until ongoing guarded command ends', async () => {
const command = reentrantGuard.makeGuardedCommand(
async () => {
logs.push('hello');
await TestUtil.sleep(20);
logs.push('bye');
}
);
const promise1 = command();
const promise2 = reentrantGuard.callExclusively(
() => {
logs.push('exclusive');
}
);
await Promise.all([promise1, promise2]);
assert.deepStrictEqual(logs, ['hello', 'bye', 'exclusive']);
});
it('should defer calling given multiple functions until ongoing guarded command ends', async () => {
const command = reentrantGuard.makeGuardedCommand(
async () => {
logs.push('hello');
await TestUtil.sleep(10);
logs.push('bye');
}
);
const promise1 = command();
const target = async () => {
logs.push('exclusive');
};
const promise2 = reentrantGuard.callExclusively(target);
const promise3 = reentrantGuard.callExclusively(target);
await Promise.all([promise1, promise2, promise3]);
assert.deepStrictEqual(logs, ['hello', 'bye', 'exclusive', 'exclusive']);
});
it('should not block guarded command execution by calling async function', async () => {
let done = null;
const cleanup = new Promise(resolve => { done = resolve; });
const target = async () => {
logs.push('exclusive');
await TestUtil.sleep(10);
logs.push('end');
done();
};
const promise1 = reentrantGuard.callExclusively(target);
const command = reentrantGuard.makeGuardedCommand(
async () => {
logs.push('hello');
}
);
const promise2 = command();
await Promise.all([promise1, promise2, cleanup]);
assert.deepStrictEqual(logs, ['exclusive', 'hello', 'end']);
});
it('should call given function after ongoing non-async guarded command', async () => {
const command = reentrantGuard.makeGuardedCommandSync(
() => {
logs.push('hello');
reentrantGuard.callExclusively(
() => {
logs.push('exclusive');
}
);
logs.push('bye');
}
);
command();
assert.deepStrictEqual(logs, ['hello', 'bye', 'exclusive']);
});
it('should defer calling given function until ongoing queueable guarded command ends', async () => {
const command = reentrantGuard.makeQueueableCommand(
async () => {
logs.push('hello');
await TestUtil.sleep(10);
logs.push('bye');
}
);
const promise1 = command();
const promise2 = command();
const promise3 = reentrantGuard.callExclusively(
() => {
logs.push('exclusive');
}
);
await Promise.all([promise1, promise2, promise3]);
assert.deepStrictEqual(logs, ['hello', 'bye', 'hello', 'bye', 'exclusive']);
});
});
});