Skip to content

Commit e5e772b

Browse files
authored
Add RabbitMQ module (#770)
1 parent 441743c commit e5e772b

File tree

10 files changed

+327
-0
lines changed

10 files changed

+327
-0
lines changed

docs/modules/rabbitmq.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# RabbitMQ Module
2+
3+
[RabbitMQ](https://www.rabbitmq.com/) is a reliable and mature messaging and streaming broker, which is easy to deploy on cloud environments, on-premises, and on your local machine. It is currently used by millions worldwide.
4+
5+
## Install
6+
7+
```bash
8+
npm install @testcontainers/rabbitmq --save-dev
9+
```
10+
11+
## Examples
12+
13+
<!--codeinclude-->
14+
[Connect:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:start
15+
<!--/codeinclude-->
16+
17+
<!--codeinclude-->
18+
[Set credentials:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:credentials
19+
<!--/codeinclude-->
20+
21+
<!--codeinclude-->
22+
[Publish and subscribe:](../../packages/modules/rabbitmq/src/rabbitmq-container.test.ts) inside_block:pubsub
23+
<!--/codeinclude-->

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ nav:
5757
- Neo4J: modules/neo4j.md
5858
- PostgreSQL: modules/postgresql.md
5959
- Qdrant: modules/qdrant.md
60+
- RabbitMQ: modules/rabbitmq.md
6061
- Redis: modules/redis.md
6162
- Redpanda: modules/redpanda.md
6263
- Selenium: modules/selenium.md

package-lock.json

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Config } from "jest";
2+
import * as path from "path";
3+
4+
const config: Config = {
5+
preset: "ts-jest",
6+
moduleNameMapper: {
7+
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"),
8+
},
9+
};
10+
11+
export default config;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@testcontainers/rabbitmq",
3+
"version": "10.9.0",
4+
"license": "MIT",
5+
"keywords": [
6+
"rabbitmq",
7+
"testing",
8+
"docker",
9+
"testcontainers"
10+
],
11+
"description": "RabbitMQ module for Testcontainers",
12+
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/testcontainers/testcontainers-node"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/testcontainers/testcontainers-node/issues"
19+
},
20+
"main": "build/index.js",
21+
"files": [
22+
"build"
23+
],
24+
"publishConfig": {
25+
"access": "public"
26+
},
27+
"scripts": {
28+
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
29+
"build": "tsc --project tsconfig.build.json"
30+
},
31+
"dependencies": {
32+
"testcontainers": "^10.9.0"
33+
},
34+
"devDependencies": {
35+
"@types/amqplib": "^0.10.4",
36+
"amqplib": "^0.10.4"
37+
}
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { RabbitMQContainer, StartedRabbitMQContainer } from "./rabbitmq-container";
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import amqp from "amqplib";
2+
import { RabbitMQContainer } from "./rabbitmq-container";
3+
4+
describe("RabbitMQContainer", () => {
5+
jest.setTimeout(240_000);
6+
7+
// start {
8+
it("should start, connect and close", async () => {
9+
const rabbitMQContainer = await new RabbitMQContainer().start();
10+
11+
const connection = await amqp.connect(rabbitMQContainer.getAmqpUrl());
12+
await connection.close();
13+
14+
await rabbitMQContainer.stop();
15+
});
16+
// }
17+
18+
// credentials {
19+
it("different username and password", async () => {
20+
const USER = "user";
21+
const PASSWORD = "password";
22+
23+
const rabbitMQContainer = await new RabbitMQContainer()
24+
.withEnvironment({
25+
RABBITMQ_DEFAULT_USER: USER,
26+
RABBITMQ_DEFAULT_PASS: PASSWORD,
27+
})
28+
.start();
29+
30+
const connection = await amqp.connect({
31+
username: USER,
32+
password: PASSWORD,
33+
port: rabbitMQContainer.getMappedPort(5672),
34+
});
35+
36+
await connection.close();
37+
38+
await rabbitMQContainer.stop();
39+
});
40+
// }
41+
42+
// pubsub {
43+
it("test publish and subscribe", async () => {
44+
const QUEUE = "test";
45+
const PAYLOAD = "Hello World";
46+
47+
const rabbitMQContainer = await new RabbitMQContainer().start();
48+
const connection = await amqp.connect(rabbitMQContainer.getAmqpUrl());
49+
50+
const channel = await connection.createChannel();
51+
await channel.assertQueue(QUEUE);
52+
53+
channel.sendToQueue(QUEUE, Buffer.from(PAYLOAD));
54+
55+
await new Promise((resolve) => {
56+
channel.consume(QUEUE, (message) => {
57+
expect(message?.content.toString()).toEqual(PAYLOAD);
58+
resolve(true);
59+
});
60+
});
61+
62+
await channel.close();
63+
await connection.close();
64+
65+
await rabbitMQContainer.stop();
66+
}, 10_000);
67+
// }
68+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";
2+
3+
const AMQP_PORT = 5672;
4+
const AMQPS_PORT = 5671;
5+
const HTTPS_PORT = 15671;
6+
const HTTP_PORT = 15672;
7+
const RABBITMQ_DEFAULT_USER = "guest";
8+
const RABBITMQ_DEFAULT_PASS = "guest";
9+
10+
export class RabbitMQContainer extends GenericContainer {
11+
constructor(image = "rabbitmq:3.12.11-management-alpine") {
12+
super(image);
13+
this.withExposedPorts(AMQP_PORT, AMQPS_PORT, HTTPS_PORT, HTTP_PORT)
14+
.withEnvironment({
15+
RABBITMQ_DEFAULT_USER,
16+
RABBITMQ_DEFAULT_PASS,
17+
})
18+
.withWaitStrategy(Wait.forLogMessage("Server startup complete"))
19+
.withStartupTimeout(30_000);
20+
}
21+
22+
public override async start(): Promise<StartedRabbitMQContainer> {
23+
return new StartedRabbitMQContainer(await super.start());
24+
}
25+
}
26+
27+
export class StartedRabbitMQContainer extends AbstractStartedContainer {
28+
constructor(startedTestContainer: StartedTestContainer) {
29+
super(startedTestContainer);
30+
}
31+
32+
public getAmqpUrl(): string {
33+
return `amqp://${this.getHost()}:${this.getMappedPort(AMQP_PORT)}`;
34+
}
35+
36+
public getAmqpsUrl(): string {
37+
return `amqps://${this.getHost()}:${this.getMappedPort(AMQPS_PORT)}`;
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": [
4+
"build",
5+
"jest.config.ts",
6+
"src/**/*.test.ts"
7+
],
8+
"references": [
9+
{
10+
"path": "../../testcontainers"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)