update tests to use new timer object
This commit is contained in:
@ -77,6 +77,7 @@ describe('Connection Abstract', function () {
|
||||
it('sets a timer for the request', function (done) {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var order = 0;
|
||||
|
||||
stub(conn, 'request', function (params, cb) {
|
||||
@ -96,13 +97,13 @@ describe('Connection Abstract', function () {
|
||||
process.nextTick(function () {
|
||||
clock.tick(1000);
|
||||
clock.tick(10000);
|
||||
clock.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('calls the requestAborter if req takes too long', function (done) {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var order = 0;
|
||||
|
||||
stub(conn, 'request', function (params, cb) {
|
||||
@ -126,7 +127,6 @@ describe('Connection Abstract', function () {
|
||||
process.nextTick(function () {
|
||||
clock.tick(1000);
|
||||
clock.tick(10000);
|
||||
clock.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@ -165,6 +165,7 @@ describe('Connection Pool', function () {
|
||||
it('should ping all of the dead nodes, in order of oldest timeout, and return the first that\'s okay',
|
||||
function (done) {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var pool = new ConnectionPool({
|
||||
deadTimeout: 10000
|
||||
});
|
||||
@ -201,7 +202,6 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
|
||||
pool.select(function (err, selection) {
|
||||
clock.restore();
|
||||
expect(selection).to.be(expectedSelection);
|
||||
expect(pingQueue.length).to.be(0);
|
||||
pool.setHosts([]);
|
||||
@ -244,17 +244,18 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
|
||||
it('clears and resets the timeout when a connection redies', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var clock = sinon.useFakeTimers();
|
||||
stub.autoRelease(clock);
|
||||
|
||||
debugger;
|
||||
connection.setStatus('dead');
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
var id = _(clock.timeouts).keys().first();
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
var id = _(clock.timers).keys().first();
|
||||
|
||||
// it re-dies
|
||||
connection.setStatus('dead');
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
expect(_(clock.timeouts).keys().first()).to.not.eql(id);
|
||||
clock.restore();
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
expect(_(clock.timers).keys().first()).to.not.eql(id);
|
||||
});
|
||||
|
||||
it('does nothing when a connection is re-alive', function () {
|
||||
|
||||
@ -75,20 +75,20 @@ describe('Transport Class', function () {
|
||||
|
||||
it('schedules a sniff when sniffInterval is set', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout');
|
||||
stub.autoRelease(clock);
|
||||
stub(Transport.prototype, 'sniff');
|
||||
|
||||
var trans = new Transport({
|
||||
sniffInterval: 25000
|
||||
});
|
||||
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
var id = _.keys(clock.timeouts).pop();
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
var id = _.keys(clock.timers).pop();
|
||||
clock.tick(25000);
|
||||
expect(trans.sniff.callCount).to.eql(1);
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
expect(clock.timeouts).to.not.have.key(id);
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
expect(clock.timers).to.not.have.key(id);
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
describe('host config', function () {
|
||||
@ -628,6 +628,7 @@ describe('Transport Class', function () {
|
||||
describe('timeout', function () {
|
||||
it('uses 30 seconds for the default', function () {
|
||||
var clock = sinon.useFakeTimers();
|
||||
stub.autoRelease(clock);
|
||||
var tran = new Transport({});
|
||||
var err;
|
||||
|
||||
@ -635,15 +636,15 @@ describe('Transport Class', function () {
|
||||
// disregard promise, prevent bluebird's warnings
|
||||
prom.then(_.noop, _.noop);
|
||||
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
_.each(clock.timeouts, function (timer, id) {
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
_.each(clock.timers, function (timer, id) {
|
||||
expect(timer.callAt).to.eql(30000);
|
||||
clearTimeout(id);
|
||||
});
|
||||
clock.restore();
|
||||
});
|
||||
it('inherits the requestTimeout from the transport', function () {
|
||||
var clock = sinon.useFakeTimers();
|
||||
stub.autoRelease(clock);
|
||||
var tran = new Transport({
|
||||
requestTimeout: 5000
|
||||
});
|
||||
@ -653,18 +654,18 @@ describe('Transport Class', function () {
|
||||
// disregard promise, prevent bluebird's warnings
|
||||
prom.then(_.noop, _.noop);
|
||||
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
_.each(clock.timeouts, function (timer, id) {
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
_.each(clock.timers, function (timer, id) {
|
||||
expect(timer.callAt).to.eql(5000);
|
||||
clearTimeout(id);
|
||||
});
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
|
||||
_.each([false, 0, null], function (falsy) {
|
||||
it('skips the timeout when it is ' + falsy, function () {
|
||||
var clock = sinon.useFakeTimers();
|
||||
stub.autoRelease(clock);
|
||||
var tran = new Transport({});
|
||||
stub(tran.connectionPool, 'select', function () {});
|
||||
|
||||
@ -672,8 +673,7 @@ describe('Transport Class', function () {
|
||||
requestTimeout: falsy
|
||||
}, function (_err) {});
|
||||
|
||||
expect(_.size(clock.timeouts)).to.eql(0);
|
||||
clock.restore();
|
||||
expect(_.size(clock.timers)).to.eql(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -262,6 +262,7 @@ describe('Transport + Mock server', function () {
|
||||
describe('timeout', function () {
|
||||
it('clears the timeout when the request is complete', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
@ -276,13 +277,12 @@ describe('Transport + Mock server', function () {
|
||||
expect(err).to.be(undefined);
|
||||
expect(resp).to.eql({ i: 'am here' });
|
||||
expect(status).to.eql(200);
|
||||
expect(_.keys(clock.timeouts)).to.have.length(0);
|
||||
expect(_.keys(clock.timers)).to.have.length(0);
|
||||
clock.restore();
|
||||
});
|
||||
});
|
||||
|
||||
it('timeout responds with a requestTimeout error', function (done) {
|
||||
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
var tran = new Transport({
|
||||
host: 'http://localhost:9200'
|
||||
});
|
||||
@ -298,25 +298,16 @@ describe('Transport + Mock server', function () {
|
||||
requestTimeout: 25
|
||||
}, function (err, resp, status) {
|
||||
expect(err).to.be.a(errors.RequestTimeout);
|
||||
// expect(_.keys(clock.timeouts)).to.have.length(0);
|
||||
// clock.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('sniffOnConnectionFault', function () {
|
||||
var clock;
|
||||
|
||||
beforeEach(function () {
|
||||
clock = sinon.useFakeTimers('setTimeout');
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('schedules a sniff when sniffOnConnectionFault is set and a connection failes', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout');
|
||||
stub.autoRelease(clock);
|
||||
|
||||
var serverMock = nock('http://esbox.1.com')
|
||||
.get('/')
|
||||
.reply(200, function () {
|
||||
@ -351,9 +342,9 @@ describe('Transport + Mock server', function () {
|
||||
.catch(function (err) {
|
||||
expect(ConnectionPool.prototype._onConnectionDied.callCount).to.eql(1);
|
||||
expect(tran.sniff.callCount).to.eql(0);
|
||||
expect(_.size(clock.timeouts)).to.eql(1);
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
|
||||
var timeout = _.values(clock.timeouts).pop();
|
||||
var timeout = _.values(clock.timers).pop();
|
||||
timeout.func();
|
||||
expect(tran.sniff.callCount).to.eql(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user