Repository navigation
Expand file tree
/
Copy pathParallelFetchServiceTest.java
More file actions
135 lines (112 loc) · 5.51 KB
/
Copy pathParallelFetchServiceTest.java
File metadata and controls
135 lines (112 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package se.deversity.asynctest.example;
import se.deversity.asynctest.diagnostics.StructuredTaskScopeMisuseDetector;
import se.deversity.asynctest.example.service.ParallelFetchService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test for ParallelFetchService.
*
* ========================================================================
* DETECTOR: StructuredTaskScopeMisuseDetector (standalone — JDK 25/26, JEP 505)
* ========================================================================
*
* The JDK 25 StructuredTaskScope.open(Joiner) API enforces a strict lifecycle:
*
* open → fork* → join → get* → close (try-with-resources)
*
* Breaking it does not just produce a bad result — it throws, leaks subtasks, or
* reads from an incomplete subtask. StructuredTaskScopeMisuseDetector models each
* scope as a state machine and flags the transitions the runtime rejects:
*
* - fork() after join() → IllegalStateException
* - Subtask.get() before join() → IllegalStateException (partial result)
* - fork()/join() off-owner → WrongThreadException (scope is confined)
* - close() without join() → running subtasks cancelled, work lost
*
* The detector is standalone (not wired into @AsyncTest) — instantiate it, record
* lifecycle events, then assert on analyze().
*/
class ParallelFetchServiceTest {
private ParallelFetchService service;
private StructuredTaskScopeMisuseDetector detector;
@BeforeEach
void setUp() {
service = new ParallelFetchService();
detector = new StructuredTaskScopeMisuseDetector();
}
// -----------------------------------------------------------------------
// Part 1: correct lifecycle — open, fork, join, read, close. No misuse.
// -----------------------------------------------------------------------
@Test
void correctLifecycle_isClean() throws Exception {
Thread owner = Thread.currentThread();
detector.recordScopeOpened("fetch", owner);
detector.recordFork("fetch", "a", owner);
detector.recordFork("fetch", "b", owner);
List<String> results = service.fetchAll(List.of("a", "b")); // real fan-out
detector.recordJoin("fetch", owner);
detector.recordResultRead("fetch", "a", owner);
detector.recordResultRead("fetch", "b", owner);
detector.recordScopeClosed("fetch", owner);
assertEquals(List.of("result-for-a", "result-for-b"), results);
assertFalse(detector.analyze().hasIssues(), () -> "Expected clean lifecycle:\n" + detector.analyze());
}
// -----------------------------------------------------------------------
// Part 2: fork after join — the scope no longer accepts work.
// -----------------------------------------------------------------------
@Test
void forkAfterJoin_isDetected() {
Thread owner = Thread.currentThread();
detector.recordScopeOpened("fetch", owner);
detector.recordFork("fetch", "a", owner);
detector.recordJoin("fetch", owner);
detector.recordFork("fetch", "late", owner); // BUG: fork after join
var report = detector.analyze();
assertTrue(report.hasIssues());
assertEquals(1, report.getForkAfterJoinIssues().size());
assertTrue(report.getForkAfterJoinIssues().get(0).contains("IllegalStateException"));
}
// -----------------------------------------------------------------------
// Part 3: result read before join — may read a partial result.
// -----------------------------------------------------------------------
@Test
void resultBeforeJoin_isDetected() {
Thread owner = Thread.currentThread();
detector.recordScopeOpened("fetch", owner);
detector.recordFork("fetch", "a", owner);
detector.recordResultRead("fetch", "a", owner); // BUG: read before join
var report = detector.analyze();
assertTrue(report.hasIssues());
assertEquals(1, report.getResultBeforeJoinIssues().size());
}
// -----------------------------------------------------------------------
// Part 4: owner confinement — fork from a non-owner thread.
// -----------------------------------------------------------------------
@Test
void forkFromNonOwnerThread_isDetected() throws Exception {
Thread owner = Thread.currentThread();
detector.recordScopeOpened("fetch", owner);
Thread other = new Thread(() -> detector.recordFork("fetch", "x", Thread.currentThread()));
other.start();
other.join();
var report = detector.analyze();
assertTrue(report.hasIssues());
assertEquals(1, report.getConfinementIssues().size());
assertTrue(report.getConfinementIssues().get(0).contains("WrongThreadException"));
}
// -----------------------------------------------------------------------
// Part 5: close without join — running subtasks are cancelled.
// -----------------------------------------------------------------------
@Test
void closeWithoutJoin_isDetected() {
Thread owner = Thread.currentThread();
detector.recordScopeOpened("fetch", owner);
detector.recordFork("fetch", "a", owner);
detector.recordScopeClosed("fetch", owner); // BUG: never joined
var report = detector.analyze();
assertTrue(report.hasIssues());
assertEquals(1, report.getMissingJoinIssues().size());
}
}