Skip to content

Commit 8c53ffe

Browse files
committed
Used shared EventStoreDB container in tests
Made it also reusable and waiting for healthcheck
1 parent 6d3940b commit 8c53ffe

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/packages/emmett-testcontainers/src/eventStore/eventStoreDBContainer.e2e.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { randomUUID } from 'node:crypto';
44
import { describe, it } from 'node:test';
55
import {
66
EventStoreDBContainer,
7-
getEventStoreDBTestContainer,
8-
releaseShartedEventStoreDBTestContainer,
7+
getSharedEventStoreDBTestContainer,
8+
releaseSharedEventStoreDBTestContainer,
99
} from './eventStoreDBContainer';
1010

1111
void describe('EventStoreDBContainer', () => {
@@ -27,7 +27,7 @@ void describe('EventStoreDBContainer', () => {
2727
});
2828

2929
void it('should connect to shared EventStoreDB and append new event', async () => {
30-
const container = await getEventStoreDBTestContainer();
30+
const container = await getSharedEventStoreDBTestContainer();
3131

3232
try {
3333
const client = container.getClient();
@@ -39,15 +39,15 @@ void describe('EventStoreDBContainer', () => {
3939

4040
assertOk(result.success);
4141
} finally {
42-
await releaseShartedEventStoreDBTestContainer();
42+
await releaseSharedEventStoreDBTestContainer();
4343
}
4444
});
4545

4646
void it('should connect to multiple shared EventStoreDB and append new event', async () => {
4747
const containers = [
48-
await getEventStoreDBTestContainer(),
49-
await getEventStoreDBTestContainer(),
50-
await getEventStoreDBTestContainer(),
48+
await getSharedEventStoreDBTestContainer(),
49+
await getSharedEventStoreDBTestContainer(),
50+
await getSharedEventStoreDBTestContainer(),
5151
];
5252

5353
try {
@@ -62,7 +62,7 @@ void describe('EventStoreDBContainer', () => {
6262
assertOk(result.success);
6363
} finally {
6464
for (let i = 0; i < containers.length; i++) {
65-
await releaseShartedEventStoreDBTestContainer();
65+
await releaseSharedEventStoreDBTestContainer();
6666
}
6767
}
6868
});

src/packages/emmett-testcontainers/src/eventStore/eventStoreDBContainer.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { EventStoreDBClient } from '@eventstore/db-client';
22
import {
33
AbstractStartedContainer,
44
GenericContainer,
5+
Wait,
56
type StartedTestContainer,
67
} from 'testcontainers';
78
import type { Environment } from 'testcontainers/build/types';
89

910
export const EVENTSTOREDB_PORT = 2113;
1011
export const EVENTSTOREDB_IMAGE_NAME = 'eventstore/eventstore';
11-
export const EVENTSTOREDB_IMAGE_TAG = '24.10.0-bookworm-slim';
12-
export const EVENTSTOREDB_ARM64_IMAGE_TAG = '24.10.0-alpha-arm64v8';
12+
export const EVENTSTOREDB_IMAGE_TAG = '23.10.1-bookworm-slim';
13+
export const EVENTSTOREDB_ARM64_IMAGE_TAG = '23.10.1-alpha-arm64v8';
1314

1415
export const EVENTSTOREDB_DEFAULT_IMAGE = `${EVENTSTOREDB_IMAGE_NAME}:${process.arch !== 'arm64' ? EVENTSTOREDB_IMAGE_TAG : EVENTSTOREDB_ARM64_IMAGE_TAG}`;
1516

@@ -61,6 +62,10 @@ export class EventStoreDBContainer extends GenericContainer {
6162
this.withEnvironment(environment).withExposedPorts(EVENTSTOREDB_PORT);
6263

6364
if (options.withReuse) this.withReuse();
65+
66+
this.withWaitStrategy(
67+
Wait.forAll([Wait.forHealthCheck(), Wait.forListeningPorts()]),
68+
);
6469
}
6570

6671
async start(): Promise<StartedEventStoreDBContainer> {
@@ -85,29 +90,40 @@ let container: EventStoreDBContainer | null = null;
8590
let startedContainer: StartedEventStoreDBContainer | null = null;
8691
let startedCount = 0;
8792

88-
export const getEventStoreDBTestContainer = async () => {
93+
export const getSharedEventStoreDBTestContainer = async () => {
8994
if (startedContainer) return startedContainer;
9095

9196
if (!container)
92-
container = new EventStoreDBContainer(EVENTSTOREDB_DEFAULT_IMAGE);
97+
container = new EventStoreDBContainer(EVENTSTOREDB_DEFAULT_IMAGE, {
98+
withReuse: true,
99+
});
93100

94101
startedContainer = await container.start();
95102
startedCount++;
96103

104+
container.withLogConsumer((stream) =>
105+
stream
106+
.on('data', (line) => console.log(line))
107+
.on('err', (line) => console.error(line))
108+
.on('end', () => console.log('Stream closed')),
109+
);
110+
97111
return startedContainer;
98112
};
99113

100114
export const getSharedTestEventStoreDBClient = async () => {
101-
return (await getEventStoreDBTestContainer()).getClient();
115+
return (await getSharedEventStoreDBTestContainer()).getClient();
102116
};
103117

104-
export const releaseShartedEventStoreDBTestContainer = async () => {
105-
if (startedContainer && --startedCount === 0)
118+
export const releaseSharedEventStoreDBTestContainer = async () => {
119+
const containerToStop = startedContainer;
120+
if (containerToStop && --startedCount === 0) {
106121
try {
107-
await startedContainer.stop();
122+
startedContainer = null;
123+
container = null;
124+
await containerToStop.stop();
108125
} catch {
109126
/* do nothing */
110127
}
111-
container = null;
112-
startedContainer = null;
128+
}
113129
};

0 commit comments

Comments
 (0)