CartoDB-SQL-API/test/integration/batch/locker.js
2016-10-12 13:15:55 +02:00

61 lines
1.8 KiB
JavaScript

'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);
});
});
});