Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVA-3117: Call CcmCustomRule#after if CcmCustomRule#before fails to … #1720

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -28,6 +30,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 @@ -37,7 +40,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