Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Test opt.retryCount
Browse files Browse the repository at this point in the history
  • Loading branch information
Throne3d committed Sep 29, 2017
1 parent 2c2480d commit f01915f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ Client.prototype.connect = function(retryCount, callback) {
self.out.debug('Disconnected: reconnecting');
self.conn.cyclingPingTimer.stop();
self.cancelAutoRenick();
self.conn = null;

// limit to retryCount reconnections
if (self.opt.retryCount !== null && retryCount >= self.opt.retryCount) {
Expand All @@ -931,7 +932,6 @@ Client.prototype.connect = function(retryCount, callback) {
}

// actually reconnect
self.conn = null;
self.out.debug('Waiting ' + self.opt.retryDelay + 'ms before retrying');
self.retryTimeout = setTimeout(function() {
self.connect(retryCount + 1);
Expand Down
2 changes: 1 addition & 1 deletion test/test-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Client', function() {
self.client.connect(0, function() { done(); });
});

// TODO: test first parameter is respected
// see test-reconnect.js for testing of retryCount parameter
});

describe('#send', function() {
Expand Down
149 changes: 127 additions & 22 deletions test/test-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,141 @@ var sinon = require('sinon');
describe('Client', function() {
describe('reconnection', function() {
context('on connection interruption', function() {
var clientConfig = {retryDelay: 50, autoConnect: false};
var clientConfig = {retryDelay: 50, autoConnect: false, millisecondsOfSilenceBeforePingSent: 100, millisecondsBeforePingTimeout: 200};
var metaConfig = {callbackEarly: true, autoGreet: false};
testHelpers.hookMockSetup(beforeEach, afterEach, {client: clientConfig, meta: metaConfig});

function sharedExample(callback) {
it('reconnects with exactly one connection at a time', function(done) {
var mock = this.mock;
var client = this.client;
var registeredSpy = sinon.spy();
client.on('registered', registeredSpy);

var conns = [];
mock.server.on('connection', function(c) {
conns.push(c);
mock.greet();
context('with reconnecting client', function() {
testHelpers.hookMockSetup(beforeEach, afterEach, {client: clientConfig, meta: metaConfig});

it('reconnects with exactly one connection at a time', function(done) {
var mock = this.mock;
var client = this.client;
var registeredSpy = sinon.spy();
client.on('registered', registeredSpy);
var debugStub = sinon.stub(client.out, 'debug');

var conns = [];
mock.server.on('connection', function(c) {
conns.push(c);
mock.greet();
});

client.once('registered', function() {
setTimeout(function() {
callback(client, conns);
}, 100);
setTimeout(teardown, 400);
});

client.connect();

function teardown() {
expect(registeredSpy.calledTwice).to.be.true;
expect(conns.length).to.equal(2);
expect(conns[0].destroyed).to.be.true;
expect(debugStub.args).to.deep.include(['Waiting 50ms before retrying']);
done();
}
});
});

context('with opt.retryCount', function() {
testHelpers.hookMockSetup(beforeEach, afterEach, {client: Object.assign({retryCount: 1}, clientConfig), meta: metaConfig});

it('retries when disconnected', function(done) {
var self = this, mock = self.mock, client = self.client;
var registerSpy = sinon.spy();
client.on('registered', registerSpy);
var debugStub = sinon.stub(client.out, 'debug');

var conns = [];
mock.server.on('connection', function(c) {
conns.push(c);
mock.greet();
});

client.once('registered', function() {
setTimeout(function() {
callback(client, conns);
}, 100);
setTimeout(teardown, 400);
});

client.connect();

client.once('registered', function() {
callback(client, conns);
setTimeout(teardown, 500);
function teardown() {
expect(registerSpy.callCount).to.equal(2);
expect(conns.length).to.equal(2);
expect(conns[0].destroyed).to.be.true;
expect(debugStub.args).to.deep.include(['Waiting 50ms before retrying']);
done();
}
});

client.connect();
it('retries only once', function(done) {
var self = this, mock = self.mock, client = self.client;
var registerSpy = sinon.spy();
client.on('registered', registerSpy);
var debugStub = sinon.stub(client.out, 'debug');

function teardown() {
expect(registeredSpy.calledTwice).to.be.true;
expect(conns.length).to.equal(2);
expect(conns[0].destroyed).to.be.true;
done();
}
var conns = [];
mock.server.on('connection', function(c) {
conns.push(c);
mock.greet();
});

client.once('registered', function() {
setTimeout(function() {
callback(client, conns);
}, 100);
setTimeout(function() {
callback(client, conns);
}, 200);
setTimeout(teardown, 400);
});

client.connect();

function teardown() {
expect(registerSpy.callCount).to.equal(2);
expect(conns.length).to.equal(2);
expect(conns[0].destroyed).to.be.true;
expect(debugStub.args).to.deep.include(['Maximum retry count (1) reached. Aborting']);
done();
}
});

it('obeys first parameter to Client.connect', function(done) {
// doesn't reconnect
var self = this, mock = self.mock, client = self.client;
var registerSpy = sinon.spy();
client.on('registered', registerSpy);
var debugStub = sinon.stub(client.out, 'debug');

var conns = [];
mock.server.on('connection', function(c) {
conns.push(c);
mock.greet();
});

client.once('registered', function() {
setTimeout(function() {
callback(client, conns);
}, 100);
setTimeout(teardown, 400);
});

client.connect(1);

function teardown() {
expect(registerSpy.callCount).to.equal(1);
expect(conns.length).to.equal(1);
expect(conns[0].destroyed).to.be.true;
expect(debugStub.args).to.deep.include(['Maximum retry count (1) reached. Aborting']);
done();
}
});
});
}

Expand Down

0 comments on commit f01915f

Please sign in to comment.