Skip to content

Commit

Permalink
FKDEV-363: mq reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gab3sz committed Nov 24, 2022
1 parent 60bba1c commit 0ad60ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/QueueConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class QueueConnection {
this._logger.error('RabbitMQ error', err)
}
})
conn.on('close', () => this._onClose())
conn.on('close', this._onClose)
conn.on('blocked', (reason) => {
this._logger.error('RabbitMQ blocked', reason)
})
Expand Down Expand Up @@ -119,9 +119,9 @@ class QueueConnection {
/**
* @return Promise
* */
close (invokeCloseEvent = false) {
close (handleCloseEvent = false) {
if (this._connection) {
if (!invokeCloseEvent) {
if (!handleCloseEvent) {
this._connection.off('close', this._onClose)
}

Expand Down
47 changes: 47 additions & 0 deletions test/ConnectionPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,51 @@ describe('ConnectionPool', () => {
done()
})
})

it('Should reconnect', (done) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
default: config
})
Promise.resolve().then(() => {
return pool.connect()
}).then(() => {
return pool.reconnect()
}).then(() => {
done()
}).catch((err) => {
done(err)
})
})

it('Should not reconnect to wrong config', (done) => {
const pool = new ConnectionPool()
pool.setLogger(logger)
pool.setupQueueManagers({
default: config
})
Promise.resolve().then(() => {
return pool.connect()
}).then(() => {
pool.setupQueueManagers({
default: new QueueConfig({
url: 'amqps://localhost:22',
rpcTimeoutMs: 10000,
rpcQueueMaxSize: 100,
logger,
options: {
timeout: 50
}
})
})

return pool.reconnect()
}).then(() => {
done('Reconnection with wrong config should not connect')
}).catch((err) => {
assert.instanceOf(err, Error, 'Reconnection with wrong config should throw an error ')
done()
})
})
})
18 changes: 18 additions & 0 deletions test/QueueConnection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,22 @@ describe('QueueConnection', () => {
await connection.connect()
assert.strictEqual(connection._activeConnectionConfig.hostname, url.hostname)
})

it('#close() closes connection to RabbitMQ', async () => {
const connection = new QueueConnection(config)
try {
await connection.connect()
} catch (e) {
throw new Error(`connect() failed: ${e}`)
}

try {
await connection.close()
} catch (e) {
throw new Error(`close() failed: ${e}`)
}

assert.strictEqual(connection._connection, null)
assert.strictEqual(connection._connectionPromise, null)
})
})
17 changes: 0 additions & 17 deletions test/config/TestConfig.js.config

This file was deleted.

0 comments on commit 0ad60ec

Please sign in to comment.