bitcore-wallet-service/test/locallock.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-04-03 08:23:55 -07:00
'use strict';
var _ = require('lodash');
var chai = require('chai');
var sinon = require('sinon');
var should = chai.should();
2015-04-07 13:02:08 -07:00
var Lock = require('../lib/locallock');
2015-04-03 08:23:55 -07:00
2015-04-08 06:21:01 -07:00
describe('Local locks', function() {
var lock;
beforeEach(function() {
2015-04-08 12:38:01 -07:00
this.clock = sinon.useFakeTimers();
2015-04-08 06:21:01 -07:00
lock = new Lock();
});
2015-04-08 12:38:01 -07:00
afterEach(function() {
this.clock.restore();
});
it('should lock tasks using the same token', function() {
2015-04-03 11:47:31 -07:00
var a = false,
b = false;
2015-04-08 06:21:01 -07:00
lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
2015-04-03 11:47:31 -07:00
a = true;
2015-04-03 08:23:55 -07:00
setTimeout(function() {
2015-04-08 06:21:01 -07:00
release();
2015-04-03 11:47:31 -07:00
}, 5);
2015-04-08 06:21:01 -07:00
lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
2015-04-03 11:47:31 -07:00
b = true;
2015-04-08 06:21:01 -07:00
release();
2015-04-03 08:23:55 -07:00
});
});
2015-04-08 12:38:01 -07:00
a.should.equal(true);
b.should.equal(false);
this.clock.tick(10);
a.should.equal(true);
b.should.equal(true);
2015-04-03 08:23:55 -07:00
});
2015-04-08 12:38:01 -07:00
it('should not lock tasks using different tokens', function() {
2015-04-03 08:23:55 -07:00
var i = 0;
2015-04-08 06:21:01 -07:00
lock.locked('123', 0, 0, function(err, release) {
should.not.exist(err);
2015-04-03 08:23:55 -07:00
i++;
setTimeout(function() {
2015-04-08 06:21:01 -07:00
release();
2015-04-03 11:47:31 -07:00
}, 5);
2015-04-08 06:21:01 -07:00
lock.locked('456', 0, 0, function(err, release) {
should.not.exist(err);
2015-04-03 08:23:55 -07:00
i++;
2015-04-08 06:21:01 -07:00
release();
2015-04-03 08:23:55 -07:00
});
});
2015-04-08 12:38:01 -07:00
i.should.equal(2);
2015-04-03 08:23:55 -07:00
});
2015-04-08 12:38:01 -07:00
it('should return error if unable to acquire lock', function() {
2015-04-08 07:39:15 -07:00
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');
});
});
2015-04-08 12:38:01 -07:00
this.clock.tick(2);
2015-04-08 07:39:15 -07:00
});
2015-04-08 12:38:01 -07:00
it('should release lock if acquired for a long time', function() {
2015-04-08 07:39:15 -07:00
var i = 0;
lock.locked('123', 0, 3, function(err, release) {
should.not.exist(err);
i++;
2015-04-08 12:38:01 -07:00
lock.locked('123', 20, 0, function(err, release) {
2015-04-08 07:39:15 -07:00
should.not.exist(err);
i++;
2015-04-08 12:38:01 -07:00
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();
2015-04-08 07:39:15 -07:00
});
});
2015-04-08 12:38:01 -07:00
i.should.equal(1);
this.clock.tick(4);
i.should.equal(2)
this.clock.tick(7);
i.should.equal(3)
2015-04-08 07:39:15 -07:00
});
2015-04-03 08:23:55 -07:00
});