Skip to content

Commit

Permalink
Make Network instances reusable. Closes #469
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Oct 4, 2017
1 parent 540f567 commit 6d8524d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Fixed TAR composition on Windows (#444)
- Allowing `addExposedPort` to be used after ports have been specified with `withExposedPorts` (#453)
- Stopping creation of temporary directory prior to creating temporary file (#443)
- Make Network instances reusable (i.e. work with `@ClassRule`) (#469)

### Changed
- Added `forResponsePredicate` method to HttpWaitStrategy to test response body (#441)
Expand Down
30 changes: 21 additions & 9 deletions core/src/main/java/org/testcontainers/containers/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@
import org.testcontainers.DockerClientFactory;
import org.testcontainers.utility.ResourceReaper;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public interface Network extends AutoCloseable, TestRule {

String getId();

@Override
default void close() {
ResourceReaper.instance().removeNetworkById(getId());
}

static Network newNetwork() {
return builder().build();
}
Expand All @@ -42,10 +37,20 @@ class NetworkImpl extends ExternalResource implements Network {
private String driver;

@Singular
private Set<Consumer<CreateNetworkCmd>> createNetworkCmdModifiers = new LinkedHashSet<>();
private Set<Consumer<CreateNetworkCmd>> createNetworkCmdModifiers;

@Getter(lazy = true)
private final String id = create();
private String id;

private final AtomicBoolean initialized = new AtomicBoolean();

@Override
public String getId() {
if (initialized.compareAndSet(false, true)) {
id = create();
}

return id;
}

private String create() {
CreateNetworkCmd createNetworkCmd = DockerClientFactory.instance().client().createNetworkCmd();
Expand Down Expand Up @@ -74,5 +79,12 @@ private String create() {
protected void after() {
close();
}

@Override
public void close() {
if (initialized.getAndSet(false)) {
ResourceReaper.instance().removeNetworkById(id);
}
}
}
}
19 changes: 19 additions & 0 deletions core/src/test/java/org/testcontainers/containers/NetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,24 @@ public void testModifiers() throws Exception {
);
}
}

@Test
public void testReusability() throws Exception {
try (Network network = Network.newNetwork()) {
String firstId = network.getId();
assertNotNull(
"Network exists",
DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(firstId).exec()
);

network.close();

assertNotEquals(
"New network created",
firstId,
DockerClientFactory.instance().client().inspectNetworkCmd().withNetworkId(network.getId()).exec().getId()
);
}
}
}
}

0 comments on commit 6d8524d

Please sign in to comment.