Tests for locker

This commit is contained in:
Raul Ochoa 2016-10-12 13:15:55 +02:00
parent 75fc21241f
commit c21f373291

View File

@ -0,0 +1,60 @@
'use strict';
require('../../helper');
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');
var Locker = require('../../../batch/leader/locker');
describe('locker', function() {
var host = 'localhost';
var TTL = 500;
var config = { ttl: TTL, redisConfig: redisUtils.getConfig() };
it('should lock and unlock', function (done) {
var lockerA = Locker.create('redis-distlock', config);
var lockerB = Locker.create('redis-distlock', config);
lockerA.lock(host, function(err, lock) {
if (err) {
return done(err);
}
assert.ok(lock);
// others can't lock on same host
lockerB.lock(host, function(err) {
assert.ok(err);
assert.equal(err.name, 'LockError');
lockerA.unlock(host, function(err) {
assert.ok(!err);
// others can lock after unlock
lockerB.lock(host, function(err, lock2) {
assert.ok(!err);
assert.ok(lock2);
lockerB.unlock(host, done);
});
});
});
});
});
it('should lock and keep locking until unlock', function (done) {
var lockerA = Locker.create('redis-distlock', config);
var lockerB = Locker.create('redis-distlock', config);
lockerA.lock(host, function(err, lock) {
if (err) {
return done(err);
}
setTimeout(function() {
lockerB.lock(host, function(err) {
assert.ok(err);
assert.ok(lock);
lockerA.unlock(host, done);
});
}, 2 * TTL);
});
});
});