Skip to content

Commit 75c00c3

Browse files
Nats
1 parent 0e9fc5d commit 75c00c3

File tree

2 files changed

+33
-57
lines changed

2 files changed

+33
-57
lines changed

docs/modules/nats.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Nats Module
2-
3-
[NATS](https://nats.io/) is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures.
1+
# Nats
42

53
## Install
64

@@ -10,18 +8,32 @@ npm install @testcontainers/nats --save-dev
108

119
## Examples
1210

13-
<!--codeinclude-->
14-
[Connect:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:connect
15-
<!--/codeinclude-->
11+
These examples use the following libraries:
12+
13+
- [@nats-io/transport-node](https://www.npmjs.com/package/@nats-io/transport-node)
14+
15+
npm install @nats-io/transport-node
16+
17+
- [@nats-io/jetstream](https://www.npmjs.com/package/@nats-io/jetstream)
18+
19+
npm install @nats-io/jetstream
20+
21+
Choose an image from the [container registry](https://hub.docker.com/_/nats) and substitute `IMAGE`.
22+
23+
### Produce/consume a message
1624

1725
<!--codeinclude-->
18-
[Publish and subscribe:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:pubsub
26+
[](../../packages/modules/nats/src/nats-container.test.ts) inside_block:natsPubsub
1927
<!--/codeinclude-->
2028

29+
### With credentials
30+
2131
<!--codeinclude-->
22-
[Set credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:credentials
32+
[](../../packages/modules/nats/src/nats-container.test.ts) inside_block:natsCredentials
2333
<!--/codeinclude-->
2434

35+
### With Jetstream
36+
2537
<!--codeinclude-->
26-
[Enable JetStream:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:jetstream
27-
<!--/codeinclude-->
38+
[](../../packages/modules/nats/src/nats-container.test.ts) inside_block:natsJetstream
39+
<!--/codeinclude-->

packages/modules/nats/src/nats-container.test.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,53 @@ import { NatsContainer } from "./nats-container";
66
const IMAGE = getImage(__dirname);
77

88
describe("NatsContainer", { timeout: 180_000 }, () => {
9-
// connect {
10-
it("should start, connect and close", async () => {
11-
await using container = await new NatsContainer(IMAGE).start();
12-
13-
// establish connection
14-
const nc = await connect(container.getConnectionOptions());
15-
// close the connection
16-
await nc.close();
17-
// check if the close was OK
18-
const err = await nc.closed();
19-
expect(err).toBe(undefined);
20-
});
21-
// }
22-
23-
it("should start, connect and close using scratch image", async () => {
24-
await using container = await new NatsContainer("nats:2.11").start();
25-
26-
// establish connection
27-
const nc = await connect(container.getConnectionOptions());
28-
// close the connection
29-
await nc.close();
30-
// check if the close was OK
31-
const err = await nc.closed();
32-
expect(err).toBe(undefined);
33-
});
34-
35-
// pubsub {
369
it("should subscribe and receive one published message", async () => {
10+
// natsPubsub {
3711
const SUBJECT = "HELLO";
3812
const PAYLOAD = "WORLD";
13+
const TE = new TextEncoder();
14+
const TD = new TextDecoder();
3915

4016
await using container = await new NatsContainer(IMAGE).start();
4117
const nc = await connect(container.getConnectionOptions());
42-
const TE = new TextEncoder();
43-
const TD = new TextDecoder();
4418

45-
//----------------
4619
const sub = nc.subscribe(SUBJECT);
20+
4721
(async () => {
4822
for await (const m of sub) {
4923
const actual = TD.decode(m.data);
5024
expect(actual).toEqual(PAYLOAD);
5125
}
5226
})().then();
5327

54-
//----------------
5528
nc.publish(SUBJECT, TE.encode(PAYLOAD));
5629

57-
//----------------
5830
await nc.drain();
5931
await nc.close();
60-
const err = await nc.closed();
61-
expect(err).toBe(undefined);
32+
// }
6233
});
63-
// }
6434

65-
// credentials {
6635
it("should start with alternative username and password ", async () => {
67-
// set username and password like this
68-
await using container = await new NatsContainer(IMAGE).withPass("1234").withUsername("George").start();
36+
// natsCredentials {
37+
await using container = await new NatsContainer(IMAGE).withUsername("George").withPass("1234").start();
38+
// }
6939

7040
const nc = await connect(container.getConnectionOptions());
71-
// close the connection
41+
7242
await nc.close();
73-
// check if the close was OK
7443
const err = await nc.closed();
7544
expect(err).toBe(undefined);
7645
});
77-
// }
7846

79-
// jetstream {
8047
it("should start with JetStream ", async () => {
81-
// enable JetStream
48+
// natsJetstream {
8249
await using container = await new NatsContainer(IMAGE).withJetStream().start();
50+
// }
8351

8452
const nc = await connect(container.getConnectionOptions());
85-
86-
// ensure JetStream is enabled, otherwise this will throw an error
8753
await jetstreamManager(nc);
8854

89-
// close the connection
9055
await nc.close();
91-
// check if the close was OK
9256
const err = await nc.closed();
9357
expect(err).toBe(undefined);
9458
});

0 commit comments

Comments
 (0)