forked from mike-marcacci/node-redlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
135 lines (118 loc) · 3.75 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
'use strict';
var assert = require('chai').assert;
var Redlock = require('./redlock');
test('node-redis', [require('redis').createClient()]);
test('ioredis', [new (require('ioredis'))()]);
function test(name, clients){
var redlock = new Redlock(clients, {
retryCount: 2,
retryDelay: 150
});
var resource = 'Redlock:test:resource';
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].del(resource, cb);
}
});
it('should throw an error if not passed any clients', function(){
assert.throws(function(){
new Redlock([], {
retryCount: 2,
retryDelay: 150
});
});
});
var one;
it('should lock a resource', function(done){
redlock.lock(resource, 200, function(err, lock){
if(err) throw err;
assert.isObject(lock);
assert.isAbove(lock.expiration, Date.now()-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);
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);
});
it('should silently fail to unlock an already-unlocked resource', function(done){
assert(two, 'Could not run because a required previous test failed.');
two.unlock(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.equal(err.name, 'LockError');
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);
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);
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.equal(err.name, 'LockError');
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.equal(err.name, 'LockError');
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);
}
});
});
}