Skip to content

Commit

Permalink
Store GenericContainer#exposedPorts as an order-preserving Set (#…
Browse files Browse the repository at this point in the history
…2613)

* Store `GenericContainer#exposedPorts` as an order-preserving `Set`

* Update core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java

Co-authored-by: Richard North <rich.north@gmail.com>

Co-authored-by: Richard North <rich.north@gmail.com>
  • Loading branch information
bsideup and rnorth committed May 15, 2020
1 parent 672cbee commit 1eef935
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -118,7 +119,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
* Default settings
*/
@NonNull
private List<Integer> exposedPorts = new ArrayList<>();
private LinkedHashSet<Integer> exposedPorts = new LinkedHashSet<>();

@NonNull
private List<String> portBindings = new ArrayList<>();
Expand Down Expand Up @@ -256,6 +257,16 @@ public void setImage(Future<String> image) {
this.image = new RemoteDockerImage(image);
}

@Override
public List<Integer> getExposedPorts() {
return new ArrayList<>(exposedPorts);
}

@Override
public void setExposedPorts(List<Integer> exposedPorts) {
this.exposedPorts = new LinkedHashSet<>(exposedPorts);
}

/**
* @see #dependsOn(Iterable)
*/
Expand Down Expand Up @@ -671,8 +682,9 @@ protected void containerIsStopped(InspectContainerResponse containerInfo) {
@Deprecated
protected Integer getLivenessCheckPort() {
// legacy implementation for backwards compatibility
if (exposedPorts.size() > 0) {
return getMappedPort(exposedPorts.get(0));
Iterator<Integer> exposedPortsIterator = exposedPorts.iterator();
if (exposedPortsIterator.hasNext()) {
return getMappedPort(exposedPortsIterator.next());
} else if (portBindings.size() > 0) {
return Integer.valueOf(PortBinding.parse(portBindings.get(0)).getBinding().getHostPortSpec());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ public void addExposedPortAfterWithExposedPortsTest() {
assertTrue("addExposedPort should be exposed", redis.getExposedPorts().contains(8987));
}

@Test
public void addingExposedPortTwiceShouldNotFail() {
redis.addExposedPort(8987);
redis.addExposedPort(8987);
assertThat("Both ports should be exposed", redis.getExposedPorts().size(), equalTo(2)); // 2 ports = de-duplicated port 8897 and original port 6379
assertTrue("withExposedPort should be exposed", redis.getExposedPorts().contains(REDIS_PORT));
assertTrue("addExposedPort should be exposed", redis.getExposedPorts().contains(8987));
}

@Test
public void sharedMemorySetTest() {
try (GenericContainer containerWithSharedMemory = new GenericContainer()
Expand Down

0 comments on commit 1eef935

Please sign in to comment.