Skip to content

Commit c961012

Browse files
hhughestolbertam
authored andcommitted
JAVA-3117: Call CcmCustomRule#after if CcmCustomRule#before fails to allow subsequent tests to run
patch by Henry Hughes; reviewed by Alexandre Dutra and Andy Tolbert for JAVA-3117
1 parent 9cfb4f6 commit c961012

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

test-infra/src/main/java/com/datastax/oss/driver/api/testinfra/ccm/CustomCcmRule.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.datastax.oss.driver.api.testinfra.ccm;
1919

2020
import java.util.concurrent.atomic.AtomicReference;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

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

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

3538
CustomCcmRule(CcmBridge ccmBridge) {
@@ -39,7 +42,20 @@ public class CustomCcmRule extends BaseCcmRule {
3942
@Override
4043
protected void before() {
4144
if (CURRENT.get() == null && CURRENT.compareAndSet(null, this)) {
42-
super.before();
45+
try {
46+
super.before();
47+
} catch (Exception e) {
48+
// ExternalResource will not call after() when before() throws an exception
49+
// Let's try and clean up and release the lock we have in CURRENT
50+
LOG.warn(
51+
"Error in CustomCcmRule before() method, attempting to clean up leftover state", e);
52+
try {
53+
after();
54+
} catch (Exception e1) {
55+
LOG.warn("Error cleaning up CustomCcmRule before() failure", e1);
56+
}
57+
throw e;
58+
}
4359
} else if (CURRENT.get() != this) {
4460
throw new IllegalStateException(
4561
"Attempting to use a Ccm rule while another is in use. This is disallowed");

0 commit comments

Comments
 (0)