From 5864705ef956fe69b8764497477e16a04dc421b9 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Tue, 5 Jan 2016 12:48:42 -0800 Subject: [PATCH] Update connection tests --- lib/common/connection.js | 3 +- spec/lib/common/connection-spec.js | 76 +++++++++++++----------------- 2 files changed, 34 insertions(+), 45 deletions(-) diff --git a/lib/common/connection.js b/lib/common/connection.js index 5d02cdaf..94719306 100644 --- a/lib/common/connection.js +++ b/lib/common/connection.js @@ -27,6 +27,7 @@ function connectionFactory (assert, amqp, Promise, util, _) { this.label = label; this.initialConnection = false; this.initialConnectionRetries = 0; + this.maxConnectionRetries = 60; Object.defineProperty(this, 'exchanges', { get: function () { @@ -74,7 +75,7 @@ function connectionFactory (assert, amqp, Promise, util, _) { console.log('AMQP %s Error (%s)'.format(self.label, self.options.url), error.message); } else { - if (self.initialConnectionRetries > 60) { + if (self.initialConnectionRetries > self.maxConnectionRetries) { reject(new Error( 'Exceeded max retries attempting to start AMQP connection to ' + self.options.url)); diff --git a/spec/lib/common/connection-spec.js b/spec/lib/common/connection-spec.js index 5295b750..fd79981a 100644 --- a/spec/lib/common/connection-spec.js +++ b/spec/lib/common/connection-spec.js @@ -15,8 +15,8 @@ describe('Connection', function () { beforeEach(function () { this.subject = new Connection({ - uri: this.uri - }); + url: this.uri + }, {}, 'test'); }); afterEach(function () { @@ -40,6 +40,36 @@ describe('Connection', function () { return self.subject.start().should.be.rejected; }); }); + + it('should reject on max retry attempts', function() { + this.subject.maxConnectionRetries = 3; + var startPromise = this.subject.start(); + this.subject.connection.removeAllListeners('ready'); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + return expect(startPromise).to.be.rejectedWith(/Exceeded max retries/); + }); + + it('should increment retry attempts if initial connection fails', function() { + this.subject.initialConnection = false; + this.subject.start(); + this.subject.connection.removeAllListeners('ready'); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + expect(this.subject.initialConnectionRetries).to.equal(3); + }); + + it('should not increment retry attempts if initial connection succeeded', function() { + this.subject.initialConnection = true; + this.subject.start(); + this.subject.connection.removeAllListeners('ready'); + this.subject.connection.emit('error', new Error('test')); + this.subject.connection.emit('error', new Error('test')); + expect(this.subject.initialConnectionRetries).to.equal(0); + }); }); describe('connected', function () { @@ -83,47 +113,5 @@ describe('Connection', function () { return this.subject.stop().should.be.rejected; }); }); - - describe('errors', function() { - it('should emit errors from the underlying connection', function(done) { - var self = this; - - this.subject.once('error', function (error) { - try { - error.should.be.an.instanceof(Error); - error.message.should.be.equal('Fake'); - done(); - } catch(e) { - done(e); - } - }); - - return this.subject.start().then(function () { - self.subject.connection.emit('error', new Error('Fake')); - }); - }); - - it('should not emit ECONNRESET errors from the underlying connection', function(done) { - var self = this; - - this.subject.once('error', function (error) { - try { - error.should.be.an.instanceof(Error); - error.message.should.be.equal('Fake'); - done(); - } catch(e) { - done(e); - } - }); - - return this.subject.start().then(function () { - var error = new Error(); - error.code = 'ECONNRESET'; - - self.subject.connection.emit('error', error); - self.subject.connection.emit('error', new Error('Fake')); - }); - }); - }); }); });