update tests to use new timer object

This commit is contained in:
Spencer Alger
2015-01-09 23:23:06 -07:00
parent 547ff26104
commit 3b69e21262
5 changed files with 31 additions and 39 deletions

View File

@ -33,7 +33,7 @@ process.once('message', function (port) {
var out = { var out = {
socketCount: err || sockets.length, socketCount: err || sockets.length,
remaining: _.where(sockets, { destroyed: true }).length - 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(); clock.restore();

View File

@ -77,6 +77,7 @@ describe('Connection Abstract', function () {
it('sets a timer for the request', function (done) { it('sets a timer for the request', function (done) {
var conn = new ConnectionAbstract(host); var conn = new ConnectionAbstract(host);
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'); var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
stub.autoRelease(clock);
var order = 0; var order = 0;
stub(conn, 'request', function (params, cb) { stub(conn, 'request', function (params, cb) {
@ -96,13 +97,13 @@ describe('Connection Abstract', function () {
process.nextTick(function () { process.nextTick(function () {
clock.tick(1000); clock.tick(1000);
clock.tick(10000); clock.tick(10000);
clock.restore();
done(); done();
}); });
}); });
it('calls the requestAborter if req takes too long', function (done) { it('calls the requestAborter if req takes too long', function (done) {
var conn = new ConnectionAbstract(host); var conn = new ConnectionAbstract(host);
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'); var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
stub.autoRelease(clock);
var order = 0; var order = 0;
stub(conn, 'request', function (params, cb) { stub(conn, 'request', function (params, cb) {
@ -126,7 +127,6 @@ describe('Connection Abstract', function () {
process.nextTick(function () { process.nextTick(function () {
clock.tick(1000); clock.tick(1000);
clock.tick(10000); clock.tick(10000);
clock.restore();
done(); done();
}); });
}); });

View File

@ -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', it('should ping all of the dead nodes, in order of oldest timeout, and return the first that\'s okay',
function (done) { function (done) {
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'); var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
stub.autoRelease(clock);
var pool = new ConnectionPool({ var pool = new ConnectionPool({
deadTimeout: 10000 deadTimeout: 10000
}); });
@ -201,7 +202,6 @@ describe('Connection Pool', function () {
}); });
pool.select(function (err, selection) { pool.select(function (err, selection) {
clock.restore();
expect(selection).to.be(expectedSelection); expect(selection).to.be(expectedSelection);
expect(pingQueue.length).to.be(0); expect(pingQueue.length).to.be(0);
pool.setHosts([]); pool.setHosts([]);
@ -244,17 +244,18 @@ describe('Connection Pool', function () {
}); });
it('clears and resets the timeout when a connection redies', 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'); connection.setStatus('dead');
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
var id = _(clock.timeouts).keys().first(); var id = _(clock.timers).keys().first();
// it re-dies // it re-dies
connection.setStatus('dead'); connection.setStatus('dead');
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
expect(_(clock.timeouts).keys().first()).to.not.eql(id); expect(_(clock.timers).keys().first()).to.not.eql(id);
clock.restore();
}); });
it('does nothing when a connection is re-alive', function () { it('does nothing when a connection is re-alive', function () {

View File

@ -75,20 +75,20 @@ describe('Transport Class', function () {
it('schedules a sniff when sniffInterval is set', function () { it('schedules a sniff when sniffInterval is set', function () {
var clock = sinon.useFakeTimers('setTimeout'); var clock = sinon.useFakeTimers('setTimeout');
stub.autoRelease(clock);
stub(Transport.prototype, 'sniff'); stub(Transport.prototype, 'sniff');
var trans = new Transport({ var trans = new Transport({
sniffInterval: 25000 sniffInterval: 25000
}); });
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
var id = _.keys(clock.timeouts).pop(); var id = _.keys(clock.timers).pop();
clock.tick(25000); clock.tick(25000);
expect(trans.sniff.callCount).to.eql(1); expect(trans.sniff.callCount).to.eql(1);
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
expect(clock.timeouts).to.not.have.key(id); expect(clock.timers).to.not.have.key(id);
clock.restore();
}); });
describe('host config', function () { describe('host config', function () {
@ -628,6 +628,7 @@ describe('Transport Class', function () {
describe('timeout', function () { describe('timeout', function () {
it('uses 30 seconds for the default', function () { it('uses 30 seconds for the default', function () {
var clock = sinon.useFakeTimers(); var clock = sinon.useFakeTimers();
stub.autoRelease(clock);
var tran = new Transport({}); var tran = new Transport({});
var err; var err;
@ -635,15 +636,15 @@ describe('Transport Class', function () {
// disregard promise, prevent bluebird's warnings // disregard promise, prevent bluebird's warnings
prom.then(_.noop, _.noop); prom.then(_.noop, _.noop);
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
_.each(clock.timeouts, function (timer, id) { _.each(clock.timers, function (timer, id) {
expect(timer.callAt).to.eql(30000); expect(timer.callAt).to.eql(30000);
clearTimeout(id); clearTimeout(id);
}); });
clock.restore();
}); });
it('inherits the requestTimeout from the transport', function () { it('inherits the requestTimeout from the transport', function () {
var clock = sinon.useFakeTimers(); var clock = sinon.useFakeTimers();
stub.autoRelease(clock);
var tran = new Transport({ var tran = new Transport({
requestTimeout: 5000 requestTimeout: 5000
}); });
@ -653,18 +654,18 @@ describe('Transport Class', function () {
// disregard promise, prevent bluebird's warnings // disregard promise, prevent bluebird's warnings
prom.then(_.noop, _.noop); prom.then(_.noop, _.noop);
expect(_.size(clock.timeouts)).to.eql(1); expect(_.size(clock.timers)).to.eql(1);
_.each(clock.timeouts, function (timer, id) { _.each(clock.timers, function (timer, id) {
expect(timer.callAt).to.eql(5000); expect(timer.callAt).to.eql(5000);
clearTimeout(id); clearTimeout(id);
}); });
clock.restore();
}); });
_.each([false, 0, null], function (falsy) { _.each([false, 0, null], function (falsy) {
it('skips the timeout when it is ' + falsy, function () { it('skips the timeout when it is ' + falsy, function () {
var clock = sinon.useFakeTimers(); var clock = sinon.useFakeTimers();
stub.autoRelease(clock);
var tran = new Transport({}); var tran = new Transport({});
stub(tran.connectionPool, 'select', function () {}); stub(tran.connectionPool, 'select', function () {});
@ -672,8 +673,7 @@ describe('Transport Class', function () {
requestTimeout: falsy requestTimeout: falsy
}, function (_err) {}); }, function (_err) {});
expect(_.size(clock.timeouts)).to.eql(0); expect(_.size(clock.timers)).to.eql(0);
clock.restore();
}); });
}); });
}); });

View File

@ -262,6 +262,7 @@ describe('Transport + Mock server', function () {
describe('timeout', function () { describe('timeout', function () {
it('clears the timeout when the request is complete', function () { it('clears the timeout when the request is complete', function () {
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'); var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
stub.autoRelease(clock);
var tran = new Transport({ var tran = new Transport({
host: 'http://localhost:9200' host: 'http://localhost:9200'
}); });
@ -276,13 +277,12 @@ describe('Transport + Mock server', function () {
expect(err).to.be(undefined); expect(err).to.be(undefined);
expect(resp).to.eql({ i: 'am here' }); expect(resp).to.eql({ i: 'am here' });
expect(status).to.eql(200); expect(status).to.eql(200);
expect(_.keys(clock.timeouts)).to.have.length(0); expect(_.keys(clock.timers)).to.have.length(0);
clock.restore(); clock.restore();
}); });
}); });
it('timeout responds with a requestTimeout error', function (done) { it('timeout responds with a requestTimeout error', function (done) {
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
var tran = new Transport({ var tran = new Transport({
host: 'http://localhost:9200' host: 'http://localhost:9200'
}); });
@ -298,25 +298,16 @@ describe('Transport + Mock server', function () {
requestTimeout: 25 requestTimeout: 25
}, function (err, resp, status) { }, function (err, resp, status) {
expect(err).to.be.a(errors.RequestTimeout); expect(err).to.be.a(errors.RequestTimeout);
// expect(_.keys(clock.timeouts)).to.have.length(0);
// clock.restore();
done(); done();
}); });
}); });
}); });
describe('sniffOnConnectionFault', function () { 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 () { 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') var serverMock = nock('http://esbox.1.com')
.get('/') .get('/')
.reply(200, function () { .reply(200, function () {
@ -351,9 +342,9 @@ describe('Transport + Mock server', function () {
.catch(function (err) { .catch(function (err) {
expect(ConnectionPool.prototype._onConnectionDied.callCount).to.eql(1); expect(ConnectionPool.prototype._onConnectionDied.callCount).to.eql(1);
expect(tran.sniff.callCount).to.eql(0); 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(); timeout.func();
expect(tran.sniff.callCount).to.eql(1); expect(tran.sniff.callCount).to.eql(1);
}); });