CartoDB-SQL-API/test/integration/batch/locker.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-10-12 19:15:55 +08:00
'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;
2016-10-17 21:02:34 +08:00
var config = { ttl: TTL, pool: redisUtils.getPool() };
2016-10-12 19:15:55 +08:00
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);
});
});
});