Skip to content

Commit 46f2099

Browse files
committed
Only allow caller owning the DeferredSupplier to run it
1 parent 160a4ed commit 46f2099

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/support/store/NamespacedHierarchicalStore.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void close() {
232232
});
233233

234234
if (newStoredValue instanceof StoredValue.DeferredValue value) {
235-
value.delegate().run();
235+
value.delegate().run(null);
236236
}
237237
return requireNonNull(newStoredValue.evaluate());
238238
}
@@ -261,20 +261,21 @@ public <K, V> Object computeIfAbsent(N namespace, K key, Function<? super K, ? e
261261
if (result != null) {
262262
return result;
263263
}
264+
Object ownerToken = new Object();
264265
StoredValue newStoredValue = this.storedValues.compute(compositeKey, (__, oldStoredValue) -> {
265266
if (StoredValue.evaluateIfNotNull(oldStoredValue) != null) {
266267
return oldStoredValue;
267268
}
268269
rejectIfClosed();
269-
return newStoredSuppliedValue(new DeferredSupplier(() -> {
270+
return newStoredSuppliedValue(new DeferredSupplier(ownerToken, () -> {
270271
rejectIfClosed();
271272
return Preconditions.notNull(defaultCreator.apply(key), "defaultCreator must not return null");
272273
}));
273274
});
274275

275276
if (newStoredValue instanceof StoredValue.DeferredOptionalValue value) {
276277
var delegate = value.delegate();
277-
delegate.run();
278+
delegate.run(ownerToken);
278279
return requireNonNull(delegate.getOrThrow());
279280
}
280281
// put or getOrComputeIfAbsent won the race
@@ -548,12 +549,21 @@ private void close(CloseAction<N> closeAction) throws Throwable {
548549
private static final class DeferredSupplier implements Supplier<@Nullable Object> {
549550

550551
private final FutureTask<@Nullable Object> task;
552+
private final @Nullable Object ownerToken;
551553

552554
DeferredSupplier(Supplier<@Nullable Object> delegate) {
555+
this(null, delegate);
556+
}
557+
558+
public DeferredSupplier(Object ownerToken, Supplier<@Nullable Object> delegate) {
559+
this.ownerToken = ownerToken;
553560
this.task = new FutureTask<>(delegate::get);
554561
}
555562

556-
void run() {
563+
void run(Object ownerToken) {
564+
if (!Objects.equals(this.ownerToken, ownerToken)) {
565+
return;
566+
}
557567
this.task.run();
558568
}
559569

0 commit comments

Comments
 (0)