Skip to content

Commit

Permalink
Cleanup if start failed (#336)
Browse files Browse the repository at this point in the history
Closes #335

* prevent potential NPE in DockerComposeContainer

* add regression test for #335

* ensure cleanup if starting fails in FailureDetectingExternalResource (#335)
  • Loading branch information
tbcs authored and bsideup committed May 29, 2017
1 parent 96bf188 commit ab56a27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
private final String identifier;
private final Map<String, AmbassadorContainer> ambassadorContainers = new HashMap<>();
private final List<File> composeFiles;
private Set<String> spawnedContainerIds;
private Set<String> spawnedContainerIds = Collections.emptySet();
private Map<String, Integer> scalingPreferences = new HashMap<>();
private DockerClient dockerClient;
private boolean localCompose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public void evaluate() throws Throwable {

List<Throwable> errors = new ArrayList<Throwable>();

starting(description);

try {
starting(description);
base.evaluate();
succeeded(description);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.testcontainers.containers;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

public class FailureDetectingExternalResourceTest {

@Test
public void finishedIsCalledForCleanupIfStartingThrows() throws Throwable {
FailureDetectingExternalResource res = spy(FailureDetectingExternalResource.class);
Statement stmt = res.apply(mock(Statement.class), Description.EMPTY);
doThrow(new RuntimeException()).when(res).starting(any());
try {
stmt.evaluate();
} catch (Throwable t) {
// ignore
}
verify(res).starting(any());
verify(res).finished(any());
}

}

0 comments on commit ab56a27

Please sign in to comment.