diff --git a/package-lock.json b/package-lock.json index 3292107..4ba0419 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2315,6 +2315,16 @@ } } }, + "cross-env": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-5.2.0.tgz", + "integrity": "sha512-jtdNFfFW1hB7sMhr/H6rW1Z45LFqyI431m3qU6bFXcQ3Eh7LtBuG3h74o7ohHZ3crrRkkqHlo4jYHFPcjroANg==", + "dev": true, + "requires": { + "cross-spawn": "^6.0.5", + "is-windows": "^1.0.0" + } + }, "cross-spawn": { "version": "6.0.5", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", diff --git a/package.json b/package.json index 974fe50..4c72baf 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "chai-as-promised": "^7.1.1", "chai-string": "^1.1.2", "coveralls": "^3.0.0", + "cross-env": "^5.2.0", "eslint": "^5.14.1", "eslint-config-benbria": "^4.0.0", "eslint-plugin-import": "^2.11.0", @@ -47,9 +48,9 @@ "build": "babel -s -d lib src", "clean": "rm -rf lib coverage", "test": "npm run test:lint && npm run test:unittest", - "test:unittest": "NODE_ENV=test nyc mocha", + "test:unittest": "cross-env NODE_ENV=test nyc mocha", "test:lint": "eslint src test", - "precommit:unittest": "BABEL_DISABLE_CACHE=1 NODE_ENV=test nyc mocha --reporter progress", + "precommit:unittest": "cross-env BABEL_DISABLE_CACHE=1 cross-env NODE_ENV=test nyc mocha --reporter progress", "semantic-release": "semantic-release" }, "husky": { diff --git a/src/AmqpConnectionManager.js b/src/AmqpConnectionManager.js index 0a8d925..d71a255 100644 --- a/src/AmqpConnectionManager.js +++ b/src/AmqpConnectionManager.js @@ -148,21 +148,15 @@ export default class AmqpConnectionManager extends EventEmitter { connection.on('unblocked', () => this.emit('unblocked')); - // Reconnect if the broker goes away. - connection.on('error', err => - Promise.resolve() - .then(() =>this._currentConnection.close()) - .catch(() => { /* Ignore */ }) - .then(() => { - this._currentConnection = null; - this.emit('disconnect', { err }); - return this._connect(); - }) - // `_connect()` should never throw. - .catch(neverThrows) - ); + connection.on('error', (/* err */) => { + // if this event was emitted, then the connection was already closed, + // so no need to call #close here + // also, 'close' is emitted after 'error', + // so no need for work already done in 'close' handler + return Promise.resolve(); + }); - // Reconnect if the connection closes gracefully + // Reconnect if the connection closes connection.on('close', err => { this._currentConnection = null; this.emit('disconnect', { err }); diff --git a/test/fixtures.js b/test/fixtures.js index 78c5bec..a7c3796 100644 --- a/test/fixtures.js +++ b/test/fixtures.js @@ -5,7 +5,9 @@ export class FakeAmqp { constructor() { this.reset(); } kill() { - this.connection.emit('error', new Error("Died in a fire")); + const err = new Error("Died in a fire"); + this.connection.emit('error', err); + this.connection.emit('close', err); } simulateRemoteClose() {