< prev index next > test/jdk/java/util/concurrent/StructuredTaskScope/StructuredTaskScopeTest.java
Print this page
@MethodSource("factories")
void testForkAfterJoin(ThreadFactory factory) throws Exception {
try (var scope = new StructuredTaskScope<String>(null, factory)) {
// round 1
var subtask1 = scope.fork(() -> "foo");
- assertThrows(IllegalStateException.class, subtask1::get);
scope.join();
assertEquals("foo", subtask1.get());
// round 2
var subtask2 = scope.fork(() -> "bar");
assertEquals("foo", subtask1.get());
- assertThrows(IllegalStateException.class, subtask2::get);
scope.join();
assertEquals("foo", subtask1.get());
assertEquals("bar", subtask2.get());
// round 3
var subtask3 = scope.fork(() -> "baz");
assertEquals("foo", subtask1.get());
assertEquals("bar", subtask2.get());
- assertThrows(IllegalStateException.class, subtask3::get);
scope.join();
assertEquals("foo", subtask1.get());
assertEquals("bar", subtask2.get());
assertEquals("baz", subtask3.get());
}
// allow subtask1 to finish
latch.countDown();
// continue to fork
var subtask2 = scope.fork(() -> "bar");
- assertThrows(IllegalStateException.class, subtask1::get);
- assertThrows(IllegalStateException.class, subtask2::get);
scope.join();
assertEquals("foo", subtask1.get());
assertEquals("bar", subtask2.get());
}
}
@MethodSource("factories")
void testSubtaskWhenSuccess(ThreadFactory factory) throws Exception {
try (var scope = new StructuredTaskScope<String>(null, factory)) {
Callable<String> task = () -> "foo";
Subtask<String> subtask = scope.fork(task);
-
- // before join, owner thread
assertEquals(task, subtask.task());
- assertThrows(IllegalStateException.class, subtask::get);
- assertThrows(IllegalStateException.class, subtask::exception);
-
scope.join();
// after join
assertEquals(task, subtask.task());
assertEquals(Subtask.State.SUCCESS, subtask.state());
@MethodSource("factories")
void testSubtaskWhenFailed(ThreadFactory factory) throws Exception {
try (var scope = new StructuredTaskScope<String>(null, factory)) {
Callable<String> task = () -> { throw new FooException(); };
Subtask<String> subtask = scope.fork(task);
-
- // before join, owner thread
assertEquals(task, subtask.task());
- assertThrows(IllegalStateException.class, subtask::get);
- assertThrows(IllegalStateException.class, subtask::exception);
-
scope.join();
// after join
assertEquals(task, subtask.task());
assertEquals(Subtask.State.FAILED, subtask.state());
< prev index next >