Skip to content

Commit

Permalink
only-call-after-once check in DockerComposeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
pnepywoda committed Nov 14, 2019
1 parent e619465 commit 3d98783
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,14 @@
public abstract class DockerComposeExtension extends DockerComposeManager
implements BeforeAllCallback, AfterAllCallback {

private boolean hasCalledAfterMethod;

@Override
public void beforeAll(ExtensionContext unused) throws IOException, InterruptedException {
try {
before();
} catch (RuntimeException e) {
after();
hasCalledAfterMethod = true;
throw e;
}
before();
}

@Override
public void afterAll(ExtensionContext unused) {
if (!hasCalledAfterMethod) {
after();
}
after();
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.connection.waiting.ClusterWait;
import com.palantir.docker.compose.events.EventConsumer;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
Expand All @@ -32,7 +31,7 @@
public class DockerComposeExtensionTest {

@Test
public void calls_after_only_once() throws IOException, InterruptedException {
public void calls_after_only_once() {
AtomicInteger count = new AtomicInteger();
DockerComposeExtension dockerComposeExtension = new DockerComposeExtension() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract class DockerComposeManager {
public static final int DEFAULT_RETRY_ATTEMPTS = 2;

private final RunRecorder runRecorder = RunRecorder.defaults();
private boolean hasCalledAfterMethod;

public DockerPort hostNetworkedPort(int port) {
return new DockerPort(machine().getIp(), port, port);
Expand Down Expand Up @@ -177,13 +178,19 @@ protected void setDescription(TestDescription testDescription) {
}

public void before() throws IOException, InterruptedException {
log.debug("Starting docker-compose cluster");
try {
log.debug("Starting docker-compose cluster");

runRecorder.before(() -> dockerCompose().config());
runRecorder.before(() -> dockerCompose().config());

pullBuildAndUp();
pullBuildAndUp();

emitEventsFor().waitingForServices(this::waitForServices);
emitEventsFor().waitingForServices(this::waitForServices);
} catch (RuntimeException e) {
after();
hasCalledAfterMethod = true;
throw e;
}
}

private void pullBuildAndUp() throws IOException, InterruptedException {
Expand Down Expand Up @@ -254,6 +261,10 @@ private void waitForAllClusterWaits(List<InterruptableClusterWait> allClusterWai
}

public void after() {
if (hasCalledAfterMethod) {
return;
}

try {
emitEventsFor().shutdownStop(() ->
shutdownStrategy().stop(this.dockerCompose()));
Expand Down

0 comments on commit 3d98783

Please sign in to comment.