Skip to content

Commit

Permalink
Fix test cluster behavior @ClusterScope(scope = Scope.SUITE) and @Ope…
Browse files Browse the repository at this point in the history
…nSearchIntegTestCase.SuiteScopeTestCase for parameterized test cases

Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Jan 23, 2024
1 parent 4167ac5 commit f4f253e
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* on the way cluster settings are being managed.
*/
class OpenSearchTestClusterRule implements MethodRule {
private final Map<TestCluster, OpenSearchIntegTestCase> suites = new IdentityHashMap<>();
private final Map<Class<?>, TestCluster> clusters = new IdentityHashMap<>();
private final Logger logger = LogManager.getLogger(getClass());

Expand Down Expand Up @@ -86,7 +87,13 @@ void afterClass() throws Exception {
printTestMessage("cleaning up after");
afterInternal(true, null);
OpenSearchTestCase.checkStaticState(true);
clusters.remove(getTestClass());
synchronized (clusters) {
final TestCluster cluster = clusters.remove(getTestClass());
IOUtils.closeWhileHandlingException(cluster);
if (cluster != null) {
suites.remove(cluster);
}
}
}
StrictCheckSpanProcessor.validateTracingStateOnShutdown();
} finally {
Expand Down Expand Up @@ -226,8 +233,8 @@ private static boolean isSuiteScopedTest(Class<?> clazz) {
return clazz.getAnnotation(SuiteScopeTestCase.class) != null;
}

private boolean hasParametersChanged(final ParameterizedOpenSearchIntegTestCase target) {
return !((ParameterizedOpenSearchIntegTestCase) suiteInstance).hasSameParametersAs(target);
private static boolean hasParametersChanged(final OpenSearchIntegTestCase instance, final ParameterizedOpenSearchIntegTestCase target) {
return !((ParameterizedOpenSearchIntegTestCase) instance).hasSameParametersAs(target);
}

private boolean runTestScopeLifecycle() {
Expand All @@ -242,8 +249,23 @@ private TestCluster buildAndPutCluster(Scope currentClusterScope, long seed, Ope
clearClusters(); // all leftovers are gone by now... this is really just a double safety if we miss something somewhere
switch (currentClusterScope) {
case SUITE:
if (testCluster != null && target instanceof ParameterizedOpenSearchIntegTestCase) {
final OpenSearchIntegTestCase instance = suites.get(testCluster);
if (instance != null) {
assert instance instanceof ParameterizedOpenSearchIntegTestCase;
if (hasParametersChanged(
(ParameterizedOpenSearchIntegTestCase) instance,
(ParameterizedOpenSearchIntegTestCase) target
)) {
IOUtils.closeWhileHandlingException(testCluster);
testCluster = null;
}
}
}

if (testCluster == null) { // only build if it's not there yet
testCluster = buildWithPrivateContext(currentClusterScope, seed, target);
suites.put(testCluster, target);
}
break;
case TEST:
Expand Down Expand Up @@ -310,6 +332,7 @@ private void clearClusters() throws Exception {
synchronized (clusters) {
if (!clusters.isEmpty()) {
IOUtils.close(clusters.values());
suites.clear();
clusters.clear();
}
}
Expand Down Expand Up @@ -363,7 +386,7 @@ private void initializeSuiteScope(OpenSearchIntegTestCase target, FrameworkMetho
// Catching the case when parameterized test cases are run: the test class stays the same but the test instances changes.
if (target instanceof ParameterizedOpenSearchIntegTestCase) {
assert suiteInstance instanceof ParameterizedOpenSearchIntegTestCase;
if (hasParametersChanged((ParameterizedOpenSearchIntegTestCase) target)) {
if (hasParametersChanged(suiteInstance, (ParameterizedOpenSearchIntegTestCase) target)) {
printTestMessage("new instance of parameterized test class, recreating cluster scope", method);
afterClass();
beforeClass();
Expand Down

0 comments on commit f4f253e

Please sign in to comment.