Skip to content

Implement minimal ContainerDef - #5119

Closed
bsideup wants to merge 4 commits into
mainfrom
minimal_container_defs
Closed

bsideup wants to merge 4 commits into
mainfrom
minimal_container_defs

Conversation

@bsideup

@bsideup bsideup commented Feb 23, 2022

Copy link
Copy Markdown
Member

Extracted this from #4465 to demonstrate the idea without the overhead of Started state + only one state field (for now)


import org.testcontainers.images.RemoteDockerImage;

class ContainerDef extends BaseContainerDef {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please explain the need of this intermediate package-private type? I didn't get it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #4465. Eventually, BaseContainerDef's signature will include S extends Started type parameter, plus it is abstract.

this(new RemoteDockerImage(image));
}

BaseContainerDef createContainerDef(RemoteDockerImage image) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of createContainerDef here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See KafkaContainer#createContainerDef


Map<String, String> env = new HashMap<>();

protected void putEnv(@NonNull String key, String value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that mutable Container classes, and lack of builders, was a mistake I made in the initial Testcontainers APIs.

Are we unable to make ContainerDef immutable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two options:

  1. reuse ContainerDefs in GenericContainer to avoid a lot of code duplicated
  2. not reuse, but then we will need to add things to both places unless we promote this new API to stable

if we reuse, then, due to getEnv() and similar methods being used in an immutable way, we can't make underlying structures immutable (one of the first things I tried!).

BUT note that these methods are protected and only supposed to be used at the time of object's construction:
https://gist.github.com/bsideup/8247229e8a8dd86ac8bcf490e2ce6acb#file-newapitest-java-L7-L14

So, while it is not truly immutable, it dramatically improves the situation as compared to GenericContainer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that, in 2.0, we can finally make it immutable

@eddumelendez

Copy link
Copy Markdown
Member

Superseded by #7714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants