-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.spec.ts
29 lines (28 loc) · 1018 Bytes
/
index.spec.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
import test from 'tape';
import { ReentrantLock } from '.';
test('should lock block code execution', async (t) => {
let counter = 0;
let executionsCount = 0;
let check = false;
const concurrencyProblem = async () => {
const _counter = counter;
t.false(check);
check = true;
await new Promise(r => setTimeout(r, 0));
t.true(check);
check = false;
counter = _counter + 1;
};
const concurrencyLock = new ReentrantLock();
let promises: Promise<any>[] = [];
for (let i = 0; i < 5; i++)
promises.push(concurrencyLock.lock(() => concurrencyProblem()).then(() => executionsCount++));
await Promise.all(promises);
promises = [];
for (let i = 0; i < 5; i++)
promises.push(concurrencyLock.lock(() => concurrencyProblem()).then(() => executionsCount++));
await Promise.all(promises);
t.equal(executionsCount, 10, 'All executed');
t.equal(counter, 10, 'Counter incremented correctly');
t.end();
});