11 |
|
import java.util.concurrent.*; |
12 |
|
import java.util.concurrent.locks.*; |
13 |
|
import java.util.concurrent.atomic.*; |
14 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
|
|
16 |
< |
public class CyclicBarrierTest extends JSR166TestCase{ |
16 |
> |
public class CyclicBarrierTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(CyclicBarrierTest.class); |
21 |
> |
return new TestSuite(CyclicBarrierTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
private volatile int countAction; |
33 |
|
try { |
34 |
|
new CyclicBarrier(-1, (Runnable)null); |
35 |
|
shouldThrow(); |
36 |
< |
} catch(IllegalArgumentException e){} |
36 |
> |
} catch (IllegalArgumentException e) {} |
37 |
|
} |
38 |
|
|
39 |
|
/** |
43 |
|
try { |
44 |
|
new CyclicBarrier(-1); |
45 |
|
shouldThrow(); |
46 |
< |
} catch(IllegalArgumentException e){} |
46 |
> |
} catch (IllegalArgumentException e) {} |
47 |
|
} |
48 |
|
|
49 |
|
/** |
51 |
|
*/ |
52 |
|
public void testGetParties() { |
53 |
|
CyclicBarrier b = new CyclicBarrier(2); |
54 |
< |
assertEquals(2, b.getParties()); |
54 |
> |
assertEquals(2, b.getParties()); |
55 |
|
assertEquals(0, b.getNumberWaiting()); |
56 |
|
} |
57 |
|
|
67 |
|
b.await(); |
68 |
|
assertEquals(0, b.getNumberWaiting()); |
69 |
|
} |
70 |
< |
catch(Exception e) { |
70 |
> |
catch (Exception e) { |
71 |
|
unexpectedException(); |
72 |
|
} |
73 |
|
} |
86 |
|
assertEquals(0, b.getNumberWaiting()); |
87 |
|
assertEquals(countAction, 2); |
88 |
|
} |
89 |
< |
catch(Exception e) { |
89 |
> |
catch (Exception e) { |
90 |
|
unexpectedException(); |
91 |
|
} |
92 |
|
} |
96 |
|
*/ |
97 |
|
public void testTwoParties() { |
98 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
99 |
< |
Thread t = new Thread(new Runnable() { |
100 |
< |
public void run() { |
99 |
> |
Thread t = new Thread(new Runnable() { |
100 |
> |
public void run() { |
101 |
|
try { |
102 |
|
b.await(); |
103 |
|
b.await(); |
104 |
|
b.await(); |
105 |
|
b.await(); |
106 |
< |
} catch(Exception e){ |
106 |
> |
} catch (Exception e) { |
107 |
|
threadUnexpectedException(); |
108 |
|
}}}); |
109 |
|
|
114 |
|
b.await(); |
115 |
|
b.await(); |
116 |
|
t.join(); |
117 |
< |
} catch(Exception e){ |
117 |
> |
} catch (Exception e) { |
118 |
|
unexpectedException(); |
119 |
|
} |
120 |
|
} |
131 |
|
try { |
132 |
|
c.await(); |
133 |
|
threadShouldThrow(); |
134 |
< |
} catch(InterruptedException success){} |
135 |
< |
catch(Exception b){ |
134 |
> |
} catch (InterruptedException success) {} |
135 |
> |
catch (Exception b) { |
136 |
|
threadUnexpectedException(); |
137 |
|
} |
138 |
|
} |
142 |
|
try { |
143 |
|
c.await(); |
144 |
|
threadShouldThrow(); |
145 |
< |
} catch(BrokenBarrierException success){ |
146 |
< |
} catch(Exception i){ |
145 |
> |
} catch (BrokenBarrierException success) { |
146 |
> |
} catch (Exception i) { |
147 |
|
threadUnexpectedException(); |
148 |
|
} |
149 |
|
} |
155 |
|
t1.interrupt(); |
156 |
|
t1.join(); |
157 |
|
t2.join(); |
158 |
< |
} catch(InterruptedException e){ |
158 |
> |
} catch (InterruptedException e) { |
159 |
|
unexpectedException(); |
160 |
|
} |
161 |
|
} |
169 |
|
Thread t1 = new Thread(new Runnable() { |
170 |
|
public void run() { |
171 |
|
try { |
172 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
172 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
173 |
|
threadShouldThrow(); |
174 |
< |
} catch(InterruptedException success){ |
175 |
< |
} catch(Exception b){ |
174 |
> |
} catch (InterruptedException success) { |
175 |
> |
} catch (Exception b) { |
176 |
|
threadUnexpectedException(); |
177 |
|
} |
178 |
|
} |
180 |
|
Thread t2 = new Thread(new Runnable() { |
181 |
|
public void run() { |
182 |
|
try { |
183 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
183 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
184 |
|
threadShouldThrow(); |
185 |
< |
} catch(BrokenBarrierException success){ |
186 |
< |
} catch(Exception i){ |
185 |
> |
} catch (BrokenBarrierException success) { |
186 |
> |
} catch (Exception i) { |
187 |
|
threadUnexpectedException(); |
188 |
|
} |
189 |
|
} |
195 |
|
t1.interrupt(); |
196 |
|
t1.join(); |
197 |
|
t2.join(); |
198 |
< |
} catch(InterruptedException e){ |
198 |
> |
} catch (InterruptedException e) { |
199 |
|
unexpectedException(); |
200 |
|
} |
201 |
|
} |
208 |
|
Thread t = new Thread(new Runnable() { |
209 |
|
public void run() { |
210 |
|
try { |
211 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
211 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
212 |
|
threadShouldThrow(); |
213 |
< |
} catch(TimeoutException success){ |
214 |
< |
} catch(Exception b){ |
213 |
> |
} catch (TimeoutException success) { |
214 |
> |
} catch (Exception b) { |
215 |
|
threadUnexpectedException(); |
216 |
|
|
217 |
|
} |
220 |
|
try { |
221 |
|
t.start(); |
222 |
|
t.join(); |
223 |
< |
} catch(InterruptedException e){ |
223 |
> |
} catch (InterruptedException e) { |
224 |
|
unexpectedException(); |
225 |
|
} |
226 |
|
} |
234 |
|
Thread t1 = new Thread(new Runnable() { |
235 |
|
public void run() { |
236 |
|
try { |
237 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
237 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
238 |
|
threadShouldThrow(); |
239 |
< |
} catch(TimeoutException success){ |
240 |
< |
} catch(Exception b){ |
239 |
> |
} catch (TimeoutException success) { |
240 |
> |
} catch (Exception b) { |
241 |
|
threadUnexpectedException(); |
242 |
|
} |
243 |
|
} |
245 |
|
Thread t2 = new Thread(new Runnable() { |
246 |
|
public void run() { |
247 |
|
try { |
248 |
< |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
248 |
> |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
249 |
|
threadShouldThrow(); |
250 |
< |
} catch(BrokenBarrierException success){ |
251 |
< |
} catch(Exception i){ |
250 |
> |
} catch (BrokenBarrierException success) { |
251 |
> |
} catch (Exception i) { |
252 |
|
threadUnexpectedException(); |
253 |
|
} |
254 |
|
} |
258 |
|
t2.start(); |
259 |
|
t1.join(); |
260 |
|
t2.join(); |
261 |
< |
} catch(InterruptedException e){ |
261 |
> |
} catch (InterruptedException e) { |
262 |
|
unexpectedException(); |
263 |
|
} |
264 |
|
} |
272 |
|
Thread t1 = new Thread(new Runnable() { |
273 |
|
public void run() { |
274 |
|
try { |
275 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
275 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
276 |
|
threadShouldThrow(); |
277 |
< |
} catch(TimeoutException success){ |
278 |
< |
} catch(Exception b){ |
277 |
> |
} catch (TimeoutException success) { |
278 |
> |
} catch (Exception b) { |
279 |
|
threadUnexpectedException(); |
280 |
|
} |
281 |
|
} |
285 |
|
try { |
286 |
|
c.await(); |
287 |
|
threadShouldThrow(); |
288 |
< |
} catch(BrokenBarrierException success){ |
289 |
< |
} catch(Exception i){ |
288 |
> |
} catch (BrokenBarrierException success) { |
289 |
> |
} catch (Exception i) { |
290 |
|
threadUnexpectedException(); |
291 |
|
} |
292 |
|
} |
296 |
|
t2.start(); |
297 |
|
t1.join(); |
298 |
|
t2.join(); |
299 |
< |
} catch(InterruptedException e){ |
299 |
> |
} catch (InterruptedException e) { |
300 |
|
unexpectedException(); |
301 |
|
} |
302 |
|
} |
312 |
|
try { |
313 |
|
c.await(); |
314 |
|
threadShouldThrow(); |
315 |
< |
} catch(BrokenBarrierException success){} |
316 |
< |
catch(Exception b){ |
315 |
> |
} catch (BrokenBarrierException success) {} |
316 |
> |
catch (Exception b) { |
317 |
|
threadUnexpectedException(); |
318 |
|
} |
319 |
|
} |
323 |
|
try { |
324 |
|
c.await(); |
325 |
|
threadShouldThrow(); |
326 |
< |
} catch(BrokenBarrierException success){ |
327 |
< |
} catch(Exception i){ |
326 |
> |
} catch (BrokenBarrierException success) { |
327 |
> |
} catch (Exception i) { |
328 |
|
threadUnexpectedException(); |
329 |
|
} |
330 |
|
} |
336 |
|
c.reset(); |
337 |
|
t1.join(); |
338 |
|
t2.join(); |
339 |
< |
} catch(InterruptedException e){ |
339 |
> |
} catch (InterruptedException e) { |
340 |
|
unexpectedException(); |
341 |
|
} |
342 |
|
} |
351 |
|
public void run() { |
352 |
|
try { |
353 |
|
c.await(); |
354 |
< |
} catch(Exception b){ |
354 |
> |
} catch (Exception b) { |
355 |
|
threadUnexpectedException(); |
356 |
|
} |
357 |
|
} |
360 |
|
public void run() { |
361 |
|
try { |
362 |
|
c.await(); |
363 |
< |
} catch(Exception i){ |
363 |
> |
} catch (Exception i) { |
364 |
|
threadUnexpectedException(); |
365 |
|
} |
366 |
|
} |
372 |
|
c.await(); |
373 |
|
t1.join(); |
374 |
|
t2.join(); |
375 |
< |
} catch(Exception e){ |
375 |
> |
} catch (Exception e) { |
376 |
|
unexpectedException(); |
377 |
|
} |
378 |
|
} |
403 |
|
}; |
404 |
|
|
405 |
|
t.start(); |
406 |
< |
for( int i = 0; i < 4; i++) { |
406 |
> |
for ( int i = 0; i < 4; i++) { |
407 |
|
Thread.sleep(SHORT_DELAY_MS); |
408 |
|
t.interrupt(); |
409 |
|
} |
480 |
|
threadFail("start barrier"); |
481 |
|
} |
482 |
|
try { barrier.await(); } |
483 |
< |
catch(InterruptedException ok) {} |
483 |
> |
catch (InterruptedException ok) {} |
484 |
|
catch (Throwable thrown) { |
485 |
|
unexpectedException(); |
486 |
|
}}}); |
492 |
|
threadFail("start barrier"); |
493 |
|
} |
494 |
|
try { barrier.await(); } |
495 |
< |
catch(BrokenBarrierException ok) {} |
495 |
> |
catch (BrokenBarrierException ok) {} |
496 |
|
catch (Throwable thrown) { |
497 |
|
unexpectedException(); |
498 |
|
}}}); |
530 |
|
catch (Exception ie) { |
531 |
|
threadFail("start barrier"); |
532 |
|
} |
533 |
< |
try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } |
534 |
< |
catch(TimeoutException ok) {} |
533 |
> |
try { barrier.await(MEDIUM_DELAY_MS, MILLISECONDS); } |
534 |
> |
catch (TimeoutException ok) {} |
535 |
|
catch (Throwable thrown) { |
536 |
|
unexpectedException(); |
537 |
|
}}}); |
543 |
|
threadFail("start barrier"); |
544 |
|
} |
545 |
|
try { barrier.await(); } |
546 |
< |
catch(BrokenBarrierException ok) {} |
546 |
> |
catch (BrokenBarrierException ok) {} |
547 |
|
catch (Throwable thrown) { |
548 |
|
unexpectedException(); |
549 |
|
}}}); |
585 |
|
threadFail("start barrier"); |
586 |
|
} |
587 |
|
try { barrier.await(); } |
588 |
< |
catch(BrokenBarrierException ok) {} |
588 |
> |
catch (BrokenBarrierException ok) {} |
589 |
|
catch (Throwable thrown) { |
590 |
|
unexpectedException(); |
591 |
|
}}}); |
597 |
|
threadFail("start barrier"); |
598 |
|
} |
599 |
|
try { barrier.await(); } |
600 |
< |
catch(BrokenBarrierException ok) {} |
600 |
> |
catch (BrokenBarrierException ok) {} |
601 |
|
catch (Throwable thrown) { |
602 |
|
unexpectedException(); |
603 |
|
}}}); |