Skip to content

Commit

Permalink
Make Network instances reusable. Closes #469 (#470)
Browse files Browse the repository at this point in the history
* Make Network instances reusable. Closes #469

* Update CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
bsideup authored Nov 6, 2017
1 parent 635f137 commit 1c66a14
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 @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
- Fixed regression in 1.4.3 when using Docker Compose on Windows ([\#439](https://github.com/testcontainers/testcontainers-java/issues/439))

### Changed
- Make Network instances reusable (i.e. work with `@ClassRule`) ([\#469](https://github.com/testcontainers/testcontainers-java/issues/469))

## [1.4.3] - 2017-10-14
### Fixed
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 1c66a14

Please sign in to comment.