forked from taylorhakes/localstorage-lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·60 lines (50 loc) · 1.21 KB
/
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
const painless = require('painless');
const createGroup = painless.createGroup;
const localstorageLock = require('./lib');
const runWithLock = localstorageLock.runWithLock;
require('mock-local-storage');
const assert = painless.assert;
const test = createGroup();
test('basic no contention', (done) => {
runWithLock('test', done);
});
test('2 in a row no contention', (done) => {
runWithLock('test', () => {
setTimeout(() => {
runWithLock('test', done);
}, 0)
});
});
test('2 at the same time', (done) => {
let ran = false;
runWithLock('test', () => {
ran = true;
runWithLock('test', () => {
assert(ran);
done();
});
});
});
test('3 at the same time', (done) => {
let done2 = null;
let done3 = null;
runWithLock('test', () => {
let done1 = Date.now();
runWithLock('test', () => {
done2 = Date.now();
if (done1 && done2 && done3) {
assert.isBelow(done1, done2);
assert.isBelow(done1, done3)
done();
}
});
runWithLock('test', () => {
done3 = Date.now();
if (done1 && done2 && done3) {
assert.isBelow(done1, done2);
assert.isBelow(done1, done3);
done();
}
});
});
});