6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
12 |
– |
import java.util.concurrent.locks.*; |
13 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.BrokenBarrierException; |
12 |
+ |
import java.util.concurrent.CountDownLatch; |
13 |
+ |
import java.util.concurrent.CyclicBarrier; |
14 |
+ |
import java.util.concurrent.TimeoutException; |
15 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
+ |
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
import junit.framework.TestSuite; |
19 |
+ |
|
20 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
main(suite(), args); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
|
return new TestSuite(CyclicBarrierTest.class); |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
+ |
* Spin-waits till the number of waiters == numberOfWaiters. |
35 |
+ |
*/ |
36 |
+ |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
37 |
+ |
long startTime = System.nanoTime(); |
38 |
+ |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
39 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
40 |
+ |
fail("timed out"); |
41 |
+ |
Thread.yield(); |
42 |
+ |
} |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
/** |
46 |
|
* Creating with negative parties throws IAE |
47 |
|
*/ |
48 |
|
public void testConstructor1() { |
94 |
|
b.await(); |
95 |
|
b.await(); |
96 |
|
assertEquals(0, b.getNumberWaiting()); |
97 |
< |
assertEquals(countAction, 2); |
97 |
> |
assertEquals(2, countAction); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
102 |
|
*/ |
103 |
|
public void testTwoParties() throws Exception { |
104 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
105 |
< |
Thread t = new Thread(new CheckedRunnable() { |
105 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
106 |
|
public void realRun() throws Exception { |
107 |
|
b.await(); |
108 |
|
b.await(); |
110 |
|
b.await(); |
111 |
|
}}); |
112 |
|
|
97 |
– |
t.start(); |
113 |
|
b.await(); |
114 |
|
b.await(); |
115 |
|
b.await(); |
116 |
|
b.await(); |
117 |
< |
t.join(); |
117 |
> |
awaitTermination(t); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
|
* An interruption in one party causes others waiting in await to |
122 |
|
* throw BrokenBarrierException |
123 |
|
*/ |
124 |
< |
public void testAwait1_Interrupted_BrokenBarrier() throws Exception { |
124 |
> |
public void testAwait1_Interrupted_BrokenBarrier() { |
125 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
126 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
127 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
128 |
|
public void realRun() throws Exception { |
129 |
+ |
pleaseInterrupt.countDown(); |
130 |
|
c.await(); |
131 |
|
}}; |
132 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
133 |
|
public void realRun() throws Exception { |
134 |
+ |
pleaseInterrupt.countDown(); |
135 |
|
c.await(); |
136 |
|
}}; |
137 |
|
|
138 |
|
t1.start(); |
139 |
|
t2.start(); |
140 |
< |
delay(SHORT_DELAY_MS); |
140 |
> |
await(pleaseInterrupt); |
141 |
|
t1.interrupt(); |
142 |
< |
t1.join(); |
143 |
< |
t2.join(); |
142 |
> |
awaitTermination(t1); |
143 |
> |
awaitTermination(t2); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
149 |
|
*/ |
150 |
|
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
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(LONG_DELAY_MS, MILLISECONDS); |
157 |
|
}}; |
158 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
159 |
|
public void realRun() throws Exception { |
160 |
+ |
pleaseInterrupt.countDown(); |
161 |
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
162 |
|
}}; |
163 |
|
|
164 |
|
t1.start(); |
165 |
|
t2.start(); |
166 |
< |
delay(SHORT_DELAY_MS); |
166 |
> |
await(pleaseInterrupt); |
167 |
|
t1.interrupt(); |
168 |
< |
t1.join(); |
169 |
< |
t2.join(); |
168 |
> |
awaitTermination(t1); |
169 |
> |
awaitTermination(t2); |
170 |
|
} |
171 |
|
|
172 |
|
/** |
174 |
|
*/ |
175 |
|
public void testAwait3_TimeoutException() throws InterruptedException { |
176 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
177 |
< |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
177 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
178 |
|
public void realRun() throws Exception { |
179 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
180 |
< |
}}; |
179 |
> |
long startTime = System.nanoTime(); |
180 |
> |
try { |
181 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
182 |
> |
shouldThrow(); |
183 |
> |
} catch (TimeoutException success) {} |
184 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
185 |
> |
}}); |
186 |
|
|
187 |
< |
t.start(); |
162 |
< |
t.join(); |
187 |
> |
awaitTermination(t); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
193 |
|
*/ |
194 |
|
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
195 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
196 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
196 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
197 |
|
public void realRun() throws Exception { |
198 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
199 |
< |
}}; |
200 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
198 |
> |
try { |
199 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
200 |
> |
shouldThrow(); |
201 |
> |
} catch (BrokenBarrierException success) {} |
202 |
> |
}}); |
203 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
204 |
|
public void realRun() throws Exception { |
205 |
< |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
206 |
< |
}}; |
205 |
> |
awaitNumberWaiting(c, 1); |
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 |
< |
t1.start(); |
215 |
< |
t2.start(); |
182 |
< |
t1.join(); |
183 |
< |
t2.join(); |
214 |
> |
awaitTermination(t1); |
215 |
> |
awaitTermination(t2); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
221 |
|
*/ |
222 |
|
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
223 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
224 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
224 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
225 |
|
public void realRun() throws Exception { |
226 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
227 |
< |
}}; |
228 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
226 |
> |
try { |
227 |
> |
c.await(); |
228 |
> |
shouldThrow(); |
229 |
> |
} catch (BrokenBarrierException success) {} |
230 |
> |
}}); |
231 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
232 |
|
public void realRun() throws Exception { |
233 |
< |
c.await(); |
234 |
< |
}}; |
233 |
> |
awaitNumberWaiting(c, 1); |
234 |
> |
long startTime = System.nanoTime(); |
235 |
> |
try { |
236 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
237 |
> |
shouldThrow(); |
238 |
> |
} catch (TimeoutException success) {} |
239 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
240 |
> |
}}); |
241 |
|
|
242 |
< |
t1.start(); |
243 |
< |
t2.start(); |
203 |
< |
t1.join(); |
204 |
< |
t2.join(); |
242 |
> |
awaitTermination(t1); |
243 |
> |
awaitTermination(t2); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
249 |
|
*/ |
250 |
|
public void testReset_BrokenBarrier() throws InterruptedException { |
251 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
252 |
+ |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
253 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
254 |
|
public void realRun() throws Exception { |
255 |
+ |
pleaseReset.countDown(); |
256 |
|
c.await(); |
257 |
|
}}; |
258 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
259 |
|
public void realRun() throws Exception { |
260 |
+ |
pleaseReset.countDown(); |
261 |
|
c.await(); |
262 |
|
}}; |
263 |
|
|
264 |
|
t1.start(); |
265 |
|
t2.start(); |
266 |
< |
delay(SHORT_DELAY_MS); |
266 |
> |
await(pleaseReset); |
267 |
> |
|
268 |
> |
awaitNumberWaiting(c, 2); |
269 |
|
c.reset(); |
270 |
< |
t1.join(); |
271 |
< |
t2.join(); |
270 |
> |
awaitTermination(t1); |
271 |
> |
awaitTermination(t2); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
277 |
|
*/ |
278 |
|
public void testReset_NoBrokenBarrier() throws Exception { |
279 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
280 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
280 |
> |
c.reset(); |
281 |
> |
|
282 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
283 |
|
public void realRun() throws Exception { |
284 |
|
c.await(); |
285 |
|
}}); |
286 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
286 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
287 |
|
public void realRun() throws Exception { |
288 |
|
c.await(); |
289 |
|
}}); |
290 |
|
|
245 |
– |
c.reset(); |
246 |
– |
t1.start(); |
247 |
– |
t2.start(); |
291 |
|
c.await(); |
292 |
< |
t1.join(); |
293 |
< |
t2.join(); |
292 |
> |
awaitTermination(t1); |
293 |
> |
awaitTermination(t2); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
299 |
|
public void testReset_Leakage() throws InterruptedException { |
300 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
301 |
|
final AtomicBoolean done = new AtomicBoolean(); |
302 |
< |
Thread t = new Thread(new CheckedRunnable() { |
302 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
303 |
|
public void realRun() { |
304 |
|
while (!done.get()) { |
305 |
|
try { |
313 |
|
catch (InterruptedException ok) {} |
314 |
|
}}}); |
315 |
|
|
273 |
– |
t.start(); |
316 |
|
for (int i = 0; i < 4; i++) { |
317 |
|
delay(timeoutMillis()); |
318 |
|
t.interrupt(); |
326 |
|
* Reset of a non-broken barrier does not break barrier |
327 |
|
*/ |
328 |
|
public void testResetWithoutBreakage() throws Exception { |
287 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
329 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
330 |
|
for (int i = 0; i < 3; i++) { |
331 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
331 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
332 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
333 |
|
public void realRun() throws Exception { |
334 |
|
start.await(); |
335 |
|
barrier.await(); |
336 |
|
}}); |
337 |
|
|
338 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
338 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
339 |
|
public void realRun() throws Exception { |
340 |
|
start.await(); |
341 |
|
barrier.await(); |
342 |
|
}}); |
343 |
|
|
302 |
– |
t1.start(); |
303 |
– |
t2.start(); |
344 |
|
start.await(); |
345 |
|
barrier.await(); |
346 |
< |
t1.join(); |
347 |
< |
t2.join(); |
346 |
> |
awaitTermination(t1); |
347 |
> |
awaitTermination(t2); |
348 |
|
assertFalse(barrier.isBroken()); |
349 |
|
assertEquals(0, barrier.getNumberWaiting()); |
350 |
|
if (i == 1) barrier.reset(); |
357 |
|
* Reset of a barrier after interruption reinitializes it. |
358 |
|
*/ |
359 |
|
public void testResetAfterInterrupt() throws Exception { |
320 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
360 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
361 |
|
for (int i = 0; i < 2; i++) { |
362 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
363 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
364 |
|
public void realRun() throws Exception { |
365 |
|
start.await(); |
376 |
|
t2.start(); |
377 |
|
start.await(); |
378 |
|
t1.interrupt(); |
379 |
< |
t1.join(); |
380 |
< |
t2.join(); |
379 |
> |
awaitTermination(t1); |
380 |
> |
awaitTermination(t2); |
381 |
|
assertTrue(barrier.isBroken()); |
382 |
|
assertEquals(0, barrier.getNumberWaiting()); |
383 |
|
barrier.reset(); |
390 |
|
* Reset of a barrier after timeout reinitializes it. |
391 |
|
*/ |
392 |
|
public void testResetAfterTimeout() throws Exception { |
353 |
– |
final CyclicBarrier start = new CyclicBarrier(2); |
393 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
394 |
|
for (int i = 0; i < 2; i++) { |
395 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
396 |
< |
public void realRun() throws Exception { |
358 |
< |
start.await(); |
359 |
< |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
360 |
< |
}}; |
361 |
< |
|
362 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
395 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
396 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
397 |
|
public void realRun() throws Exception { |
398 |
< |
start.await(); |
399 |
< |
barrier.await(); |
400 |
< |
}}; |
398 |
> |
try { |
399 |
> |
barrier.await(); |
400 |
> |
shouldThrow(); |
401 |
> |
} catch (BrokenBarrierException success) {} |
402 |
> |
}}); |
403 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
404 |
> |
public void realRun() throws Exception { |
405 |
> |
awaitNumberWaiting(barrier, 1); |
406 |
> |
long startTime = System.nanoTime(); |
407 |
> |
try { |
408 |
> |
barrier.await(timeoutMillis(), MILLISECONDS); |
409 |
> |
shouldThrow(); |
410 |
> |
} catch (TimeoutException success) {} |
411 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
412 |
> |
}}); |
413 |
|
|
414 |
< |
t1.start(); |
415 |
< |
t2.start(); |
416 |
< |
t1.join(); |
371 |
< |
t2.join(); |
414 |
> |
awaitTermination(t1); |
415 |
> |
awaitTermination(t2); |
416 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
417 |
|
assertTrue(barrier.isBroken()); |
418 |
|
assertEquals(0, barrier.getNumberWaiting()); |
419 |
|
barrier.reset(); |
426 |
|
* Reset of a barrier after a failed command reinitializes it. |
427 |
|
*/ |
428 |
|
public void testResetAfterCommandException() throws Exception { |
384 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
429 |
|
final CyclicBarrier barrier = |
430 |
|
new CyclicBarrier(3, new Runnable() { |
431 |
|
public void run() { |
432 |
|
throw new NullPointerException(); }}); |
433 |
|
for (int i = 0; i < 2; i++) { |
434 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
435 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
436 |
|
public void realRun() throws Exception { |
437 |
|
start.await(); |
447 |
|
t1.start(); |
448 |
|
t2.start(); |
449 |
|
start.await(); |
450 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
450 |
> |
awaitNumberWaiting(barrier, 2); |
451 |
|
try { |
452 |
|
barrier.await(); |
453 |
|
shouldThrow(); |
454 |
|
} catch (NullPointerException success) {} |
455 |
< |
t1.join(); |
456 |
< |
t2.join(); |
455 |
> |
awaitTermination(t1); |
456 |
> |
awaitTermination(t2); |
457 |
|
assertTrue(barrier.isBroken()); |
458 |
|
assertEquals(0, barrier.getNumberWaiting()); |
459 |
|
barrier.reset(); |