From f01915f0687dac61d31fa55ec8c8706a41821ff5 Mon Sep 17 00:00:00 2001 From: Edward Jones Date: Fri, 29 Sep 2017 18:01:41 +0100 Subject: [PATCH] Test opt.retryCount --- lib/irc.js | 2 +- test/test-api.js | 2 +- test/test-reconnect.js | 149 +++++++++++++++++++++++++++++++++++------ 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/lib/irc.js b/lib/irc.js index 58253a6f..2f569c62 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -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) { @@ -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); diff --git a/test/test-api.js b/test/test-api.js index b708fa4e..cac28f1f 100644 --- a/test/test-api.js +++ b/test/test-api.js @@ -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() { diff --git a/test/test-reconnect.js b/test/test-reconnect.js index 3adddf1c..f332f914 100644 --- a/test/test-reconnect.js +++ b/test/test-reconnect.js @@ -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(); + } + }); }); }