author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
37 |
||
38 |
import java.util.concurrent.BrokenBarrierException; |
|
39 |
import java.util.concurrent.CountDownLatch; |
|
40 |
import java.util.concurrent.CyclicBarrier; |
|
41 |
import java.util.concurrent.TimeoutException; |
|
42 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
43 |
import java.util.concurrent.atomic.AtomicInteger; |
35394 | 44 |
|
45 |
import junit.framework.Test; |
|
46 |
import junit.framework.TestSuite; |
|
47 |
||
48 |
public class CyclicBarrierTest extends JSR166TestCase { |
|
49 |
public static void main(String[] args) { |
|
50 |
main(suite(), args); |
|
51 |
} |
|
52 |
public static Test suite() { |
|
53 |
return new TestSuite(CyclicBarrierTest.class); |
|
54 |
} |
|
55 |
||
56 |
/** |
|
57 |
* Spin-waits till the number of waiters == numberOfWaiters. |
|
58 |
*/ |
|
59 |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
|
60 |
long startTime = System.nanoTime(); |
|
61 |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
|
62 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
|
63 |
fail("timed out"); |
|
64 |
Thread.yield(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
41131
diff
changeset
|
69 |
* Creating with negative parties throws IllegalArgumentException |
35394 | 70 |
*/ |
71 |
public void testConstructor1() { |
|
72 |
try { |
|
73 |
new CyclicBarrier(-1, (Runnable)null); |
|
74 |
shouldThrow(); |
|
75 |
} catch (IllegalArgumentException success) {} |
|
76 |
} |
|
77 |
||
78 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
41131
diff
changeset
|
79 |
* Creating with negative parties and no action throws |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
41131
diff
changeset
|
80 |
* IllegalArgumentException |
35394 | 81 |
*/ |
82 |
public void testConstructor2() { |
|
83 |
try { |
|
84 |
new CyclicBarrier(-1); |
|
85 |
shouldThrow(); |
|
86 |
} catch (IllegalArgumentException success) {} |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* getParties returns the number of parties given in constructor |
|
91 |
*/ |
|
92 |
public void testGetParties() { |
|
93 |
CyclicBarrier b = new CyclicBarrier(2); |
|
94 |
assertEquals(2, b.getParties()); |
|
95 |
assertEquals(0, b.getNumberWaiting()); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* A 1-party barrier triggers after single await |
|
100 |
*/ |
|
101 |
public void testSingleParty() throws Exception { |
|
102 |
CyclicBarrier b = new CyclicBarrier(1); |
|
103 |
assertEquals(1, b.getParties()); |
|
104 |
assertEquals(0, b.getNumberWaiting()); |
|
105 |
b.await(); |
|
106 |
b.await(); |
|
107 |
assertEquals(0, b.getNumberWaiting()); |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* The supplied barrier action is run at barrier |
|
112 |
*/ |
|
113 |
public void testBarrierAction() throws Exception { |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
114 |
final AtomicInteger count = new AtomicInteger(0); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
115 |
final Runnable incCount = new Runnable() { public void run() { |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
116 |
count.getAndIncrement(); }}; |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
117 |
CyclicBarrier b = new CyclicBarrier(1, incCount); |
35394 | 118 |
assertEquals(1, b.getParties()); |
119 |
assertEquals(0, b.getNumberWaiting()); |
|
120 |
b.await(); |
|
121 |
b.await(); |
|
122 |
assertEquals(0, b.getNumberWaiting()); |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
123 |
assertEquals(2, count.get()); |
35394 | 124 |
} |
125 |
||
126 |
/** |
|
127 |
* A 2-party/thread barrier triggers after both threads invoke await |
|
128 |
*/ |
|
129 |
public void testTwoParties() throws Exception { |
|
130 |
final CyclicBarrier b = new CyclicBarrier(2); |
|
131 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
132 |
public void realRun() throws Exception { |
|
133 |
b.await(); |
|
134 |
b.await(); |
|
135 |
b.await(); |
|
136 |
b.await(); |
|
137 |
}}); |
|
138 |
||
139 |
b.await(); |
|
140 |
b.await(); |
|
141 |
b.await(); |
|
142 |
b.await(); |
|
143 |
awaitTermination(t); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* An interruption in one party causes others waiting in await to |
|
148 |
* throw BrokenBarrierException |
|
149 |
*/ |
|
150 |
public void testAwait1_Interrupted_BrokenBarrier() { |
|
151 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
152 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
|
153 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
|
154 |
public void realRun() throws Exception { |
|
155 |
pleaseInterrupt.countDown(); |
|
156 |
c.await(); |
|
157 |
}}; |
|
158 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
159 |
public void realRun() throws Exception { |
|
160 |
pleaseInterrupt.countDown(); |
|
161 |
c.await(); |
|
162 |
}}; |
|
163 |
||
164 |
t1.start(); |
|
165 |
t2.start(); |
|
166 |
await(pleaseInterrupt); |
|
167 |
t1.interrupt(); |
|
168 |
awaitTermination(t1); |
|
169 |
awaitTermination(t2); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* An interruption in one party causes others waiting in timed await to |
|
174 |
* throw BrokenBarrierException |
|
175 |
*/ |
|
176 |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
|
177 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
178 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
|
179 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
|
180 |
public void realRun() throws Exception { |
|
181 |
pleaseInterrupt.countDown(); |
|
182 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
|
183 |
}}; |
|
184 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
185 |
public void realRun() throws Exception { |
|
186 |
pleaseInterrupt.countDown(); |
|
187 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
|
188 |
}}; |
|
189 |
||
190 |
t1.start(); |
|
191 |
t2.start(); |
|
192 |
await(pleaseInterrupt); |
|
193 |
t1.interrupt(); |
|
194 |
awaitTermination(t1); |
|
195 |
awaitTermination(t2); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* A timeout in timed await throws TimeoutException |
|
200 |
*/ |
|
201 |
public void testAwait3_TimeoutException() throws InterruptedException { |
|
202 |
final CyclicBarrier c = new CyclicBarrier(2); |
|
203 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
204 |
public void realRun() throws Exception { |
|
205 |
long startTime = System.nanoTime(); |
|
206 |
try { |
|
207 |
c.await(timeoutMillis(), MILLISECONDS); |
|
208 |
shouldThrow(); |
|
209 |
} catch (TimeoutException success) {} |
|
210 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
211 |
}}); |
|
212 |
||
213 |
awaitTermination(t); |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* A timeout in one party causes others waiting in timed await to |
|
218 |
* throw BrokenBarrierException |
|
219 |
*/ |
|
220 |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
|
221 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
222 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
|
223 |
public void realRun() throws Exception { |
|
224 |
try { |
|
225 |
c.await(LONG_DELAY_MS, MILLISECONDS); |
|
226 |
shouldThrow(); |
|
227 |
} catch (BrokenBarrierException success) {} |
|
228 |
}}); |
|
229 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
|
230 |
public void realRun() throws Exception { |
|
231 |
awaitNumberWaiting(c, 1); |
|
232 |
long startTime = System.nanoTime(); |
|
233 |
try { |
|
234 |
c.await(timeoutMillis(), MILLISECONDS); |
|
235 |
shouldThrow(); |
|
236 |
} catch (TimeoutException success) {} |
|
237 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
238 |
}}); |
|
239 |
||
240 |
awaitTermination(t1); |
|
241 |
awaitTermination(t2); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* A timeout in one party causes others waiting in await to |
|
246 |
* throw BrokenBarrierException |
|
247 |
*/ |
|
248 |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
|
249 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
250 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
|
251 |
public void realRun() throws Exception { |
|
252 |
try { |
|
253 |
c.await(); |
|
254 |
shouldThrow(); |
|
255 |
} catch (BrokenBarrierException success) {} |
|
256 |
}}); |
|
257 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
|
258 |
public void realRun() throws Exception { |
|
259 |
awaitNumberWaiting(c, 1); |
|
260 |
long startTime = System.nanoTime(); |
|
261 |
try { |
|
262 |
c.await(timeoutMillis(), MILLISECONDS); |
|
263 |
shouldThrow(); |
|
264 |
} catch (TimeoutException success) {} |
|
265 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
266 |
}}); |
|
267 |
||
268 |
awaitTermination(t1); |
|
269 |
awaitTermination(t2); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* A reset of an active barrier causes waiting threads to throw |
|
274 |
* BrokenBarrierException |
|
275 |
*/ |
|
276 |
public void testReset_BrokenBarrier() throws InterruptedException { |
|
277 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
278 |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
|
279 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
280 |
public void realRun() throws Exception { |
|
281 |
pleaseReset.countDown(); |
|
282 |
c.await(); |
|
283 |
}}; |
|
284 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
285 |
public void realRun() throws Exception { |
|
286 |
pleaseReset.countDown(); |
|
287 |
c.await(); |
|
288 |
}}; |
|
289 |
||
290 |
t1.start(); |
|
291 |
t2.start(); |
|
292 |
await(pleaseReset); |
|
293 |
||
294 |
awaitNumberWaiting(c, 2); |
|
295 |
c.reset(); |
|
296 |
awaitTermination(t1); |
|
297 |
awaitTermination(t2); |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* A reset before threads enter barrier does not throw |
|
302 |
* BrokenBarrierException |
|
303 |
*/ |
|
304 |
public void testReset_NoBrokenBarrier() throws Exception { |
|
305 |
final CyclicBarrier c = new CyclicBarrier(3); |
|
306 |
c.reset(); |
|
307 |
||
308 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
|
309 |
public void realRun() throws Exception { |
|
310 |
c.await(); |
|
311 |
}}); |
|
312 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
|
313 |
public void realRun() throws Exception { |
|
314 |
c.await(); |
|
315 |
}}); |
|
316 |
||
317 |
c.await(); |
|
318 |
awaitTermination(t1); |
|
319 |
awaitTermination(t2); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* All threads block while a barrier is broken. |
|
324 |
*/ |
|
325 |
public void testReset_Leakage() throws InterruptedException { |
|
326 |
final CyclicBarrier c = new CyclicBarrier(2); |
|
327 |
final AtomicBoolean done = new AtomicBoolean(); |
|
328 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
329 |
public void realRun() { |
|
330 |
while (!done.get()) { |
|
331 |
try { |
|
332 |
while (c.isBroken()) |
|
333 |
c.reset(); |
|
334 |
||
335 |
c.await(); |
|
336 |
shouldThrow(); |
|
337 |
} |
|
338 |
catch (BrokenBarrierException ok) {} |
|
339 |
catch (InterruptedException ok) {} |
|
340 |
}}}); |
|
341 |
||
342 |
for (int i = 0; i < 4; i++) { |
|
343 |
delay(timeoutMillis()); |
|
344 |
t.interrupt(); |
|
345 |
} |
|
346 |
done.set(true); |
|
347 |
t.interrupt(); |
|
348 |
awaitTermination(t); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Reset of a non-broken barrier does not break barrier |
|
353 |
*/ |
|
354 |
public void testResetWithoutBreakage() throws Exception { |
|
355 |
final CyclicBarrier barrier = new CyclicBarrier(3); |
|
356 |
for (int i = 0; i < 3; i++) { |
|
357 |
final CyclicBarrier start = new CyclicBarrier(3); |
|
358 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
|
359 |
public void realRun() throws Exception { |
|
360 |
start.await(); |
|
361 |
barrier.await(); |
|
362 |
}}); |
|
363 |
||
364 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
|
365 |
public void realRun() throws Exception { |
|
366 |
start.await(); |
|
367 |
barrier.await(); |
|
368 |
}}); |
|
369 |
||
370 |
start.await(); |
|
371 |
barrier.await(); |
|
372 |
awaitTermination(t1); |
|
373 |
awaitTermination(t2); |
|
374 |
assertFalse(barrier.isBroken()); |
|
375 |
assertEquals(0, barrier.getNumberWaiting()); |
|
376 |
if (i == 1) barrier.reset(); |
|
377 |
assertFalse(barrier.isBroken()); |
|
378 |
assertEquals(0, barrier.getNumberWaiting()); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Reset of a barrier after interruption reinitializes it. |
|
384 |
*/ |
|
385 |
public void testResetAfterInterrupt() throws Exception { |
|
386 |
final CyclicBarrier barrier = new CyclicBarrier(3); |
|
387 |
for (int i = 0; i < 2; i++) { |
|
388 |
final CyclicBarrier start = new CyclicBarrier(3); |
|
389 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
|
390 |
public void realRun() throws Exception { |
|
391 |
start.await(); |
|
392 |
barrier.await(); |
|
393 |
}}; |
|
394 |
||
395 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
396 |
public void realRun() throws Exception { |
|
397 |
start.await(); |
|
398 |
barrier.await(); |
|
399 |
}}; |
|
400 |
||
401 |
t1.start(); |
|
402 |
t2.start(); |
|
403 |
start.await(); |
|
404 |
t1.interrupt(); |
|
405 |
awaitTermination(t1); |
|
406 |
awaitTermination(t2); |
|
407 |
assertTrue(barrier.isBroken()); |
|
408 |
assertEquals(0, barrier.getNumberWaiting()); |
|
409 |
barrier.reset(); |
|
410 |
assertFalse(barrier.isBroken()); |
|
411 |
assertEquals(0, barrier.getNumberWaiting()); |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
/** |
|
416 |
* Reset of a barrier after timeout reinitializes it. |
|
417 |
*/ |
|
418 |
public void testResetAfterTimeout() throws Exception { |
|
419 |
final CyclicBarrier barrier = new CyclicBarrier(3); |
|
420 |
for (int i = 0; i < 2; i++) { |
|
421 |
assertEquals(0, barrier.getNumberWaiting()); |
|
422 |
Thread t1 = newStartedThread(new CheckedRunnable() { |
|
423 |
public void realRun() throws Exception { |
|
424 |
try { |
|
425 |
barrier.await(); |
|
426 |
shouldThrow(); |
|
427 |
} catch (BrokenBarrierException success) {} |
|
428 |
}}); |
|
429 |
Thread t2 = newStartedThread(new CheckedRunnable() { |
|
430 |
public void realRun() throws Exception { |
|
431 |
awaitNumberWaiting(barrier, 1); |
|
432 |
long startTime = System.nanoTime(); |
|
433 |
try { |
|
434 |
barrier.await(timeoutMillis(), MILLISECONDS); |
|
435 |
shouldThrow(); |
|
436 |
} catch (TimeoutException success) {} |
|
437 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
438 |
}}); |
|
439 |
||
440 |
awaitTermination(t1); |
|
441 |
awaitTermination(t2); |
|
442 |
assertEquals(0, barrier.getNumberWaiting()); |
|
443 |
assertTrue(barrier.isBroken()); |
|
444 |
assertEquals(0, barrier.getNumberWaiting()); |
|
445 |
barrier.reset(); |
|
446 |
assertFalse(barrier.isBroken()); |
|
447 |
assertEquals(0, barrier.getNumberWaiting()); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* Reset of a barrier after a failed command reinitializes it. |
|
453 |
*/ |
|
454 |
public void testResetAfterCommandException() throws Exception { |
|
455 |
final CyclicBarrier barrier = |
|
456 |
new CyclicBarrier(3, new Runnable() { |
|
457 |
public void run() { |
|
458 |
throw new NullPointerException(); }}); |
|
459 |
for (int i = 0; i < 2; i++) { |
|
460 |
final CyclicBarrier start = new CyclicBarrier(3); |
|
461 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
462 |
public void realRun() throws Exception { |
|
463 |
start.await(); |
|
464 |
barrier.await(); |
|
465 |
}}; |
|
466 |
||
467 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
|
468 |
public void realRun() throws Exception { |
|
469 |
start.await(); |
|
470 |
barrier.await(); |
|
471 |
}}; |
|
472 |
||
473 |
t1.start(); |
|
474 |
t2.start(); |
|
475 |
start.await(); |
|
476 |
awaitNumberWaiting(barrier, 2); |
|
477 |
try { |
|
478 |
barrier.await(); |
|
479 |
shouldThrow(); |
|
480 |
} catch (NullPointerException success) {} |
|
481 |
awaitTermination(t1); |
|
482 |
awaitTermination(t2); |
|
483 |
assertTrue(barrier.isBroken()); |
|
484 |
assertEquals(0, barrier.getNumberWaiting()); |
|
485 |
barrier.reset(); |
|
486 |
assertFalse(barrier.isBroken()); |
|
487 |
assertEquals(0, barrier.getNumberWaiting()); |
|
488 |
} |
|
489 |
} |
|
490 |
} |