|
11 | 11 | package org.junit.vintage.engine.samples.junit4; |
12 | 12 |
|
13 | 13 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 | | -import static org.junit.Assert.fail; |
15 | 14 |
|
16 | | -import java.util.HashSet; |
17 | 15 | import java.util.Set; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
18 | 17 | import java.util.concurrent.CountDownLatch; |
19 | | -import java.util.concurrent.atomic.AtomicInteger; |
20 | 18 |
|
21 | | -import org.junit.BeforeClass; |
22 | 19 | import org.junit.Rule; |
23 | 20 | import org.junit.Test; |
24 | 21 | import org.junit.experimental.runners.Enclosed; |
|
29 | 26 | @RunWith(Enclosed.class) |
30 | 27 | public class JUnit4ParallelTestCase { |
31 | 28 |
|
32 | | - public static class SuccessfulParallelTestCase { |
33 | | - public static Set<String> threadNames; |
34 | | - static AtomicInteger sharedResource; |
35 | | - static CountDownLatch countDownLatch; |
| 29 | + public static class AbstractBlockingTestCase { |
36 | 30 |
|
37 | | - @Rule |
38 | | - public final TestWatcher testWatcher = new TestWatcher() { |
39 | | - @Override |
40 | | - protected void starting(Description description) { |
41 | | - super.starting(description); |
42 | | - threadNames.add(Thread.currentThread().getName()); |
43 | | - } |
44 | | - }; |
45 | | - |
46 | | - @BeforeClass |
47 | | - public static void initialize() { |
48 | | - sharedResource = new AtomicInteger(); |
49 | | - countDownLatch = new CountDownLatch(3); |
50 | | - threadNames = new HashSet<>(); |
51 | | - } |
52 | | - |
53 | | - @Test |
54 | | - public void firstTest() throws Exception { |
55 | | - incrementAndBlock(sharedResource, countDownLatch); |
56 | | - } |
57 | | - |
58 | | - @Test |
59 | | - public void secondTest() throws Exception { |
60 | | - incrementAndBlock(sharedResource, countDownLatch); |
61 | | - } |
62 | | - |
63 | | - @Test |
64 | | - public void thirdTest() throws Exception { |
65 | | - incrementAndBlock(sharedResource, countDownLatch); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - public static class ConcurrentIncrementTestCase { |
70 | | - public static Set<String> threadNames; |
71 | | - static AtomicInteger sharedResource; |
72 | | - static CountDownLatch countDownLatch; |
| 31 | + public static final Set<String> threadNames = ConcurrentHashMap.newKeySet(); |
| 32 | + public static CountDownLatch countDownLatch; |
73 | 33 |
|
74 | 34 | @Rule |
75 | 35 | public final TestWatcher testWatcher = new TestWatcher() { |
76 | 36 | @Override |
77 | 37 | protected void starting(Description description) { |
78 | | - super.starting(description); |
79 | | - threadNames.add(Thread.currentThread().getName()); |
| 38 | + AbstractBlockingTestCase.threadNames.add(Thread.currentThread().getName()); |
80 | 39 | } |
81 | 40 | }; |
82 | 41 |
|
83 | | - @BeforeClass |
84 | | - public static void initialize() { |
85 | | - sharedResource = new AtomicInteger(); |
86 | | - countDownLatch = new CountDownLatch(3); |
87 | | - threadNames = new HashSet<>(); |
88 | | - } |
89 | | - |
90 | 42 | @Test |
91 | | - public void firstTest() throws Exception { |
92 | | - incrementAndBlock(sharedResource, countDownLatch); |
| 43 | + public void test() throws Exception { |
| 44 | + countDownAndBlock(countDownLatch); |
93 | 45 | } |
94 | 46 |
|
95 | | - @Test |
96 | | - public void secondTest() throws Exception { |
97 | | - incrementAndBlock(sharedResource, countDownLatch); |
| 47 | + @SuppressWarnings("ResultOfMethodCallIgnored") |
| 48 | + private static void countDownAndBlock(CountDownLatch countDownLatch) throws InterruptedException { |
| 49 | + countDownLatch.countDown(); |
| 50 | + countDownLatch.await(estimateSimulatedTestDurationInMilliseconds(), MILLISECONDS); |
98 | 51 | } |
99 | 52 |
|
100 | | - @Test |
101 | | - public void thirdTest() throws Exception { |
102 | | - incrementAndBlock(sharedResource, countDownLatch); |
| 53 | + private static long estimateSimulatedTestDurationInMilliseconds() { |
| 54 | + var runningInCi = Boolean.parseBoolean(System.getenv("CI")); |
| 55 | + return runningInCi ? 1000 : 100; |
103 | 56 | } |
104 | 57 | } |
105 | 58 |
|
106 | | - public static class AtomicOperationParallelTestCase { |
107 | | - public static Set<String> threadNames; |
108 | | - static AtomicInteger sharedResource; |
109 | | - static CountDownLatch countDownLatch; |
110 | | - |
111 | | - @Rule |
112 | | - public final TestWatcher testWatcher = new TestWatcher() { |
113 | | - @Override |
114 | | - protected void starting(Description description) { |
115 | | - super.starting(description); |
116 | | - threadNames.add(Thread.currentThread().getName()); |
117 | | - } |
118 | | - }; |
119 | | - |
120 | | - @BeforeClass |
121 | | - public static void initialize() { |
122 | | - sharedResource = new AtomicInteger(); |
123 | | - countDownLatch = new CountDownLatch(3); |
124 | | - threadNames = new HashSet<>(); |
125 | | - } |
126 | | - |
127 | | - @Test |
128 | | - public void firstTest() throws Exception { |
129 | | - incrementAndBlock(sharedResource, countDownLatch); |
130 | | - } |
131 | | - |
132 | | - @Test |
133 | | - public void secondTest() throws Exception { |
134 | | - incrementAndBlock(sharedResource, countDownLatch); |
135 | | - } |
136 | | - |
137 | | - @Test |
138 | | - public void thirdTest() throws Exception { |
139 | | - incrementAndBlock(sharedResource, countDownLatch); |
140 | | - } |
141 | | - } |
142 | | - |
143 | | - public static class FailingParallelTestCase { |
144 | | - public static Set<String> threadNames; |
145 | | - |
146 | | - @Rule |
147 | | - public final TestWatcher testWatcher = new TestWatcher() { |
148 | | - @Override |
149 | | - protected void starting(Description description) { |
150 | | - super.starting(description); |
151 | | - threadNames.add(Thread.currentThread().getName()); |
152 | | - } |
153 | | - }; |
154 | | - |
155 | | - @BeforeClass |
156 | | - public static void initialize() { |
157 | | - threadNames = new HashSet<>(); |
158 | | - } |
159 | | - |
160 | | - @Test |
161 | | - public void firstTest() { |
162 | | - fail("failing test"); |
163 | | - } |
164 | | - |
165 | | - @Test |
166 | | - public void secondTest() { |
167 | | - fail("failing test"); |
168 | | - } |
169 | | - |
170 | | - @Test |
171 | | - public void thirdTest() { |
172 | | - fail("failing test"); |
173 | | - } |
174 | | - } |
175 | | - |
176 | | - public static class ConcurrentFailureTestCase { |
177 | | - public static Set<String> threadNames; |
178 | | - |
179 | | - @Rule |
180 | | - public final TestWatcher testWatcher = new TestWatcher() { |
181 | | - @Override |
182 | | - protected void starting(Description description) { |
183 | | - super.starting(description); |
184 | | - threadNames.add(Thread.currentThread().getName()); |
185 | | - } |
186 | | - }; |
187 | | - |
188 | | - @BeforeClass |
189 | | - public static void initialize() { |
190 | | - threadNames = new HashSet<>(); |
191 | | - } |
192 | | - |
193 | | - @Test |
194 | | - public void firstTest() { |
195 | | - fail("failing test"); |
196 | | - } |
197 | | - |
198 | | - @Test |
199 | | - public void secondTest() { |
200 | | - fail("failing test"); |
201 | | - } |
202 | | - |
203 | | - @Test |
204 | | - public void thirdTest() { |
205 | | - fail("failing test"); |
206 | | - } |
207 | | - } |
208 | | - |
209 | | - public static class ParallelFailingTestCase { |
210 | | - public static Set<String> threadNames; |
211 | | - |
212 | | - @Rule |
213 | | - public final TestWatcher testWatcher = new TestWatcher() { |
214 | | - @Override |
215 | | - protected void starting(Description description) { |
216 | | - super.starting(description); |
217 | | - threadNames.add(Thread.currentThread().getName()); |
218 | | - } |
219 | | - }; |
220 | | - |
221 | | - @BeforeClass |
222 | | - public static void initialize() { |
223 | | - threadNames = new HashSet<>(); |
224 | | - } |
225 | | - |
226 | | - @Test |
227 | | - public void firstTest() { |
228 | | - fail("failing test"); |
229 | | - } |
230 | | - |
231 | | - @Test |
232 | | - public void secondTest() { |
233 | | - fail("failing test"); |
234 | | - } |
235 | | - |
236 | | - @Test |
237 | | - public void thirdTest() { |
238 | | - fail("failing test"); |
239 | | - } |
| 59 | + public static class FirstTestCase extends AbstractBlockingTestCase { |
240 | 60 | } |
241 | 61 |
|
242 | | - @SuppressWarnings("ResultOfMethodCallIgnored") |
243 | | - private static int incrementAndBlock(AtomicInteger sharedResource, CountDownLatch countDownLatch) |
244 | | - throws InterruptedException { |
245 | | - var value = sharedResource.incrementAndGet(); |
246 | | - countDownLatch.countDown(); |
247 | | - countDownLatch.await(estimateSimulatedTestDurationInMiliseconds(), MILLISECONDS); |
248 | | - return value; |
| 62 | + public static class SecondTestCase extends AbstractBlockingTestCase { |
249 | 63 | } |
250 | 64 |
|
251 | | - private static long estimateSimulatedTestDurationInMiliseconds() { |
252 | | - var runningInCi = Boolean.parseBoolean(System.getenv("CI")); |
253 | | - return runningInCi ? 1000 : 100; |
| 65 | + public static class ThirdTestCase extends AbstractBlockingTestCase { |
254 | 66 | } |
255 | 67 | } |
0 commit comments