Pass the started container to `WaitStrategy.waitUntilReady`

Open
#1,558 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
docker, node.js, typescript

Research direction

Start with packages/testcontainers/src/wait-strategies/wait-strategy.ts and inspect every strategy in packages/testcontainers/src/wait-strategies/, then trace the listed callers in generic-container.ts, started-generic-container.ts and docker-compose-environment.ts. Check the start() and restart() paths, including how the log strategy receives startTime, and review the coordination issues before changing the shared signatures. Done means strategies receive the started container or a suitable target, callers and custom strategy examples are updated, and the startup deadline and abort signal have a defined place in the interface.

Written by the indexing model from the issue text.

Description

enhancement major

What happens

A wait strategy is handed the low-level pieces instead of the container:

export interface WaitStrategy {
  waitUntilReady(container: Dockerode.Container, boundPorts: BoundPorts, startTime?: Date): Promise<void>;
  withStartupTimeout(startupTimeoutMs: number): WaitStrategy;
  isStartupTimeoutSet(): boolean;
  getStartupTimeout(): number;
}

To do anything useful, an implementation has to fetch the rest itself. The six built-in strategies each call getContainerRuntimeClient() to find the host, read logs or run a command. A custom strategy needs Dockerode knowledge to do what StartedTestContainer already offers as getHost(), getMappedPort(), exec() and logs(). The documented route for custom strategies, StartupCheckStrategy.checkStartupState(dockerClient: Dockerode, containerId: string), has the same shape.

It also makes strategies awkward to reuse on a container that is already started. CouchbaseContainer rebuilds the arguments by hand to run an HTTP wait:

Wait.forHttp("/pools", PORTS.MGMT_PORT)
  .forStatusCode(200)
  .waitUntilReady(
    client.container.getById(startedTestContainer.getId()),
    BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, inspectResult)
  );

Kafka and Redpanda do the same through waitForContainer.

Expected

waitUntilReady receives the container, or a narrower target interface that the started container satisfies:

interface WaitStrategy {
  waitUntilReady(container: StartedTestContainer): Promise<void>;
}
  • A module can write await Wait.forHttp("/pools", port).waitUntilReady(container).
  • A custom strategy uses container.getHost(), container.getMappedPort(port), container.exec(...) and container.logs().
  • Dockerode.Container and BoundPorts leave the public interface.

Where

  • packages/testcontainers/src/wait-strategies/wait-strategy.ts
  • every strategy in packages/testcontainers/src/wait-strategies/
  • packages/testcontainers/src/wait-strategies/wait-for-container.ts
  • callers in generic-container.ts, started-generic-container.ts and docker-compose-environment.ts

Notes

  • Java: void waitUntilReady(WaitStrategyTarget waitStrategyTarget), where WaitStrategyTarget extends ContainerState and the container itself is passed (getHost, getMappedPort, execInContainer, getLogs, getContainerInfo).
  • Go: WaitUntilReady(ctx, target StrategyTarget), with Host, Inspect, MappedPort, Logs, Exec, State and CopyFileFromContainer.
  • .NET: IWaitUntil.UntilAsync(IContainer container).
  • The wait runs before start() returns, so the started container has to exist before the wait instead of after it. restart() passes a startTime so the log strategy ignores older output; that needs a home on the target.
  • Semver: this changes the interface that custom strategies implement, hence major.
  • Whatever replaces the current arguments should have room for a startup deadline and an abort signal, so the interface changes once.

Dependencies

  • Nothing blocks this. It can start now.
  • Coordinate with #1523, #1533, #1534: small changes to the same strategy files. Land them first, since this rewrites every strategy's signature.
  • Coordinate with #1532: the same files gain typed errors.
  • Coordinate with #1582: the startup deadline needs a place in the new arguments.
Dominant language
TypeScript
Stars
2.7k
Forks
290
Avg merge
4d 1h
Merged PRs (30d)
22

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-node

All issues in testcontainers/testcontainers-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.