From 3b69e21262d2e6e82c7bab670554b77227f69e21 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Fri, 9 Jan 2015 23:23:06 -0700 Subject: [PATCH] update tests to use new timer object --- test/fixtures/keepalive.js | 2 +- test/unit/specs/connection_abstract.js | 4 ++-- test/unit/specs/connection_pool.js | 15 +++++++------- test/unit/specs/transport.js | 26 ++++++++++++------------ test/unit/specs/transport_with_server.js | 23 +++++++-------------- 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/test/fixtures/keepalive.js b/test/fixtures/keepalive.js index 1866915d8..f3c95e105 100644 --- a/test/fixtures/keepalive.js +++ b/test/fixtures/keepalive.js @@ -33,7 +33,7 @@ process.once('message', function (port) { var out = { socketCount: err || sockets.length, remaining: _.where(sockets, { destroyed: true }).length - sockets.length, - timeouts: _.size(clock.timeouts) && _.pluck(clock.timeouts, 'func').map(String) + timeouts: _.size(clock.timers) && _.pluck(clock.timers, 'func').map(String) }; clock.restore(); diff --git a/test/unit/specs/connection_abstract.js b/test/unit/specs/connection_abstract.js index a06a211e3..f949af516 100644 --- a/test/unit/specs/connection_abstract.js +++ b/test/unit/specs/connection_abstract.js @@ -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(); }); }); diff --git a/test/unit/specs/connection_pool.js b/test/unit/specs/connection_pool.js index eaed37a61..5f43e02e3 100644 --- a/test/unit/specs/connection_pool.js +++ b/test/unit/specs/connection_pool.js @@ -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 () { diff --git a/test/unit/specs/transport.js b/test/unit/specs/transport.js index 93f0a1bd5..0072d9dd6 100644 --- a/test/unit/specs/transport.js +++ b/test/unit/specs/transport.js @@ -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); }); }); }); diff --git a/test/unit/specs/transport_with_server.js b/test/unit/specs/transport_with_server.js index 1e1997036..df6c80ddd 100644 --- a/test/unit/specs/transport_with_server.js +++ b/test/unit/specs/transport_with_server.js @@ -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); });