-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLockSpec.js
More file actions
266 lines (232 loc) · 6.45 KB
/
Copy pathLockSpec.js
File metadata and controls
266 lines (232 loc) · 6.45 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import Lock from './Lock'
import TimeoutError from './TimeoutError'
describe('Lock', () => {
/**
* @type {Lock}
*/
let lock
beforeEach(() => {
lock = new Lock()
})
it('should generate its name automatically if none is provided', () => {
expect(/Lock:[0-9a-z]+:[0-9a-z]+/.test(lock.name)).toBeTruthy()
})
it('should accept its name as the first constructor argument', () => {
let name = 'generated lock name ' + Date.now() + Math.random()
lock = new Lock(name)
expect(lock.name).toBe(name)
})
it('should reject a non-string name', () => {
let invalidNames = [null, 1, true, {}, []]
for (let invalidName of invalidNames) {
expect(() => new Lock(invalidName)).toThrowError(TypeError)
}
})
it('should reject an empty string as a name', () => {
expect(() => new Lock('')).toThrowError(Error)
})
it('should seal its instance', () => {
expect(() => lock.test = 1).toThrow()
})
it('should not be locked when created', () => {
expect(lock.isLocked).toBe(false)
})
it('should reject non-function tasks', async (done) => {
let invalidTasks = [null, 1, '', true, {}, []]
for (let invalidTask of invalidTasks) {
try {
await lock.lock(invalidTask)
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
}
done()
})
it('should reject non-integer or negative timeouts', async (done) => {
let invalidTimeouts = [null, true, '', 1.2, {}, []]
for (let invalidTimeout of invalidTimeouts) {
try {
await lock.lock(() => {}, invalidTimeout)
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
}
try {
await lock.lock(() => {}, -1)
fail()
} catch (error) {
expect(error instanceof RangeError).toBe(true)
}
done()
})
it('should be locked only during task execution', async (done) => {
expect(lock.isLocked).toBe(false)
let executed = false
await lock.lock(() => {
expect(lock.isLocked).toBe(true)
executed = true
})
expect(executed).toBe(true)
done()
})
it('should accept 0 as timeout', async (done) => {
// It is pretty much impossible to test whether the API would wait
// indefinitely, so we have to settle for accepting 0 at all.
let executed = false
await lock.lock(() => {
executed = true
}, 0)
expect(executed).toBe(true)
done()
})
it('should reject after waiting longer than timeout', async (done) => {
let firstTaskPromise = lock.lock(
() => new Promise(resolve => setTimeout(resolve, 100))
)
try {
await lock.lock(() => {}, 10)
fail()
} catch (error) {
expect(error instanceof TimeoutError).toBe(true)
}
expect(lock.isLocked).toBe(true)
await firstTaskPromise
done()
})
it('should execute the task once', async (done) => {
let executionCounter = 0
await lock.lock(() => executionCounter++)
expect(executionCounter).toBe(1)
done()
})
it('should preserve the order of the tasks', async (done) => {
let executedTasks = []
lock.lock(() => {
return new Promise((resolve) => {
setTimeout(() => {
executedTasks.push('a')
resolve()
}, 10)
})
})
lock.lock(() => {
return new Promise((resolve) => {
setTimeout(() => {
executedTasks.push('b')
resolve()
}, 20)
})
})
await lock.lock(() => {
return new Promise((resolve) => {
setTimeout(() => {
executedTasks.push('c')
resolve()
}, 5)
})
})
expect(executedTasks).toEqual(['a', 'b', 'c'])
done()
})
it('should release the lock if the task fails with error', async (done) => {
try {
await lock.lock(() => {
expect(lock.isLocked).toBe(true)
throw new EvalError('Testing')
})
} catch (error) {
expect(error instanceof EvalError).toBe(true)
expect(lock.isLocked).toBe(false)
}
done()
})
describe('all', () => {
it('should reject non-array locks sequence', async (done) => {
let locks = {
0: lock,
length: 1
}
try {
await Lock.all(locks, () => {})
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
done()
})
it('should reject locks array containing a non-lock', async (done) => {
let locks = [lock, {}]
try {
await Lock.all(locks, () => {})
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
done()
})
it('should reject a task that is not a function', async (done) => {
try {
await Lock.all([lock], [])
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
done()
})
it('should reject non-integer or negative timeouts', async (done) => {
let locks = [lock]
let invalidTimeouts = [null, true, '', 1.2, {}, []]
for (let invalidTimeout of invalidTimeouts) {
try {
await Lock.all(locks, () => {}, invalidTimeout)
fail()
} catch (error) {
expect(error instanceof TypeError).toBe(true)
}
}
try {
await Lock.all(locks, () => {}, -1)
fail()
} catch (error) {
expect(error instanceof RangeError).toBe(true)
}
done()
})
it('should reject empty locks array', async (done) => {
try {
await Lock.all([], () => {})
fail()
} catch (error) {
expect(error instanceof RangeError).toBe(true)
}
done()
})
it('should reject locks with non-unique names', async (done) => {
let locks = [new Lock('a'), new Lock('a')]
try {
await Lock.all(locks, () => {})
fail()
} catch (error) {
expect(error instanceof Error).toBe(true)
}
done()
})
it('should acquire all locks before executing the task', async (done) => {
let locks = [new Lock(), new Lock(), new Lock()]
let executed = false
await Lock.all(locks, () => {
executed = true
expect(locks.every(lock => lock.isLocked)).toBe(true)
})
expect(executed).toBe(true)
expect(locks.every(lock => !lock.isLocked)).toBe(true)
done()
})
})
afterEach(() => {
// this also checks that timed out tasks wont block the lock
expect(lock.isLocked).toBe(false)
})
})