Skip to content

Commit 5e318eb

Browse files
committed
guard against parallel evaluation
1 parent 9683133 commit 5e318eb

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementsRule.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.Set;
5252
import java.util.TreeSet;
5353
import java.util.concurrent.ConcurrentHashMap;
54+
import java.util.concurrent.atomic.AtomicBoolean;
5455
import java.util.function.BiConsumer;
5556
import java.util.function.BiFunction;
5657
import java.util.function.Consumer;
@@ -68,6 +69,7 @@ public class TestEntitlementsRule implements TestRule {
6869

6970
private static final Map<BaseDir, Collection<Path>> BASE_DIR_PATHS = new ConcurrentHashMap<>();
7071
private static final TestPolicyManager POLICY_MANAGER;
72+
private static final AtomicBoolean active = new AtomicBoolean(false);
7173

7274
static {
7375
PathLookup pathLookup = new TestPathLookup(BASE_DIR_PATHS);
@@ -103,25 +105,30 @@ public Statement apply(Statement base, Description description) {
103105
return new Statement() {
104106
@Override
105107
public void evaluate() throws Throwable {
106-
try {
107-
POLICY_MANAGER.setActive(false == withoutEntitlements);
108-
POLICY_MANAGER.setTriviallyAllowingTestCode(false == withEntitlementsOnTestCode);
109-
if (entitledPackages != null) {
110-
assert entitledPackages.value().length > 0 : "No test packages specified in @EntitledTestPackages";
111-
POLICY_MANAGER.setEntitledTestPackages(entitledPackages.value());
112-
} else {
108+
if (active.compareAndSet(false, true)) {
109+
try {
110+
POLICY_MANAGER.setActive(false == withoutEntitlements);
111+
POLICY_MANAGER.setTriviallyAllowingTestCode(false == withEntitlementsOnTestCode);
112+
if (entitledPackages != null) {
113+
assert entitledPackages.value().length > 0 : "No test packages specified in @EntitledTestPackages";
114+
POLICY_MANAGER.setEntitledTestPackages(entitledPackages.value());
115+
} else {
116+
POLICY_MANAGER.setEntitledTestPackages();
117+
}
118+
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
119+
POLICY_MANAGER.clearModuleEntitlementsCache();
120+
// evaluate the suite
121+
base.evaluate();
122+
} finally {
123+
POLICY_MANAGER.setActive(false);
124+
POLICY_MANAGER.setTriviallyAllowingTestCode(true);
113125
POLICY_MANAGER.setEntitledTestPackages();
126+
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
127+
POLICY_MANAGER.clearModuleEntitlementsCache();
128+
active.set(false);
114129
}
115-
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
116-
POLICY_MANAGER.clearModuleEntitlementsCache();
117-
// evaluate the suite
118-
base.evaluate();
119-
} finally {
120-
POLICY_MANAGER.setActive(false);
121-
POLICY_MANAGER.setTriviallyAllowingTestCode(true);
122-
POLICY_MANAGER.setEntitledTestPackages();
123-
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
124-
POLICY_MANAGER.clearModuleEntitlementsCache();
130+
} else {
131+
throw new AssertionError("TestPolicyManager doesn't support test isolation, test suits cannot be run in parallel");
125132
}
126133
}
127134
};

0 commit comments

Comments
 (0)