Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ describe("LogWaitStrategy", { timeout: 180_000 }, () => {
expect(await getRunningContainerNames()).not.toContain(containerName);
});

it("should throw an error if the message is never received", async () => {
it("should throw an error if the log stream ends and the message is not received", async () => {
const containerName = `container-${new RandomUuid().nextUuid()}`;

await expect(
new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(containerName)
.withCommand("/bin/sh", "-c", 'echo "Ready"')
.withCommand(["/bin/sh", "-c", 'echo "Ready"'])
.withWaitStrategy(Wait.forLogMessage("unexpected"))
.start()
).rejects.toThrowError(`Log stream ended and message "unexpected" was not received`);
Expand Down
39 changes: 30 additions & 9 deletions packages/testcontainers/src/wait-strategies/log-wait-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,58 @@ export class LogWaitStrategy extends AbstractWaitStrategy {
super();
}

public async waitUntilReady(container: Dockerode.Container, boundPorts: BoundPorts, startTime?: Date): Promise<void> {
await Promise.race([this.handleTimeout(container.id), this.handleLogs(container, startTime)]);
async waitUntilReady(container: Dockerode.Container, boundPorts: BoundPorts, startTime?: Date): Promise<void> {
const abortController = new AbortController();

await Promise.race([
this.handleTimeout(container.id, abortController),
this.handleLogs(container, startTime ? startTime.getTime() / 1000 : 0, abortController),
]);
}

async handleTimeout(containerId: string): Promise<void> {
await setTimeout(this.startupTimeout);
private async handleTimeout(containerId: string, abortController: AbortController): Promise<void> {
try {
await setTimeout(this.startupTimeout, undefined, { signal: abortController.signal });
} catch (err) {
if (!(err instanceof Error && err.name === "AbortError")) {
throw err;
}
}
this.throwError(containerId, `Log message "${this.message}" not received after ${this.startupTimeout}ms`);
abortController.abort();
}

async handleLogs(container: Dockerode.Container, startTime?: Date): Promise<void> {
private async handleLogs(
container: Dockerode.Container,
startTime: number,
abortController: AbortController
): Promise<void> {
log.debug(`Waiting for log message "${this.message}"...`, { containerId: container.id });
const client = await getContainerRuntimeClient();
const stream = await client.container.logs(container, { since: startTime ? startTime.getTime() / 1000 : 0 });
const stream = await client.container.logs(container, { since: startTime });

let matches = 0;
for await (const line of byline(stream)) {
if (abortController.signal.aborted) {
break;
}
if (this.matches(line)) {
if (++matches === this.times) {
return log.debug(`Log wait strategy complete`, { containerId: container.id });
log.debug(`Log wait strategy complete`, { containerId: container.id });
abortController.abort();
return;
}
}
}

this.throwError(container.id, `Log stream ended and message "${this.message}" was not received`);
}

matches(line: string): boolean {
private matches(line: string): boolean {
return this.message instanceof RegExp ? this.message.test(line) : line.includes(this.message);
}

throwError(containerId: string, message: string): void {
private throwError(containerId: string, message: string): void {
log.error(message, { containerId });
throw new Error(message);
}
Expand Down