Skip to content

Commit 267e938

Browse files
Approach 1 using MemoizingSupplier with a transientFailures flag
Signed-off-by: martinfrancois <f.martin@fastmail.com>
1 parent 4207b3a commit 267e938

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public <K, V> Object computeIfAbsent(N namespace, K key, Function<? super K, ? e
250250
return newStoredValue(new MemoizingSupplier(() -> {
251251
rejectIfClosed();
252252
return Preconditions.notNull(defaultCreator.apply(key), "defaultCreator must not return null");
253-
}));
253+
}, true));
254254
}
255255
return oldStoredValue;
256256
});
@@ -445,8 +445,15 @@ private record StoredValue(int order, Supplier<@Nullable Object> supplier) {
445445
return this.supplier.get();
446446
}
447447

448+
private @Nullable Object evaluateOrNullOnFailure() {
449+
if (this.supplier instanceof MemoizingSupplier memoizing) {
450+
return memoizing.getOrNullOnFailure();
451+
}
452+
return this.supplier.get();
453+
}
454+
448455
static @Nullable Object evaluateIfNotNull(@Nullable StoredValue value) {
449-
return value != null ? value.evaluate() : null;
456+
return value != null ? value.evaluateOrNullOnFailure() : null;
450457
}
451458

452459
}
@@ -478,12 +485,18 @@ private static class MemoizingSupplier implements Supplier<@Nullable Object> {
478485
private static final Object NO_VALUE_SET = new Object();
479486

480487
private final Supplier<@Nullable Object> delegate;
488+
private final boolean transientFailures;
481489

482490
@Nullable
483491
private volatile Object value = NO_VALUE_SET;
484492

485493
private MemoizingSupplier(Supplier<@Nullable Object> delegate) {
494+
this(delegate, false);
495+
}
496+
497+
private MemoizingSupplier(Supplier<@Nullable Object> delegate, boolean transientFailures) {
486498
this.delegate = delegate;
499+
this.transientFailures = transientFailures;
487500
}
488501

489502
@Override
@@ -497,6 +510,19 @@ private MemoizingSupplier(Supplier<@Nullable Object> delegate) {
497510
return this.value;
498511
}
499512

513+
private @Nullable Object getOrNullOnFailure() {
514+
if (this.value == NO_VALUE_SET) {
515+
computeValue();
516+
}
517+
if (this.value instanceof Failure failure) {
518+
if (this.transientFailures) {
519+
return null;
520+
}
521+
throw throwAsUncheckedException(failure.throwable);
522+
}
523+
return this.value;
524+
}
525+
500526
private synchronized void computeValue() {
501527
try {
502528
if (this.value == NO_VALUE_SET) {

0 commit comments

Comments
 (0)