-
Notifications
You must be signed in to change notification settings - Fork 13
/
block.test.js
69 lines (53 loc) · 1.47 KB
/
block.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
/* eslint-env node, jest */
const Blocker = require('./block.js');
let blocker = null;
beforeEach(() => {
blocker = new Blocker();
});
it('blocks until unblocked', () => new Promise(async (resolve) => {
const unblock = await blocker.block('block1');
let blocked = true;
blocker.wait(() => {
expect(blocked).toBe(false);
resolve();
});
process.nextTick(() => {
blocked = false;
unblock();
});
}));
it('blocks until all unblocked', () => new Promise(async (resolve) => {
const unblocks = [];
for (let i = 0; i < 10; i++) {
unblocks.push(await blocker.block(`block${i}`));
}
let unblocked = 0;
blocker.wait(() => {
expect(unblocked).toBe(10);
resolve();
});
while (unblocks.length > 0) {
unblocked++;
unblocks.shift()();
await new Promise((resolve) => process.nextTick(resolve));
}
}));
it('calls intervalCallback when block continues', () => new Promise(async (resolve, reject) => {
await blocker.block('block');
blocker.wait(reject, 0, resolve);
}));
it('does not call intervalCallback unless blocked', () => new Promise(async (resolve, reject) => {
const unblock = await blocker.block('block');
unblock();
blocker.wait(() => {
process.nextTick(resolve);
}, 0, reject);
}));
it('cannot block while wait callback is runnning', () => new Promise(async (resolve, reject) => {
blocker.wait(() => new Promise((callbackResolve) => process.nextTick(() => {
resolve();
callbackResolve();
})));
await blocker.block('block');
reject(new Error());
}));