-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.test.js
70 lines (57 loc) · 1.61 KB
/
lock.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
"use strict"
const assert = require('chai').assert
const async = require('async')
const lock = require('./lock.js')
describe('lock', function() {
function asyncPush(val, timeout) {
return new Promise((resolve, reject) => {
setTimeout(() => {
map.push(val)
resolve()
}, timeout)
})
}
let map
beforeEach(function() {
map = []
})
it('sync', function() {
lock(next => map.push(1) && next())
lock(next => map.push(2) && next())
lock(next => map.push(3) && next())
lock(next => map.push(4) && next())
assert.deepEqual(map, [1, 2, 3, 4])
})
it('async', function(done) {
lock(next => asyncPush(1, 10).then(next))
lock(next => asyncPush(2, 5).then(next))
lock(next => asyncPush(3, 20).then(next))
lock(next => asyncPush(4, 15).then(next))
setTimeout(() => {
assert.deepEqual(map, [1, 2, 3, 4])
done()
}, 100)
})
it('parallel', function(done) {
async.parallel([
() => lock(next => asyncPush(1, 10).then(next)),
() => lock(next => asyncPush(2, 5).then(next)),
() => lock(next => asyncPush(3, 20).then(next)),
() => lock(next => asyncPush(4, 15).then(next)),
])
setTimeout(() => {
assert.deepEqual(map, [1, 2, 3, 4])
done()
}, 100)
})
it('namespace', function(done) {
lock('ns1', next => asyncPush(1, 10).then(next))
lock('ns2', next => asyncPush(2, 5).then(next))
lock('ns2', next => asyncPush(3, 20).then(next))
lock('ns2', next => asyncPush(4, 15).then(next))
setTimeout(() => {
assert.deepEqual(map, [2, 1, 3, 4])
done()
}, 100)
})
})