switched out assertion library with should.js... I really should have written my own baby util library since that was the third time I've done that...

This commit is contained in:
Spencer Alger
2013-12-23 15:33:53 -07:00
parent 3d0b2fde4e
commit 2b3734a081
32 changed files with 828 additions and 987 deletions

View File

@ -1,7 +1,7 @@
var ConnectionAbstract = require('../../src/lib/connection');
var Host = require('../../src/lib/host');
var sinon = require('sinon');
var should = require('should');
var expect = require('expect.js');
var _ = require('lodash');
var errors = require('../../src/lib/errors');
@ -12,24 +12,24 @@ describe('Connection Abstract', function () {
it('constructs with defaults for host, and bound', function () {
var conn = new ConnectionAbstract(host);
conn.host.should.be.exactly(host);
expect(conn.host).to.be(host);
});
it('requires a valid host', function () {
(function () {
expect(function () {
new ConnectionAbstract();
}).should.throw(TypeError);
}).to.throwError(TypeError);
(function () {
expect(function () {
new ConnectionAbstract({});
}).should.throw(TypeError);
}).to.throwError(TypeError);
});
it('required that the request method is overridden', function () {
(function () {
expect(function () {
var conn = new ConnectionAbstract(host);
conn.request();
}).should.throw(/overwrit/);
}).to.throwError(/overwrit/);
});
describe('#ping', function () {
@ -38,18 +38,18 @@ describe('Connection Abstract', function () {
stub(conn, 'request');
var cb = function () {};
conn.ping(cb);
conn.request.callCount.should.eql(1);
conn.request.lastCall.args[0].should.have.type('object');
conn.request.lastCall.args[1].should.have.type('function');
expect(conn.request.callCount).to.eql(1);
expect(conn.request.lastCall.args[0]).to.be.a('object');
expect(conn.request.lastCall.args[1]).to.be.a('function');
});
it('accpets just params', function () {
var conn = new ConnectionAbstract(host);
stub(conn, 'request');
conn.ping({});
conn.request.callCount.should.eql(1);
conn.request.lastCall.args[0].should.have.type('object');
conn.request.lastCall.args[1].should.have.type('function');
expect(conn.request.callCount).to.eql(1);
expect(conn.request.lastCall.args[0]).to.be.a('object');
expect(conn.request.lastCall.args[1]).to.be.a('function');
});
it('allows overriding the requestTimeout, method, and path', function () {
@ -61,9 +61,9 @@ describe('Connection Abstract', function () {
requestTimeout: 10000
};
conn.ping(params);
conn.request.callCount.should.eql(1);
conn.request.lastCall.args[0].should.include(params);
conn.request.lastCall.args[1].should.have.type('function');
expect(conn.request.callCount).to.eql(1);
expect(conn.request.lastCall.args[0]).to.eql(params);
expect(conn.request.lastCall.args[1]).to.be.a('function');
});
it('calls it\'s own request method', function () {
@ -71,7 +71,7 @@ describe('Connection Abstract', function () {
var football = {};
stub(conn, 'request');
conn.ping();
conn.request.callCount.should.eql(1);
expect(conn.request.callCount).to.eql(1);
});
it('sets a timer for the request', function (done) {
@ -81,7 +81,7 @@ describe('Connection Abstract', function () {
stub(conn, 'request', function (params, cb) {
setTimeout(function () {
should(++order).eql(2);
expect(++order).to.eql(2);
cb();
}, 10001);
});
@ -89,8 +89,8 @@ describe('Connection Abstract', function () {
conn.ping({
requestTimeout: 100
}, function (err) {
should(++order).eql(1);
err.should.be.an.instanceOf(errors.RequestTimeout);
expect(++order).to.eql(1);
expect(err).to.be.an(errors.RequestTimeout);
});
process.nextTick(function () {
@ -107,20 +107,20 @@ describe('Connection Abstract', function () {
stub(conn, 'request', function (params, cb) {
setTimeout(function () {
should(++order).eql(3);
expect(++order).to.eql(3);
cb();
}, 10001);
return function () {
should(++order).eql(1);
expect(++order).to.eql(1);
};
});
conn.ping({
requestTimeout: 100
}, function (err) {
should(++order).eql(2);
err.should.be.an.instanceOf(errors.RequestTimeout);
expect(++order).to.eql(2);
expect(err).to.be.an(errors.RequestTimeout);
});
process.nextTick(function () {
@ -130,6 +130,7 @@ describe('Connection Abstract', function () {
done();
});
});
it('ignores the response from the request if it already aborted');
});
@ -146,90 +147,15 @@ describe('Connection Abstract', function () {
};
conn.setStatus('closed');
emitted.name.should.eql('status set');
emitted.args.should.eql(['closed', null, conn]);
expect(emitted.name).to.eql('status set');
expect(emitted.args).to.eql(['closed', undefined, conn]);
});
it('stores the status in this.status', function () {
var conn = new ConnectionAbstract(host);
conn.setStatus('closed');
conn.status.should.eql('closed');
expect(conn.status).to.eql('closed');
});
// it('sets a timeout when set to dead, and removed when alive', function () {
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
// stub.autoRelease(clock);
// var conn = new ConnectionAbstract(host);
// var start = _.size(clock.timeouts);
// conn.setStatus('dead');
// _.size(clock.timeouts).should.be.eql(start + 1);
// conn.setStatus('alive');
// _.size(clock.timeouts).should.eql(start);
// clock.restore();
// });
});
// describe('#resuscitate', function () {
// it('should not ping the connection unless it is still dead', function () {
// var conn = new ConnectionAbstract(host);
// conn.setStatus('alive');
// stub(conn, 'ping', function () {
// throw new Error('ping should not have been called');
// });
// conn.resuscitate();
// });
// it('should ping the connection after the deadTimeout, and set the status to "alive" on pong', function (done) {
// var conn = new ConnectionAbstract(host);
// var clock;
// stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
// // schedules the resuscitate
// conn.setStatus('dead');
// // override the ping method to just callback without an error
// stub(conn, 'ping', function (cb) {
// cb();
// });
// // will be called after the ping calls back
// stub(conn, 'setStatus', function (status) {
// status.should.eql('alive');
// done();
// });
// // fast forward the clock
// clock.tick(conn.deadTimeout);
// });
// it('should ping the connection after the deadTimeout, and set the status to "dead" on error', function (done) {
// var conn = new ConnectionAbstract(host);
// var clock;
// stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
// // schedules the resuscitate
// conn.setStatus('dead');
// // override the ping method to just callback without an error
// stub(conn, 'ping', function (cb) {
// cb(new Error('server still down'));
// });
// // will be called after the ping calls back
// stub(conn, 'setStatus', function (status) {
// status.should.eql('dead');
// done();
// });
// // fast forward the clock
// clock.tick(conn.deadTimeout);
// });
// });
});