-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: '3.2' | ||
services: | ||
rabbitmq: | ||
image: rabbitmq:3-management-alpine | ||
container_name: 'rabbitmq' | ||
ports: | ||
- 5672:5672 | ||
- 15672:15672 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module.exports = { | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: { | ||
tsconfig: { | ||
target: 'es2019', | ||
}, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ConfirmChannel, ConsumeMessage } from 'amqplib'; | ||
import chai from 'chai'; | ||
import chaiJest from 'chai-jest'; | ||
import pEvent from 'p-event'; | ||
import { defer, timeout } from 'promise-tools'; | ||
import amqp from '../src'; | ||
|
||
chai.use(chaiJest); | ||
|
||
const { expect } = chai; | ||
|
||
/** | ||
* Tests in this file assume you have a RabbitMQ instance running on localhost. | ||
* You can start one with: | ||
* | ||
* docker-compose up -d | ||
* | ||
*/ | ||
describe('Integration tests', () => { | ||
beforeEach(() => { | ||
// TODO: Uncomment this if you're using `jest.spyOn()` to restore mocks between tests | ||
// jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should connect to the broker', async () => { | ||
// Create a new connection manager | ||
const connection = amqp.connect(['amqp://localhost']); | ||
await timeout(pEvent(connection, 'connect'), 3000); | ||
await connection.close(); | ||
}); | ||
|
||
it('send and receive messages', async () => { | ||
const queueName = 'testQueue1'; | ||
const content = `hello world - ${Date.now()}`; | ||
|
||
// Create a new connection manager | ||
const connection = amqp.connect(['amqp://localhost']); | ||
|
||
// Ask the connection manager for a ChannelWrapper. Specify a setup function to | ||
// run every time we reconnect to the broker. | ||
const sendChannel = connection.createChannel({ | ||
setup: async (channel: ConfirmChannel) => { | ||
await channel.assertQueue(queueName, { durable: false }); | ||
}, | ||
}); | ||
|
||
const rxPromise = defer<ConsumeMessage>(); | ||
|
||
const receiveWrapper = connection.createChannel({ | ||
setup: async (channel: ConfirmChannel) => { | ||
// `channel` here is a regular amqplib `ConfirmChannel`. | ||
// Note that `this` here is the channelWrapper instance. | ||
await channel.assertQueue(queueName, { durable: false }); | ||
await channel.consume( | ||
queueName, | ||
(message) => { | ||
if (!message) { | ||
// Ignore. | ||
} else if (message.content.toString() === content) { | ||
rxPromise.resolve(message); | ||
} else { | ||
console.log( | ||
`Received message ${message?.content.toString()} !== ${content}` | ||
); | ||
} | ||
}, | ||
{ | ||
noAck: true, | ||
} | ||
); | ||
}, | ||
}); | ||
|
||
await sendChannel.sendToQueue(queueName, content); | ||
|
||
const result = await timeout(rxPromise.promise, 3000); | ||
expect(result.content.toString()).to.equal(content); | ||
|
||
await sendChannel.close(); | ||
await receiveWrapper.close(); | ||
await connection.close(); | ||
}); | ||
}); |