Skip to content

Commit c02ea69

Browse files
committed
Restored original message as that was not meant to have changed. Added test to for this case to LogWaitrStrategy
1 parent 2fcd711 commit c02ea69

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/testcontainers/src/wait-strategies/log-wait-strategy.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GenericContainer } from "../generic-container/generic-container";
33
import { checkContainerIsHealthy, getRunningContainerNames } from "../utils/test-helper";
44
import { Wait } from "./wait";
55

6-
describe("LogWaitStrategy", { timeout: 180_000 }, () => {
6+
describe("LogWaitStrategy", { timeout: 5_000 }, () => {
77
it("should wait for log", async () => {
88
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
99
.withExposedPorts(8080)
@@ -54,4 +54,19 @@ describe("LogWaitStrategy", { timeout: 180_000 }, () => {
5454

5555
expect(await getRunningContainerNames()).not.toContain(containerName);
5656
});
57+
58+
59+
it("should throw an error if the message is never received", async () => {
60+
const containerName = `container-${new RandomUuid().nextUuid()}`;
61+
62+
await expect(
63+
new GenericContainer("cristianrgreco/testcontainer:1.1.14")
64+
.withName(containerName)
65+
.withCommand("/bin/sh", "-c", 'echo "Ready"')
66+
.withWaitStrategy(Wait.forLogMessage("unexpected"))
67+
.start()
68+
).rejects.toThrowError(`Log stream ended and message "unexpected" was not received`);
69+
70+
expect(await getRunningContainerNames()).not.toContain(containerName);
71+
});
5772
});

packages/testcontainers/src/wait-strategies/log-wait-strategy.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Log = string;
1010
export class LogWaitStrategy extends AbstractWaitStrategy {
1111
constructor(
1212
private readonly message: Log | RegExp,
13-
private readonly times: number
13+
private readonly times: number,
1414
) {
1515
super();
1616
}
@@ -21,9 +21,7 @@ export class LogWaitStrategy extends AbstractWaitStrategy {
2121

2222
async handleTimeout(containerId: string): Promise<void> {
2323
await setTimeout(this.startupTimeout);
24-
const message = `Log message "${this.message}" not received after ${this.startupTimeout}ms`;
25-
log.error(message, { containerId });
26-
throw new Error(message);
24+
this.throwError(containerId, `Log message "${this.message}" not received after ${this.startupTimeout}ms`);
2725
}
2826

2927
async handleLogs(container: Dockerode.Container, startTime?: Date): Promise<void> {
@@ -40,10 +38,15 @@ export class LogWaitStrategy extends AbstractWaitStrategy {
4038
}
4139
}
4240

43-
throw new Error("Log message not found");
41+
this.throwError(container.id, `Log stream ended and message "${this.message}" was not received`);
4442
}
4543

4644
matches(chunk: string): boolean {
4745
return this.message instanceof RegExp ? this.message.test(chunk) : chunk.includes(this.message);
4846
}
49-
}
47+
48+
throwError(containerId: string, message: string): void {
49+
log.error(message, { containerId });
50+
throw new Error(message);
51+
}
52+
}

0 commit comments

Comments
 (0)