|
|
@ -10,9 +10,13 @@ var Lock = require('../lib/locallock'); |
|
|
|
describe('Local locks', function() { |
|
|
|
var lock; |
|
|
|
beforeEach(function() { |
|
|
|
this.clock = sinon.useFakeTimers(); |
|
|
|
lock = new Lock(); |
|
|
|
}); |
|
|
|
it('should lock tasks using the same token', function(done) { |
|
|
|
afterEach(function() { |
|
|
|
this.clock.restore(); |
|
|
|
}); |
|
|
|
it('should lock tasks using the same token', function() { |
|
|
|
var a = false, |
|
|
|
b = false; |
|
|
|
lock.locked('123', 0, 0, function(err, release) { |
|
|
@ -27,17 +31,13 @@ describe('Local locks', function() { |
|
|
|
release(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
setTimeout(function() { |
|
|
|
a.should.equal(true); |
|
|
|
b.should.equal(false); |
|
|
|
}, 1); |
|
|
|
setTimeout(function() { |
|
|
|
a.should.equal(true); |
|
|
|
b.should.equal(true); |
|
|
|
done(); |
|
|
|
}, 8); |
|
|
|
a.should.equal(true); |
|
|
|
b.should.equal(false); |
|
|
|
this.clock.tick(10); |
|
|
|
a.should.equal(true); |
|
|
|
b.should.equal(true); |
|
|
|
}); |
|
|
|
it('should not lock tasks using different tokens', function(done) { |
|
|
|
it('should not lock tasks using different tokens', function() { |
|
|
|
var i = 0; |
|
|
|
lock.locked('123', 0, 0, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
@ -51,12 +51,9 @@ describe('Local locks', function() { |
|
|
|
release(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
setTimeout(function() { |
|
|
|
i.should.equal(2); |
|
|
|
done(); |
|
|
|
}, 1); |
|
|
|
i.should.equal(2); |
|
|
|
}); |
|
|
|
it('should return error if unable to acquire lock', function(done) { |
|
|
|
it('should return error if unable to acquire lock', function() { |
|
|
|
lock.locked('123', 0, 0, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
setTimeout(function() { |
|
|
@ -65,24 +62,50 @@ describe('Local locks', function() { |
|
|
|
lock.locked('123', 1, 0, function(err, release) { |
|
|
|
should.exist(err); |
|
|
|
err.toString().should.contain('Could not acquire lock 123'); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.clock.tick(2); |
|
|
|
}); |
|
|
|
it('should release lock if acquired for a long time', function(done) { |
|
|
|
it('should release lock if acquired for a long time', function() { |
|
|
|
var i = 0; |
|
|
|
lock.locked('123', 0, 3, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
i++; |
|
|
|
lock.locked('123', 15, 0, function(err, release) { |
|
|
|
lock.locked('123', 20, 0, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
i++; |
|
|
|
release(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
i.should.equal(1); |
|
|
|
this.clock.tick(1); |
|
|
|
i.should.equal(1); |
|
|
|
this.clock.tick(10); |
|
|
|
i.should.equal(2); |
|
|
|
}); |
|
|
|
it('should only release one pending task on lock timeout', function() { |
|
|
|
var i = 0; |
|
|
|
lock.locked('123', 0, 3, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
i++; |
|
|
|
lock.locked('123', 5, 0, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
i++; |
|
|
|
setTimeout(function() { |
|
|
|
release(); |
|
|
|
}, 5); |
|
|
|
}); |
|
|
|
lock.locked('123', 20, 0, function(err, release) { |
|
|
|
should.not.exist(err); |
|
|
|
i++; |
|
|
|
release(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
setTimeout(function() { |
|
|
|
i.should.equal(2); |
|
|
|
done(); |
|
|
|
}, 10); |
|
|
|
i.should.equal(1); |
|
|
|
this.clock.tick(4); |
|
|
|
i.should.equal(2) |
|
|
|
this.clock.tick(7); |
|
|
|
i.should.equal(3) |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|