This commit is contained in:
Ivan Socolsky 2015-04-08 11:39:15 -03:00
parent c33e0b0de4
commit 7a8a7ea997
2 changed files with 57 additions and 6 deletions

View File

@ -9,26 +9,48 @@ function Lock() {
Lock.prototype._runOne = function(token) {
var self = this;
if (locks[token].length == 0) return;
var item = _.first(locks[token]);
if (!item || item.started) return;
var task = locks[token][0];
item.started = true;
if (item.maxRunningTime > 0) {
setTimeout(function() {
var it = _.first(locks[token]);
if (it != item) return;
locks[token].shift();
self._runOne(token);
}, item.maxRunningTime);
}
task(null, function() {
item.fn(null, function() {
locks[token].shift();
self._runOne(token);
});
};
Lock.prototype.locked = function(token, wait, max, task) {
var self = this;
if (_.isUndefined(locks[token])) {
locks[token] = [];
}
locks[token].push(task);
var item = {
maxRunningTime: max,
started: false,
fn: task,
};
locks[token].push(item);
if (locks[token].length == 1) {
this._runOne(token);
if (wait > 0) {
setTimeout(function() {
var it = _.find(locks[token], item);
if (!it || it.started) return;
locks[token] = _.without(locks[token], it);
it.fn(new Error('Could not acquire lock ' + token));
}, wait);
}
self._runOne(token);
};
module.exports = Lock;

View File

@ -56,4 +56,33 @@ describe('Local locks', function() {
done();
}, 1);
});
it('should return error if unable to acquire lock', function(done) {
lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
setTimeout(function() {
release();
}, 5);
lock.locked('123', 1, 0, function(err, release) {
should.exist(err);
err.toString().should.contain('Could not acquire lock 123');
done();
});
});
});
it('should release lock if acquired for a long time', function(done) {
var i = 0;
lock.locked('123', 0, 3, function(err, release) {
should.not.exist(err);
i++;
lock.locked('123', 15, 0, function(err, release) {
should.not.exist(err);
i++;
});
});
setTimeout(function() {
i.should.equal(2);
done();
}, 10);
});
});