-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.test.js
162 lines (144 loc) · 3.95 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import test from 'tape'
import advisoryLock, { strToKey } from '../src'
import { conString, timeout } from './common'
test('strToKey', (t) => {
const key = strToKey('test-lock')
t.deepEqual(key, [-107789403, -1898803861], 'generates 2 integer key from string')
t.end()
})
test('lock/unlock on same connection', (t) => {
t.plan(3)
const getMutex = advisoryLock(conString)
const {
lock,
unlock,
} = getMutex('test-lock')
let i = 0
const testLockUnlock = (iVal) => lock()
.then(() => {
t.equal(i, iVal, 'i is equal to 0')
i++
// wait 300ms before decrementing i
return timeout(300)
})
.then(() => i--)
.then(unlock)
.catch(t.fail)
testLockUnlock(0)
testLockUnlock(1)
testLockUnlock(2)
// we can acquire lock both times because we're using the same connection
})
test('lock/unlock on different connections', (t) => {
t.plan(5)
let i = 0
const testLockUnlock = ({ lock, unlock }) => lock()
.then(() => {
t.equal(i, 0, 'i is equal to 0')
i++
// wait 300ms before decrementing i
return timeout(300)
})
.then(() => i--)
.then(unlock)
.catch(t.fail)
testLockUnlock(advisoryLock(conString)('test-lock'))
// blocks... because we're using different connections
// advisoryLock(conString) creates a new connection
testLockUnlock(advisoryLock(conString)('test-lock'))
testLockUnlock(advisoryLock(conString)('test-lock'))
testLockUnlock(advisoryLock(conString)('test-lock'))
testLockUnlock(advisoryLock(conString)('test-lock'))
})
test('tryLock', (t) => {
const mutex1 = advisoryLock(conString)('test-try-lock')
const mutex2 = advisoryLock(conString)('test-try-lock')
mutex1.tryLock()
.then((obtained) => {
t.equal(obtained, true)
})
.then(() => mutex2.tryLock())
.then((obtained) => {
t.equal(obtained, false)
})
.then(() => mutex1.unlock())
.then(() => mutex2.tryLock())
.then((obtained) => {
t.equal(obtained, true)
})
.then(() => mutex1.tryLock())
.then((obtained) => {
t.equal(obtained, false)
})
.then(() => mutex2.unlock())
.then(() => t.end())
.catch(t.fail)
})
test('withLock followed by tryLock', (t) => {
const mutex1 = advisoryLock(conString)('test-withlock-lock')
const mutex2 = advisoryLock(conString)('test-withlock-lock')
mutex1
.withLock(() => (
mutex2
.tryLock()
.then((obtained) => t.equal(obtained, false))
.then(() => 'someval')
))
.then((res) => t.equal(res, 'someval'))
.then(() => mutex2.tryLock())
.then((obtained) => t.equal(obtained, true))
.then(() => mutex2.unlock())
.then(() => t.end())
.catch(t.fail)
})
test('withLock - no promise', (t) => {
const mutex1 = advisoryLock(conString)('test-withlock-lock')
mutex1
.withLock(() => ('someval'))
.then((res) => t.equal(res, 'someval'))
.then(() => t.end())
.catch(t.fail)
})
test('withLock blocks until lock available', (t) => {
const mutex1 = advisoryLock(conString)('test-withlock-lock')
const mutex2 = advisoryLock(conString)('test-withlock-lock')
const logs = []
const maybeDone = () => {
if (logs.length !== 4) return
const version1 = [
'mutex1 enters',
'mutex1 leaves',
'mutex2 enters',
'mutex2 leaves',
]
const version2 = [
'mutex2 enters',
'mutex2 leaves',
'mutex1 enters',
'mutex1 leaves',
]
if (logs[0] === version1[0]) {
t.deepEqual(logs, version1)
} else {
t.deepEqual(logs, version2)
}
t.end()
}
mutex1
.withLock(() => {
logs.push('mutex1 enters')
return timeout(300)
.then(() => logs.push('mutex1 leaves'))
})
.then(maybeDone)
.catch(t.fail)
mutex2
.withLock(() => {
logs.push('mutex2 enters')
return timeout(300)
.then(() => logs.push('mutex2 leaves'))
})
.then(maybeDone)
.catch(t.fail)
})
// TODO: test thowing inside critical section unlocks mutex