Skip to content

Commit

Permalink
Update connection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp committed Jan 5, 2016
1 parent f927ff7 commit 5864705
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
3 changes: 2 additions & 1 deletion lib/common/connection.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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));
Expand Down
76 changes: 32 additions & 44 deletions spec/lib/common/connection-spec.js
Expand Up @@ -15,8 +15,8 @@ describe('Connection', function () {

beforeEach(function () {
this.subject = new Connection({
uri: this.uri
});
url: this.uri
}, {}, 'test');
});

afterEach(function () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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'));
});
});
});
});
});

0 comments on commit 5864705

Please sign in to comment.