Skip to content

Commit 8ea29bb

Browse files
Use pg_isready as healthcheck
1 parent 2b92f98 commit 8ea29bb

File tree

5 files changed

+9
-29
lines changed

5 files changed

+9
-29
lines changed

docs/features/wait-strategies.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,3 @@ const container = await new GenericContainer("alpine")
256256
.withWaitStrategy(new ReadyAfterDelayWaitStrategy())
257257
.start();
258258
```
259-
260-
## Restart wait strategies
261-
262-
The default wait strategy used when a container restarts is the same as the one used when it starts. However sometimes a container behaves differently between its first start and subsequent starts. Postgres is one such example. For this reason you can specify a wait strategy specifically used for restarts:
263-
264-
```javascript
265-
const container = await new GenericContainer("postgres")
266-
.withWaitStrategy(Wait.forLogMessage(/.*database system is ready to accept connections.*/, 2))
267-
.withRestartWaitStrategy(Wait.forLogMessage(/.*database system is ready to accept connections.*/, 1))
268-
.start();
269-
```

packages/modules/postgresql/src/postgresql-container.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ export class PostgreSqlContainer extends GenericContainer {
1010
constructor(image = "postgres:13.3-alpine") {
1111
super(image);
1212
this.withExposedPorts(POSTGRES_PORT);
13-
this.withWaitStrategy(
14-
Wait.forAll([Wait.forHealthCheck(), Wait.forLogMessage(/.*database system is ready to accept connections.*/, 2)])
15-
);
16-
this.withRestartWaitStrategy(
17-
Wait.forAll([Wait.forHealthCheck(), Wait.forLogMessage(/.*database system is ready to accept connections.*/, 1)])
18-
);
13+
this.withWaitStrategy(Wait.forHealthCheck());
1914
this.withStartupTimeout(120_000);
2015
}
2116

@@ -42,7 +37,10 @@ export class PostgreSqlContainer extends GenericContainer {
4237
});
4338
if (!this.healthCheck) {
4439
this.withHealthCheck({
45-
test: ["CMD-SHELL", `PGPASSWORD=${this.password} psql -U ${this.username} -d ${this.database} -c 'SELECT 1;'`],
40+
test: [
41+
"CMD-SHELL",
42+
`PGPASSWORD=${this.password} pg_isready --host localhost --username ${this.username} --dbname ${this.database}`,
43+
],
4644
interval: 250,
4745
timeout: 1000,
4846
retries: 1000,

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export class GenericContainer implements TestContainer {
4848
protected imageName: ImageName;
4949
protected startupTimeout?: number;
5050
protected waitStrategy: WaitStrategy = Wait.forListeningPorts();
51-
protected restartWaitStrategy?: WaitStrategy;
5251
protected environment: Record<string, string> = {};
5352
protected exposedPorts: PortWithOptionalBinding[] = [];
5453
protected reuse = false;
@@ -162,7 +161,7 @@ export class GenericContainer implements TestContainer {
162161
inspectResult,
163162
boundPorts,
164163
inspectResult.Name,
165-
this.restartWaitStrategy ?? this.waitStrategy,
164+
this.waitStrategy,
166165
this.autoRemove
167166
);
168167
}
@@ -231,7 +230,7 @@ export class GenericContainer implements TestContainer {
231230
inspectResult,
232231
boundPorts,
233232
inspectResult.Name,
234-
this.restartWaitStrategy ?? this.waitStrategy,
233+
this.waitStrategy,
235234
this.autoRemove
236235
);
237236

@@ -420,11 +419,6 @@ export class GenericContainer implements TestContainer {
420419
return this;
421420
}
422421

423-
public withRestartWaitStrategy(waitStrategy: WaitStrategy): this {
424-
this.restartWaitStrategy = waitStrategy;
425-
return this;
426-
}
427-
428422
public withDefaultLogDriver(): this {
429423
this.hostConfig.LogConfig = {
430424
Type: "json-file",

packages/testcontainers/src/generic-container/started-generic-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class StartedGenericContainer implements StartedTestContainer {
2424
private inspectResult: ContainerInspectInfo,
2525
private boundPorts: BoundPorts,
2626
private readonly name: string,
27-
private readonly restartWaitStrategy: WaitStrategy,
27+
private readonly waitStrategy: WaitStrategy,
2828
private readonly autoRemove: boolean
2929
) {}
3030

@@ -94,7 +94,7 @@ export class StartedGenericContainer implements StartedTestContainer {
9494
Array.from(this.boundPorts.iterator()).map((port) => port[0])
9595
);
9696

97-
await waitForContainer(client, this.container, this.restartWaitStrategy, this.boundPorts, startTime);
97+
await waitForContainer(client, this.container, this.waitStrategy, this.boundPorts, startTime);
9898
log.info(`Restarted container`, { containerId: this.container.id });
9999
}
100100

packages/testcontainers/src/test-container.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export interface TestContainer {
3232
withExposedPorts(...ports: PortWithOptionalBinding[]): this;
3333
withBindMounts(bindMounts: BindMount[]): this;
3434
withWaitStrategy(waitStrategy: WaitStrategy): this;
35-
withRestartWaitStrategy(waitStrategy: WaitStrategy): this;
3635
withStartupTimeout(startupTimeoutMs: number): this;
3736
withNetwork(network: StartedNetwork): this;
3837
withNetworkMode(networkMode: string): this;

0 commit comments

Comments
 (0)