-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
47 lines (35 loc) · 973 Bytes
/
index.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
const Mutex = require('./index');
let mutex = null;
beforeEach(() => {
mutex = new Mutex();
});
test('one caller', done => {
const lock = mutex.acquire().then(lock => {
lock.release();
done();
});
});
test('two callers', done => {
const resolveHandler = jest.fn(lock => lock.release());
mutex.acquire().then(resolveHandler);
mutex.acquire().then(lock => {
expect(resolveHandler.mock.calls.length).toBe(1);
lock.release();
done();
});
});
test('two callers, first one does not release the lock', done => {
mutex.acquire();
mutex.acquire({ timeout: 100 }).then(() => {}, reject => {
expect(reject).toBe('Queue timeout');
done();
});
});
test('one caller, internal timer expires after the lock has given to it', done => {
const resolveHandler = jest.fn();
mutex.acquire({ timeout: 100 }).then(resolveHandler);
setTimeout(() => {
expect(resolveHandler.mock.calls.length).toBe(1);
done();
}, 200);
});