Skip to content

Commit

Permalink
stop database containers when spock execution ends (#2843)
Browse files Browse the repository at this point in the history
The existing integration test framework was configured to stop TestSystem's when the cleanup: method was called. Spock internally called this method when it finished executing all of the tests in a class. Thus, when a particular test class was completed, its TestSystem was stopped, which in some cases was premature, since other classes might use the same TestSystem.

Instead, the TestSystem's should be stopped when Spock finishes executing all of the tests in all classes.
  • Loading branch information
StevenMassaro committed May 12, 2022
1 parent 8d3abcd commit d2bea1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package liquibase.extension.testing.testsystem.spock;

import liquibase.extension.testing.testsystem.TestSystem;
import org.spockframework.runtime.extension.IGlobalExtension;
import org.spockframework.runtime.model.SpecInfo;

public class LiquibaseGlobalExtension implements IGlobalExtension {
@Override
public void start() {

}

@Override
public void visitSpec(SpecInfo spec) {

}

@Override
public void stop() {
for (TestSystem startedTestSystem : LiquibaseIntegrationMethodInterceptor.startedTestSystems) {
try {
startedTestSystem.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
import org.spockframework.runtime.model.FieldInfo;
import org.spockframework.runtime.model.SpecInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.*;

public class LiquibaseIntegrationMethodInterceptor extends AbstractMethodInterceptor {

private static final SortedSet<TestSystem.Definition> testSystems = new TreeSet<>();
public static final Set<TestSystem> startedTestSystems = new HashSet<>();

private final SpecInfo spec;
private final LiquibaseIntegrationTestExtension.ErrorListener errorListener;
Expand Down Expand Up @@ -47,15 +45,6 @@ public void interceptSetupSpecMethod(IMethodInvocation invocation) throws Throwa
invocation.proceed();
}

@Override
public void interceptCleanupSpecMethod(IMethodInvocation invocation) throws Throwable {
final List<FieldInfo> containers = findAllContainers();
stopContainers(containers, invocation);

invocation.proceed();
}


private List<FieldInfo> findAllContainers() {
List<FieldInfo> returnList = new ArrayList<>();
for (FieldInfo fieldInfo : spec.getAllFields()) {
Expand All @@ -74,6 +63,7 @@ private static void startContainers(List<FieldInfo> containers, IMethodInvocatio
Assume.assumeTrue("Not running test against " + testSystem.getDefinition() + ": liquibase.sdk.testSystem.test is " + configuredTestSystems + " and liquibase.sdk.testSystem.skip is " + skippedTestSystems, testSystem.shouldTest());

testSystem.start();
startedTestSystems.add(testSystem);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
liquibase.extension.testing.testsystem.spock.LiquibaseGlobalExtension

0 comments on commit d2bea1a

Please sign in to comment.