Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Store GenericContainer#exposedPorts as an order-preserving Set #2613

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -672,8 +683,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