forked from mike-marcacci/node-redlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
458 lines (415 loc) · 14.2 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
'use strict';
var assert = require('chai').assert;
var Promise = require('bluebird');
var Redlock = require('./redlock');
test('single-server: https://www.npmjs.com/package/redis', [require('redis').createClient()]);
test('single-server: https://www.npmjs.com/package/redis (string_numbers=true)', [require('redis').createClient({string_numbers: true})]);
test('single-server: https://www.npmjs.com/package/ioredis', [new (require('ioredis'))()]);
test('multi-server: https://www.npmjs.com/package/ioredis', [new (require('ioredis'))({db: 1}), new (require('ioredis'))({db: 2}), new (require('ioredis'))({db: 3})]);
/* istanbul ignore next */
function test(name, clients){
var redlock = new Redlock(clients, {
retryCount: 2,
retryDelay: 150,
retryJitter: 50
});
var resource = 'Redlock:test:resource';
var error = 'Redlock:test:error';
describe('Redlock: ' + name, function(){
before(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].sadd(error, 'having a set here should cause a failure', cb);
}
});
it('should throw an error if not passed any clients', function(){
assert.throws(function(){
new Redlock([], {
retryCount: 2,
retryDelay: 150,
retryJitter: 0
});
});
});
it('emits a clientError event when a client error occurs', function(done){
var emitted = 0;
function test(err) {
assert.isNotNull(err);
emitted++;
}
redlock.on('clientError', test);
redlock.lock(error, 200, function(err, lock){
redlock.removeListener('clientError', test);
assert.isNotNull(err);
assert.equal(emitted, 3 * redlock.servers.length);
done();
});
});
it('supports custom script functions in options', function(){
var opts = {
lockScript: function(lockScript) { return lockScript + 'and 1'; },
unlockScript: function(unlockScript) { return unlockScript + 'and 2'; },
extendScript: function(extendScript) { return extendScript + 'and 3'; }
};
var customRedlock = new Redlock(clients, opts);
var i = 1;
assert.equal(customRedlock.lockScript, redlock.lockScript + 'and ' + i++);
assert.equal(customRedlock.unlockScript, redlock.unlockScript + 'and ' + i++);
assert.equal(customRedlock.extendScript, redlock.extendScript + 'and ' + i);
});
describe('callbacks', function(){
before(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
var one;
it('should lock a resource', function(done) {
redlock.lock(resource, 200, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.instanceOf(lock, Redlock.Lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.equal(lock.attempts, 1);
one = lock;
done();
});
});
var two;
var two_expiration;
it('should wait until a lock expires before issuing another lock', function(done) {
assert(one, 'Could not run because a required previous test failed.');
redlock.lock(resource, 800, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isAbove(Date.now()+1, one.expiration);
assert.isAbove(lock.attempts, 1);
two = lock;
two_expiration = lock.expiration;
done();
});
});
it('should unlock a resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.unlock(done);
assert.equal(two.expiration, 0, 'Failed to immediately invalidate the lock.');
});
it('should unlock an already-unlocked resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.unlock(done);
});
it('should error when unable to fully release a resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
var failingTwo = Object.create(two);
failingTwo.resource = error;
failingTwo.unlock(function(err) {
assert.isNotNull(err);
done();
});
});
it('should fail to extend a lock on an already-unlocked resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.extend(200, function(err, lock){
assert.isNotNull(err);
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 0);
done();
});
});
var three;
it('should issue another lock immediately after a resource is unlocked', function(done) {
assert(two_expiration, 'Could not run because a required previous test failed.');
redlock.lock(resource, 800, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isBelow(Date.now()-1, two_expiration);
assert.equal(lock.attempts, 1);
three = lock;
done();
});
});
var four;
it('should extend an unexpired lock', function(done) {
assert(three, 'Could not run because a required previous test failed.');
three.extend(800, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isAbove(lock.expiration, three.expiration-1);
assert.equal(lock.attempts, 1);
assert.equal(three, lock);
four = lock;
done();
});
});
it('should fail after the maximum retry count is exceeded', function(done) {
assert(four, 'Could not run because a required previous test failed.');
redlock.lock(resource, 200, function(err, lock){
assert.isNotNull(err);
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 3);
done();
});
});
it('should fail to extend an expired lock', function(done) {
assert(four, 'Could not run because a required previous test failed.');
setTimeout(function(){
three.extend(800, function(err, lock){
assert.isNotNull(err);
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 0);
done();
});
}, four.expiration - Date.now() + 100);
});
it('should issue another lock immediately after a resource is expired', function(done) {
assert(four, 'Could not run because a required previous test failed.');
redlock.lock(resource, 800, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.equal(lock.attempts, 1);
done();
});
});
after(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
});
describe('promises', function(){
before(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
var one;
it('should lock a resource', function(done) {
redlock.lock(resource, 200)
.done(function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.equal(lock.attempts, 1);
one = lock;
done();
}, done);
});
var two;
var two_expiration;
it('should wait until a lock expires before issuing another lock', function(done) {
assert(one, 'Could not run because a required previous test failed.');
redlock.lock(resource, 800)
.done(function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isAbove(Date.now()+1, one.expiration);
assert.isAbove(lock.attempts, 1);
two = lock;
two_expiration = lock.expiration;
done();
}, done);
});
it('should unlock a resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.unlock().done(done, done);
});
it('should unlock an already-unlocked resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.unlock().done(done, done);
});
it('should error when unable to fully release a resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
var failingTwo = Object.create(two);
failingTwo.resource = error;
failingTwo.unlock().done(done, function(err) {
assert.isNotNull(err);
done();
});
});
it('should fail to extend a lock on an already-unlocked resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
two.extend(200)
.done(function(){
done(new Error('Should have failed with a LockError'));
}, function(err){
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 0);
done();
});
});
var three;
it('should issue another lock immediately after a resource is unlocked', function(done) {
assert(two_expiration, 'Could not run because a required previous test failed.');
redlock.lock(resource, 800)
.done(function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isBelow(Date.now()-1, two_expiration);
assert.equal(lock.attempts, 1);
three = lock;
done();
}, done);
});
var four;
it('should extend an unexpired lock', function(done) {
assert(three, 'Could not run because a required previous test failed.');
three.extend(800)
.done(function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isAbove(lock.expiration, three.expiration-1);
assert.equal(lock.attempts, 1);
assert.equal(three, lock);
four = lock;
done();
}, done);
});
it('should fail after the maximum retry count is exceeded', function(done) {
assert(four, 'Could not run because a required previous test failed.');
redlock.lock(resource, 200)
.done(function(){
done(new Error('Should have failed with a LockError'));
}, function(err){
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 3);
done();
});
});
it('should fail to extend an expired lock', function(done) {
assert(four, 'Could not run because a required previous test failed.');
setTimeout(function(){
three.extend(800)
.done(function(){
done(new Error('Should have failed with a LockError'));
}, function(err){
assert.instanceOf(err, Redlock.LockError);
assert.equal(err.attempts, 0);
done();
});
}, four.expiration - Date.now() + 100);
});
after(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
});
describe('disposer', function(){
before(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
var one;
var one_expiration;
it('should automatically release a lock after the using block', function(done) {
Promise.using(
redlock.disposer(resource, 200),
function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.equal(lock.attempts, 1);
one = lock;
one_expiration = lock.expiration;
}
).done(done, done);
});
var two;
var two_expiration;
it('should issue another lock immediately after a resource is unlocked', function(done) {
assert(one_expiration, 'Could not run because a required previous test failed.');
Promise.using(
redlock.disposer(resource, 800),
function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isBelow(Date.now()-1, one_expiration);
assert.equal(lock.attempts, 1);
two = lock;
two_expiration = lock.expiration;
}
).done(done, done);
});
it('should call unlockErrorHandler when unable to fully release a resource', function(done) {
assert(two, 'Could not run because a required previous test failed.');
var errs = 0;
var lock;
Promise.using(
redlock.disposer(resource, 800, function(err) {
errs++;
}),
function(l){
lock = l;
lock.resource = error;
}
).done(function() {
assert.equal(errs, 1);
lock.resource = resource;
lock.unlock().done(done, done);
}, done);
});
var three_original, three_extended;
var three_original_expiration;
var three_extended_expiration;
it('should automatically release an extended lock', function(done) {
assert(two_expiration, 'Could not run because a required previous test failed.');
Promise.using(
redlock.disposer(resource, 200),
function(lock){
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-1);
assert.isBelow(Date.now()-1, two_expiration);
three_original = lock;
three_original_expiration = lock.expiration;
return Promise.delay(100)
.then(function(){ return lock.extend(200); })
.then(function(extended) {
assert.isObject(extended);
assert.isAbove(extended.expiration, Date.now()-1);
assert.isBelow(Date.now()-1, three_original_expiration);
assert.isAbove(extended.expiration, three_original_expiration);
assert.equal(lock.attempts, 1);
assert.equal(extended, lock);
three_extended = extended;
three_extended_expiration = extended.expiration;
});
}
)
.then(function(){
assert.equal(three_original.expiration, 0);
assert.equal(three_extended.expiration, 0);
}).done(done, done);
});
after(function(done) {
var err;
var l = clients.length; function cb(e){ if(e) err = e; l--; if(l === 0) done(err); }
for (var i = clients.length - 1; i >= 0; i--) {
clients[i].del(resource, cb);
}
});
});
describe('quit', function() {
it('should quit all clients', function(done){
redlock.quit()
.done(function(results) {
assert.isArray(results);
done();
}, done);
});
})
});
}