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;
|
|
43 |
|
|
44 |
import junit.framework.Test;
|
|
45 |
import junit.framework.TestSuite;
|
|
46 |
|
|
47 |
public class CyclicBarrierTest extends JSR166TestCase {
|
|
48 |
public static void main(String[] args) {
|
|
49 |
main(suite(), args);
|
|
50 |
}
|
|
51 |
public static Test suite() {
|
|
52 |
return new TestSuite(CyclicBarrierTest.class);
|
|
53 |
}
|
|
54 |
|
|
55 |
private volatile int countAction;
|
|
56 |
private class MyAction implements Runnable {
|
|
57 |
public void run() { ++countAction; }
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Spin-waits till the number of waiters == numberOfWaiters.
|
|
62 |
*/
|
|
63 |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) {
|
|
64 |
long startTime = System.nanoTime();
|
|
65 |
while (barrier.getNumberWaiting() != numberOfWaiters) {
|
|
66 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS)
|
|
67 |
fail("timed out");
|
|
68 |
Thread.yield();
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Creating with negative parties throws IAE
|
|
74 |
*/
|
|
75 |
public void testConstructor1() {
|
|
76 |
try {
|
|
77 |
new CyclicBarrier(-1, (Runnable)null);
|
|
78 |
shouldThrow();
|
|
79 |
} catch (IllegalArgumentException success) {}
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Creating with negative parties and no action throws IAE
|
|
84 |
*/
|
|
85 |
public void testConstructor2() {
|
|
86 |
try {
|
|
87 |
new CyclicBarrier(-1);
|
|
88 |
shouldThrow();
|
|
89 |
} catch (IllegalArgumentException success) {}
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* getParties returns the number of parties given in constructor
|
|
94 |
*/
|
|
95 |
public void testGetParties() {
|
|
96 |
CyclicBarrier b = new CyclicBarrier(2);
|
|
97 |
assertEquals(2, b.getParties());
|
|
98 |
assertEquals(0, b.getNumberWaiting());
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* A 1-party barrier triggers after single await
|
|
103 |
*/
|
|
104 |
public void testSingleParty() throws Exception {
|
|
105 |
CyclicBarrier b = new CyclicBarrier(1);
|
|
106 |
assertEquals(1, b.getParties());
|
|
107 |
assertEquals(0, b.getNumberWaiting());
|
|
108 |
b.await();
|
|
109 |
b.await();
|
|
110 |
assertEquals(0, b.getNumberWaiting());
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* The supplied barrier action is run at barrier
|
|
115 |
*/
|
|
116 |
public void testBarrierAction() throws Exception {
|
|
117 |
countAction = 0;
|
|
118 |
CyclicBarrier b = new CyclicBarrier(1, new MyAction());
|
|
119 |
assertEquals(1, b.getParties());
|
|
120 |
assertEquals(0, b.getNumberWaiting());
|
|
121 |
b.await();
|
|
122 |
b.await();
|
|
123 |
assertEquals(0, b.getNumberWaiting());
|
|
124 |
assertEquals(2, countAction);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* A 2-party/thread barrier triggers after both threads invoke await
|
|
129 |
*/
|
|
130 |
public void testTwoParties() throws Exception {
|
|
131 |
final CyclicBarrier b = new CyclicBarrier(2);
|
|
132 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
133 |
public void realRun() throws Exception {
|
|
134 |
b.await();
|
|
135 |
b.await();
|
|
136 |
b.await();
|
|
137 |
b.await();
|
|
138 |
}});
|
|
139 |
|
|
140 |
b.await();
|
|
141 |
b.await();
|
|
142 |
b.await();
|
|
143 |
b.await();
|
|
144 |
awaitTermination(t);
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* An interruption in one party causes others waiting in await to
|
|
149 |
* throw BrokenBarrierException
|
|
150 |
*/
|
|
151 |
public void testAwait1_Interrupted_BrokenBarrier() {
|
|
152 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
153 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
|
|
154 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
|
|
155 |
public void realRun() throws Exception {
|
|
156 |
pleaseInterrupt.countDown();
|
|
157 |
c.await();
|
|
158 |
}};
|
|
159 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
160 |
public void realRun() throws Exception {
|
|
161 |
pleaseInterrupt.countDown();
|
|
162 |
c.await();
|
|
163 |
}};
|
|
164 |
|
|
165 |
t1.start();
|
|
166 |
t2.start();
|
|
167 |
await(pleaseInterrupt);
|
|
168 |
t1.interrupt();
|
|
169 |
awaitTermination(t1);
|
|
170 |
awaitTermination(t2);
|
|
171 |
}
|
|
172 |
|
|
173 |
/**
|
|
174 |
* An interruption in one party causes others waiting in timed await to
|
|
175 |
* throw BrokenBarrierException
|
|
176 |
*/
|
|
177 |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception {
|
|
178 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
179 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
|
|
180 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
|
|
181 |
public void realRun() throws Exception {
|
|
182 |
pleaseInterrupt.countDown();
|
|
183 |
c.await(LONG_DELAY_MS, MILLISECONDS);
|
|
184 |
}};
|
|
185 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
186 |
public void realRun() throws Exception {
|
|
187 |
pleaseInterrupt.countDown();
|
|
188 |
c.await(LONG_DELAY_MS, MILLISECONDS);
|
|
189 |
}};
|
|
190 |
|
|
191 |
t1.start();
|
|
192 |
t2.start();
|
|
193 |
await(pleaseInterrupt);
|
|
194 |
t1.interrupt();
|
|
195 |
awaitTermination(t1);
|
|
196 |
awaitTermination(t2);
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* A timeout in timed await throws TimeoutException
|
|
201 |
*/
|
|
202 |
public void testAwait3_TimeoutException() throws InterruptedException {
|
|
203 |
final CyclicBarrier c = new CyclicBarrier(2);
|
|
204 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
205 |
public void realRun() throws Exception {
|
|
206 |
long startTime = System.nanoTime();
|
|
207 |
try {
|
|
208 |
c.await(timeoutMillis(), MILLISECONDS);
|
|
209 |
shouldThrow();
|
|
210 |
} catch (TimeoutException success) {}
|
|
211 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
|
|
212 |
}});
|
|
213 |
|
|
214 |
awaitTermination(t);
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* A timeout in one party causes others waiting in timed await to
|
|
219 |
* throw BrokenBarrierException
|
|
220 |
*/
|
|
221 |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException {
|
|
222 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
223 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
224 |
public void realRun() throws Exception {
|
|
225 |
try {
|
|
226 |
c.await(LONG_DELAY_MS, MILLISECONDS);
|
|
227 |
shouldThrow();
|
|
228 |
} catch (BrokenBarrierException success) {}
|
|
229 |
}});
|
|
230 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
231 |
public void realRun() throws Exception {
|
|
232 |
awaitNumberWaiting(c, 1);
|
|
233 |
long startTime = System.nanoTime();
|
|
234 |
try {
|
|
235 |
c.await(timeoutMillis(), MILLISECONDS);
|
|
236 |
shouldThrow();
|
|
237 |
} catch (TimeoutException success) {}
|
|
238 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
|
|
239 |
}});
|
|
240 |
|
|
241 |
awaitTermination(t1);
|
|
242 |
awaitTermination(t2);
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* A timeout in one party causes others waiting in await to
|
|
247 |
* throw BrokenBarrierException
|
|
248 |
*/
|
|
249 |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException {
|
|
250 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
251 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
252 |
public void realRun() throws Exception {
|
|
253 |
try {
|
|
254 |
c.await();
|
|
255 |
shouldThrow();
|
|
256 |
} catch (BrokenBarrierException success) {}
|
|
257 |
}});
|
|
258 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
259 |
public void realRun() throws Exception {
|
|
260 |
awaitNumberWaiting(c, 1);
|
|
261 |
long startTime = System.nanoTime();
|
|
262 |
try {
|
|
263 |
c.await(timeoutMillis(), MILLISECONDS);
|
|
264 |
shouldThrow();
|
|
265 |
} catch (TimeoutException success) {}
|
|
266 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
|
|
267 |
}});
|
|
268 |
|
|
269 |
awaitTermination(t1);
|
|
270 |
awaitTermination(t2);
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* A reset of an active barrier causes waiting threads to throw
|
|
275 |
* BrokenBarrierException
|
|
276 |
*/
|
|
277 |
public void testReset_BrokenBarrier() throws InterruptedException {
|
|
278 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
279 |
final CountDownLatch pleaseReset = new CountDownLatch(2);
|
|
280 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
281 |
public void realRun() throws Exception {
|
|
282 |
pleaseReset.countDown();
|
|
283 |
c.await();
|
|
284 |
}};
|
|
285 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
286 |
public void realRun() throws Exception {
|
|
287 |
pleaseReset.countDown();
|
|
288 |
c.await();
|
|
289 |
}};
|
|
290 |
|
|
291 |
t1.start();
|
|
292 |
t2.start();
|
|
293 |
await(pleaseReset);
|
|
294 |
|
|
295 |
awaitNumberWaiting(c, 2);
|
|
296 |
c.reset();
|
|
297 |
awaitTermination(t1);
|
|
298 |
awaitTermination(t2);
|
|
299 |
}
|
|
300 |
|
|
301 |
/**
|
|
302 |
* A reset before threads enter barrier does not throw
|
|
303 |
* BrokenBarrierException
|
|
304 |
*/
|
|
305 |
public void testReset_NoBrokenBarrier() throws Exception {
|
|
306 |
final CyclicBarrier c = new CyclicBarrier(3);
|
|
307 |
c.reset();
|
|
308 |
|
|
309 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
310 |
public void realRun() throws Exception {
|
|
311 |
c.await();
|
|
312 |
}});
|
|
313 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
314 |
public void realRun() throws Exception {
|
|
315 |
c.await();
|
|
316 |
}});
|
|
317 |
|
|
318 |
c.await();
|
|
319 |
awaitTermination(t1);
|
|
320 |
awaitTermination(t2);
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* All threads block while a barrier is broken.
|
|
325 |
*/
|
|
326 |
public void testReset_Leakage() throws InterruptedException {
|
|
327 |
final CyclicBarrier c = new CyclicBarrier(2);
|
|
328 |
final AtomicBoolean done = new AtomicBoolean();
|
|
329 |
Thread t = newStartedThread(new CheckedRunnable() {
|
|
330 |
public void realRun() {
|
|
331 |
while (!done.get()) {
|
|
332 |
try {
|
|
333 |
while (c.isBroken())
|
|
334 |
c.reset();
|
|
335 |
|
|
336 |
c.await();
|
|
337 |
shouldThrow();
|
|
338 |
}
|
|
339 |
catch (BrokenBarrierException ok) {}
|
|
340 |
catch (InterruptedException ok) {}
|
|
341 |
}}});
|
|
342 |
|
|
343 |
for (int i = 0; i < 4; i++) {
|
|
344 |
delay(timeoutMillis());
|
|
345 |
t.interrupt();
|
|
346 |
}
|
|
347 |
done.set(true);
|
|
348 |
t.interrupt();
|
|
349 |
awaitTermination(t);
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* Reset of a non-broken barrier does not break barrier
|
|
354 |
*/
|
|
355 |
public void testResetWithoutBreakage() throws Exception {
|
|
356 |
final CyclicBarrier barrier = new CyclicBarrier(3);
|
|
357 |
for (int i = 0; i < 3; i++) {
|
|
358 |
final CyclicBarrier start = new CyclicBarrier(3);
|
|
359 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
360 |
public void realRun() throws Exception {
|
|
361 |
start.await();
|
|
362 |
barrier.await();
|
|
363 |
}});
|
|
364 |
|
|
365 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
366 |
public void realRun() throws Exception {
|
|
367 |
start.await();
|
|
368 |
barrier.await();
|
|
369 |
}});
|
|
370 |
|
|
371 |
start.await();
|
|
372 |
barrier.await();
|
|
373 |
awaitTermination(t1);
|
|
374 |
awaitTermination(t2);
|
|
375 |
assertFalse(barrier.isBroken());
|
|
376 |
assertEquals(0, barrier.getNumberWaiting());
|
|
377 |
if (i == 1) barrier.reset();
|
|
378 |
assertFalse(barrier.isBroken());
|
|
379 |
assertEquals(0, barrier.getNumberWaiting());
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
/**
|
|
384 |
* Reset of a barrier after interruption reinitializes it.
|
|
385 |
*/
|
|
386 |
public void testResetAfterInterrupt() throws Exception {
|
|
387 |
final CyclicBarrier barrier = new CyclicBarrier(3);
|
|
388 |
for (int i = 0; i < 2; i++) {
|
|
389 |
final CyclicBarrier start = new CyclicBarrier(3);
|
|
390 |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
|
|
391 |
public void realRun() throws Exception {
|
|
392 |
start.await();
|
|
393 |
barrier.await();
|
|
394 |
}};
|
|
395 |
|
|
396 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
397 |
public void realRun() throws Exception {
|
|
398 |
start.await();
|
|
399 |
barrier.await();
|
|
400 |
}};
|
|
401 |
|
|
402 |
t1.start();
|
|
403 |
t2.start();
|
|
404 |
start.await();
|
|
405 |
t1.interrupt();
|
|
406 |
awaitTermination(t1);
|
|
407 |
awaitTermination(t2);
|
|
408 |
assertTrue(barrier.isBroken());
|
|
409 |
assertEquals(0, barrier.getNumberWaiting());
|
|
410 |
barrier.reset();
|
|
411 |
assertFalse(barrier.isBroken());
|
|
412 |
assertEquals(0, barrier.getNumberWaiting());
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Reset of a barrier after timeout reinitializes it.
|
|
418 |
*/
|
|
419 |
public void testResetAfterTimeout() throws Exception {
|
|
420 |
final CyclicBarrier barrier = new CyclicBarrier(3);
|
|
421 |
for (int i = 0; i < 2; i++) {
|
|
422 |
assertEquals(0, barrier.getNumberWaiting());
|
|
423 |
Thread t1 = newStartedThread(new CheckedRunnable() {
|
|
424 |
public void realRun() throws Exception {
|
|
425 |
try {
|
|
426 |
barrier.await();
|
|
427 |
shouldThrow();
|
|
428 |
} catch (BrokenBarrierException success) {}
|
|
429 |
}});
|
|
430 |
Thread t2 = newStartedThread(new CheckedRunnable() {
|
|
431 |
public void realRun() throws Exception {
|
|
432 |
awaitNumberWaiting(barrier, 1);
|
|
433 |
long startTime = System.nanoTime();
|
|
434 |
try {
|
|
435 |
barrier.await(timeoutMillis(), MILLISECONDS);
|
|
436 |
shouldThrow();
|
|
437 |
} catch (TimeoutException success) {}
|
|
438 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
|
|
439 |
}});
|
|
440 |
|
|
441 |
awaitTermination(t1);
|
|
442 |
awaitTermination(t2);
|
|
443 |
assertEquals(0, barrier.getNumberWaiting());
|
|
444 |
assertTrue(barrier.isBroken());
|
|
445 |
assertEquals(0, barrier.getNumberWaiting());
|
|
446 |
barrier.reset();
|
|
447 |
assertFalse(barrier.isBroken());
|
|
448 |
assertEquals(0, barrier.getNumberWaiting());
|
|
449 |
}
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* Reset of a barrier after a failed command reinitializes it.
|
|
454 |
*/
|
|
455 |
public void testResetAfterCommandException() throws Exception {
|
|
456 |
final CyclicBarrier barrier =
|
|
457 |
new CyclicBarrier(3, new Runnable() {
|
|
458 |
public void run() {
|
|
459 |
throw new NullPointerException(); }});
|
|
460 |
for (int i = 0; i < 2; i++) {
|
|
461 |
final CyclicBarrier start = new CyclicBarrier(3);
|
|
462 |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
463 |
public void realRun() throws Exception {
|
|
464 |
start.await();
|
|
465 |
barrier.await();
|
|
466 |
}};
|
|
467 |
|
|
468 |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
|
|
469 |
public void realRun() throws Exception {
|
|
470 |
start.await();
|
|
471 |
barrier.await();
|
|
472 |
}};
|
|
473 |
|
|
474 |
t1.start();
|
|
475 |
t2.start();
|
|
476 |
start.await();
|
|
477 |
awaitNumberWaiting(barrier, 2);
|
|
478 |
try {
|
|
479 |
barrier.await();
|
|
480 |
shouldThrow();
|
|
481 |
} catch (NullPointerException success) {}
|
|
482 |
awaitTermination(t1);
|
|
483 |
awaitTermination(t2);
|
|
484 |
assertTrue(barrier.isBroken());
|
|
485 |
assertEquals(0, barrier.getNumberWaiting());
|
|
486 |
barrier.reset();
|
|
487 |
assertFalse(barrier.isBroken());
|
|
488 |
assertEquals(0, barrier.getNumberWaiting());
|
|
489 |
}
|
|
490 |
}
|
|
491 |
}
|