Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
151623c
commit f7a9cc0
Showing
4 changed files
with
154 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
{ | ||
"node": true, | ||
"unused": "vars", | ||
"multistr": true | ||
"multistr": true, | ||
"globals": { | ||
"describe": false, | ||
"it": false, | ||
"before": false, | ||
"beforeEach": false, | ||
"after": false, | ||
"afterEach": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,101 @@ | ||
'use strict'; | ||
|
||
var assert = require('chai').assert; | ||
var client = require('redis').createClient(); | ||
var Redlock = require('./redlock'); | ||
|
||
var redlock = new Redlock({ | ||
retryCount: 2, | ||
retryDelay: 150 | ||
}, client); | ||
|
||
var resource = 'Redlock:test:resource'; | ||
|
||
describe('Redlock', function(){ | ||
|
||
before(function(done){ | ||
client.del(resource, done); | ||
}); | ||
|
||
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()); | ||
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()); | ||
assert.isAbove(Date.now(), 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); | ||
}); | ||
|
||
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()); | ||
assert.isBelow(Date.now(), 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()); | ||
assert.isAbove(lock.expiration, three.expiration); | ||
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){ | ||
client.del(resource, done); | ||
}); | ||
|
||
}); |