Skip to content

Commit

Permalink
JAVA-3117: Call CcmCustomRule#after if CcmCustomRule#before fails to …
Browse files Browse the repository at this point in the history
…allow subsequent tests to run

patch by Henry Hughes; reviewed by Alexandre Dutra and Andy Tolbert for JAVA-3117
  • Loading branch information
hhughes authored and tolbertam committed Sep 11, 2024
1 parent 9cfb4f6 commit c961012
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.datastax.oss.driver.api.testinfra.ccm;

import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A rule that creates a ccm cluster that can be used in a test. This should be used if you plan on
Expand All @@ -30,6 +32,7 @@
*/
public class CustomCcmRule extends BaseCcmRule {

private static final Logger LOG = LoggerFactory.getLogger(CustomCcmRule.class);
private static final AtomicReference<CustomCcmRule> CURRENT = new AtomicReference<>();

CustomCcmRule(CcmBridge ccmBridge) {
Expand All @@ -39,7 +42,20 @@ public class CustomCcmRule extends BaseCcmRule {
@Override
protected void before() {
if (CURRENT.get() == null && CURRENT.compareAndSet(null, this)) {
super.before();
try {
super.before();
} catch (Exception e) {
// ExternalResource will not call after() when before() throws an exception
// Let's try and clean up and release the lock we have in CURRENT
LOG.warn(
"Error in CustomCcmRule before() method, attempting to clean up leftover state", e);
try {
after();
} catch (Exception e1) {
LOG.warn("Error cleaning up CustomCcmRule before() failure", e1);
}
throw e;
}
} else if (CURRENT.get() != this) {
throw new IllegalStateException(
"Attempting to use a Ccm rule while another is in use. This is disallowed");
Expand Down

0 comments on commit c961012

Please sign in to comment.