Skip to content

Commit 3c5a571

Browse files
committed
Test DeferredSupplier
1 parent bd2042f commit 3c5a571

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ private void close(CloseAction<N> closeAction) throws Throwable {
565565
* {@link ConcurrentHashMap#compute(Object, BiFunction)} calls and
566566
* prevents recursive updates.
567567
*/
568-
private static final class DeferredSupplier implements Supplier<@Nullable Object> {
568+
static final class DeferredSupplier implements Supplier<@Nullable Object> {
569569

570570
private final FutureTask<@Nullable Object> task;
571571
private final @Nullable Object ownerToken;

platform-tests/src/test/java/org/junit/platform/engine/support/store/NamespacedHierarchicalStoreTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.junit.jupiter.api.Assertions.assertNull;
1717
import static org.junit.jupiter.api.Assertions.assertSame;
1818
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
1920
import static org.junit.platform.commons.test.ConcurrencyTestingUtils.executeConcurrently;
2021
import static org.mockito.Mockito.doThrow;
2122
import static org.mockito.Mockito.inOrder;
@@ -732,6 +733,46 @@ private void assertClosed() {
732733

733734
}
734735

736+
@Nested
737+
class DeferredSupplierTests {
738+
739+
@Test
740+
void getCanBeInterrupted() {
741+
var supplier = new NamespacedHierarchicalStore.DeferredSupplier(() -> {
742+
try {
743+
Thread.sleep(1000);
744+
}
745+
catch (InterruptedException e) {
746+
throw new ComputeException(e);
747+
}
748+
return value;
749+
});
750+
Thread.currentThread().interrupt();
751+
assertThrows(InterruptedException.class, () -> {
752+
supplier.get();
753+
});
754+
assertTrue(Thread.interrupted());
755+
}
756+
757+
@Test
758+
void getOrThrowCanBeInterrupted() {
759+
var supplier = new NamespacedHierarchicalStore.DeferredSupplier(() -> {
760+
try {
761+
Thread.sleep(1000);
762+
}
763+
catch (InterruptedException e) {
764+
throw new ComputeException(e);
765+
}
766+
return value;
767+
});
768+
Thread.currentThread().interrupt();
769+
assertThrows(InterruptedException.class, () -> {
770+
supplier.getOrThrow();
771+
});
772+
assertTrue(Thread.interrupted());
773+
}
774+
}
775+
735776
private static Object createObject(String display) {
736777
return new Object() {
737778

@@ -753,5 +794,9 @@ private static final class ComputeException extends RuntimeException {
753794
ComputeException(String msg) {
754795
super(msg);
755796
}
797+
798+
ComputeException(InterruptedException e) {
799+
super(e);
800+
}
756801
}
757802
}

0 commit comments

Comments
 (0)