Skip to content

Commit

Permalink
Merge pull request #237 from mktange/fix/memoized-ports-on-restart
Browse files Browse the repository at this point in the history
Fixed issue with memoized ports upon (re)start of docker container
  • Loading branch information
fryz authored Jun 28, 2018
2 parents 061fa90 + 4b9d885 commit a584cb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Container {
private final Docker docker;
private final DockerCompose dockerCompose;

private final Supplier<Ports> portMappings = Suppliers.memoize(this::getDockerPorts);
private Supplier<Ports> portMappings = Suppliers.memoize(this::getDockerPorts);

public Container(String containerName, Docker docker, DockerCompose dockerCompose) {
this.containerName = containerName;
Expand Down Expand Up @@ -92,6 +92,7 @@ public DockerPort port(int internalPort) {

public void start() throws IOException, InterruptedException {
dockerCompose.start(this);
portMappings = Suppliers.memoize(this::getDockerPorts);
}

public void stop() throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MockDockerEnvironment {
Expand Down Expand Up @@ -69,6 +70,14 @@ public DockerPort port(String service, String ip, int externalPortNumber, int in
return port;
}

public void ephemeralPort(String service, String ip, int internalPortNumber) throws IOException, InterruptedException {
AtomicInteger currentExternalPort = new AtomicInteger(33700);
when(dockerComposeProcess.ports(service)).then(a -> {
DockerPort port = dockerPortSpy(ip, currentExternalPort.incrementAndGet(), internalPortNumber);
return new Ports(port);
});
}

public void ports(String service, String ip, Integer... portNumbers) throws IOException, InterruptedException {
List<DockerPort> ports = Arrays.asList(portNumbers)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -28,6 +29,7 @@
import com.palantir.docker.compose.configuration.MockDockerEnvironment;
import com.palantir.docker.compose.execution.Docker;
import com.palantir.docker.compose.execution.DockerCompose;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -66,6 +68,24 @@ public void call_docker_ports_once_when_two_ports_are_requested() throws Excepti
verify(dockerCompose, times(1)).ports("service");
}

@Test
public void return_updated_external_port_on_restart() throws IOException, InterruptedException {
int internalPort = 5432;
env.ephemeralPort("service", IP, internalPort);

DockerPort port = container.port(internalPort);
int prePort = port.getExternalPort();

DockerPort samePort = container.port(internalPort);
assertThat(prePort, is(samePort.getExternalPort()));

container.stop();
container.start();

DockerPort updatedPort = container.port(internalPort);
assertThat(prePort, not(is(updatedPort.getExternalPort())));
}

@Test
public void throw_illegal_argument_exception_when_a_port_for_an_unknown_external_port_is_requested()
throws Exception {
Expand Down

0 comments on commit a584cb3

Please sign in to comment.