1 /*
2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @summary Stress test of StructuredTaskScope cancellation with running and starting threads
27 * @enablePreview
28 * @run junit StressCancellation
29 */
30
31 import java.time.Duration;
32 import java.util.concurrent.StructuredTaskScope;
33 import java.util.concurrent.StructuredTaskScope.Joiner;
34 import java.util.concurrent.StructuredTaskScope.Subtask;
35 import java.util.concurrent.ThreadFactory;
36 import java.util.stream.IntStream;
37 import java.util.stream.Stream;
38
39 import org.junit.jupiter.params.ParameterizedTest;
40 import org.junit.jupiter.params.provider.Arguments;
41 import org.junit.jupiter.params.provider.MethodSource;
42 import static org.junit.jupiter.api.Assertions.*;
43
44 class StressCancellation {
45
46 static Stream<Arguments> testCases() {
47 Stream<ThreadFactory> factories = Stream.of(
48 Thread.ofPlatform().factory(),
49 Thread.ofVirtual().factory()
50 );
51 // 0..15 forks before shutdown, 0..15 forks after shutdown
52 return factories.flatMap(f -> IntStream.range(0, 256)
53 .mapToObj(x -> Arguments.of(f, x & 0x0F, (x & 0xF0) >> 4)));
54 }
55
56 /**
57 * Test StructuredTaskScope cancellation with running threads and concurrently with
58 * threads that are starting. The cancellation should interrupt all running threads,
59 * join should wakeup, and close would complete quickly.
60 *
61 * @param factory the ThreadFactory to use
62 * @param beforeCancel the number of subtasks to fork before cancel
63 * @param afterCancel the number of subtasks to fork after cancel
64 */
65 @ParameterizedTest
66 @MethodSource("testCases")
67 void test(ThreadFactory factory, int beforeCancel, int afterCancel) throws Exception {
68 var joiner = new Joiner<Boolean, Void>() {
69 @Override
70 public boolean onComplete(Subtask<? extends Boolean> subtask) {
71 boolean cancel = subtask.get();
72 return cancel;
73 }
74 @Override
75 public Void result() {
76 return null;
77 }
78 };
79
80 try (var scope = StructuredTaskScope.open(joiner, cf -> cf.withThreadFactory(factory))) {
81 // fork subtasks
82 for (int i = 0; i < beforeCancel; i++) {
83 scope.fork(() -> {
84 Thread.sleep(Duration.ofDays(1));
85 return false;
86 });
87 }
88
89 // fork subtask to cancel
90 scope.fork(() -> true);
91
92 // fork after forking subtask to cancel
93 for (int i = 0; i < afterCancel; i++) {
94 scope.fork(() -> {
95 Thread.sleep(Duration.ofDays(1));
96 return false;
97 });
98 }
99
100 scope.join();
101 }
102 }
103 }