86 |
|
*/ |
87 |
|
public void testTwoParties() throws Exception { |
88 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
89 |
< |
Thread t = new Thread(new CheckedRunnable() { |
89 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
|
public void realRun() throws Exception { |
91 |
|
b.await(); |
92 |
|
b.await(); |
94 |
|
b.await(); |
95 |
|
}}); |
96 |
|
|
97 |
– |
t.start(); |
97 |
|
b.await(); |
98 |
|
b.await(); |
99 |
|
b.await(); |
100 |
|
b.await(); |
101 |
< |
t.join(); |
101 |
> |
awaitTermination(t); |
102 |
|
} |
103 |
|
|
105 |
– |
|
104 |
|
/** |
105 |
|
* An interruption in one party causes others waiting in await to |
106 |
|
* throw BrokenBarrierException |
107 |
|
*/ |
108 |
< |
public void testAwait1_Interrupted_BrokenBarrier() throws Exception { |
108 |
> |
public void testAwait1_Interrupted_BrokenBarrier() { |
109 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
110 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
111 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
112 |
|
public void realRun() throws Exception { |
113 |
+ |
pleaseInterrupt.countDown(); |
114 |
|
c.await(); |
115 |
|
}}; |
116 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
117 |
|
public void realRun() throws Exception { |
118 |
+ |
pleaseInterrupt.countDown(); |
119 |
|
c.await(); |
120 |
|
}}; |
121 |
|
|
122 |
|
t1.start(); |
123 |
|
t2.start(); |
124 |
< |
delay(SHORT_DELAY_MS); |
124 |
> |
await(pleaseInterrupt); |
125 |
|
t1.interrupt(); |
126 |
< |
t1.join(); |
127 |
< |
t2.join(); |
126 |
> |
awaitTermination(t1); |
127 |
> |
awaitTermination(t2); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
133 |
|
*/ |
134 |
|
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
135 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
136 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
137 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
138 |
|
public void realRun() throws Exception { |
139 |
+ |
pleaseInterrupt.countDown(); |
140 |
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
141 |
|
}}; |
142 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
143 |
|
public void realRun() throws Exception { |
144 |
+ |
pleaseInterrupt.countDown(); |
145 |
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
146 |
|
}}; |
147 |
|
|
148 |
|
t1.start(); |
149 |
|
t2.start(); |
150 |
< |
delay(SHORT_DELAY_MS); |
150 |
> |
await(pleaseInterrupt); |
151 |
|
t1.interrupt(); |
152 |
< |
t1.join(); |
153 |
< |
t2.join(); |
152 |
> |
awaitTermination(t1); |
153 |
> |
awaitTermination(t2); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
158 |
|
*/ |
159 |
|
public void testAwait3_TimeoutException() throws InterruptedException { |
160 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
161 |
< |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
161 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
162 |
|
public void realRun() throws Exception { |
163 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
164 |
< |
}}; |
163 |
> |
long startTime = System.nanoTime(); |
164 |
> |
try { |
165 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
166 |
> |
shouldThrow(); |
167 |
> |
} catch (TimeoutException success) {} |
168 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
169 |
> |
}}); |
170 |
|
|
171 |
< |
t.start(); |
163 |
< |
t.join(); |
171 |
> |
awaitTermination(t); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
177 |
|
*/ |
178 |
|
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
179 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
180 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
180 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
181 |
|
public void realRun() throws Exception { |
182 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
183 |
< |
}}; |
184 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
182 |
> |
try { |
183 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
184 |
> |
shouldThrow(); |
185 |
> |
} catch (BrokenBarrierException success) {} |
186 |
> |
}}); |
187 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
188 |
|
public void realRun() throws Exception { |
189 |
< |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
190 |
< |
}}; |
189 |
> |
while (c.getNumberWaiting() == 0) |
190 |
> |
Thread.yield(); |
191 |
> |
long startTime = System.nanoTime(); |
192 |
> |
try { |
193 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
194 |
> |
shouldThrow(); |
195 |
> |
} catch (TimeoutException success) {} |
196 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
197 |
> |
}}); |
198 |
|
|
199 |
< |
t1.start(); |
200 |
< |
t2.start(); |
183 |
< |
t1.join(); |
184 |
< |
t2.join(); |
199 |
> |
awaitTermination(t1); |
200 |
> |
awaitTermination(t2); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
206 |
|
*/ |
207 |
|
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
208 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
209 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
209 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
210 |
|
public void realRun() throws Exception { |
211 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
212 |
< |
}}; |
213 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
211 |
> |
try { |
212 |
> |
c.await(); |
213 |
> |
shouldThrow(); |
214 |
> |
} catch (BrokenBarrierException success) {} |
215 |
> |
}}); |
216 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
217 |
|
public void realRun() throws Exception { |
218 |
< |
c.await(); |
219 |
< |
}}; |
218 |
> |
while (c.getNumberWaiting() == 0) |
219 |
> |
Thread.yield(); |
220 |
> |
long startTime = System.nanoTime(); |
221 |
> |
try { |
222 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
223 |
> |
shouldThrow(); |
224 |
> |
} catch (TimeoutException success) {} |
225 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
226 |
> |
}}); |
227 |
|
|
228 |
< |
t1.start(); |
229 |
< |
t2.start(); |
204 |
< |
t1.join(); |
205 |
< |
t2.join(); |
228 |
> |
awaitTermination(t1); |
229 |
> |
awaitTermination(t2); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
235 |
|
*/ |
236 |
|
public void testReset_BrokenBarrier() throws InterruptedException { |
237 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
238 |
+ |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
239 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
240 |
|
public void realRun() throws Exception { |
241 |
+ |
pleaseReset.countDown(); |
242 |
|
c.await(); |
243 |
|
}}; |
244 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
245 |
|
public void realRun() throws Exception { |
246 |
+ |
pleaseReset.countDown(); |
247 |
|
c.await(); |
248 |
|
}}; |
249 |
|
|
250 |
|
t1.start(); |
251 |
|
t2.start(); |
252 |
< |
delay(SHORT_DELAY_MS); |
252 |
> |
await(pleaseReset); |
253 |
|
c.reset(); |
254 |
< |
t1.join(); |
255 |
< |
t2.join(); |
254 |
> |
awaitTermination(t1); |
255 |
> |
awaitTermination(t2); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
261 |
|
*/ |
262 |
|
public void testReset_NoBrokenBarrier() throws Exception { |
263 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
264 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
264 |
> |
c.reset(); |
265 |
> |
|
266 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
267 |
|
public void realRun() throws Exception { |
268 |
|
c.await(); |
269 |
|
}}); |
270 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
270 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
271 |
|
public void realRun() throws Exception { |
272 |
|
c.await(); |
273 |
|
}}); |
274 |
|
|
246 |
– |
c.reset(); |
247 |
– |
t1.start(); |
248 |
– |
t2.start(); |
275 |
|
c.await(); |
276 |
< |
t1.join(); |
277 |
< |
t2.join(); |
276 |
> |
awaitTermination(t1); |
277 |
> |
awaitTermination(t2); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
283 |
|
public void testReset_Leakage() throws InterruptedException { |
284 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
285 |
|
final AtomicBoolean done = new AtomicBoolean(); |
286 |
< |
Thread t = new Thread() { |
287 |
< |
public void run() { |
288 |
< |
while (!done.get()) { |
289 |
< |
try { |
290 |
< |
while (c.isBroken()) |
291 |
< |
c.reset(); |
292 |
< |
|
293 |
< |
c.await(); |
294 |
< |
threadFail("await should not return"); |
269 |
< |
} |
270 |
< |
catch (BrokenBarrierException e) { |
271 |
< |
} |
272 |
< |
catch (InterruptedException ie) { |
273 |
< |
} |
286 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
287 |
> |
public void realRun() { |
288 |
> |
while (!done.get()) { |
289 |
> |
try { |
290 |
> |
while (c.isBroken()) |
291 |
> |
c.reset(); |
292 |
> |
|
293 |
> |
c.await(); |
294 |
> |
shouldThrow(); |
295 |
|
} |
296 |
< |
} |
297 |
< |
}; |
296 |
> |
catch (BrokenBarrierException ok) {} |
297 |
> |
catch (InterruptedException ok) {} |
298 |
> |
}}}); |
299 |
|
|
278 |
– |
t.start(); |
300 |
|
for (int i = 0; i < 4; i++) { |
301 |
< |
delay(SHORT_DELAY_MS); |
301 |
> |
delay(timeoutMillis()); |
302 |
|
t.interrupt(); |
303 |
|
} |
304 |
|
done.set(true); |
305 |
|
t.interrupt(); |
306 |
< |
t.join(); |
306 |
> |
awaitTermination(t); |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
|
* Reset of a non-broken barrier does not break barrier |
311 |
|
*/ |
312 |
|
public void testResetWithoutBreakage() throws Exception { |
292 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
313 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
314 |
|
for (int i = 0; i < 3; i++) { |
315 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
315 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
316 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
317 |
|
public void realRun() throws Exception { |
318 |
|
start.await(); |
319 |
|
barrier.await(); |
320 |
|
}}); |
321 |
|
|
322 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
322 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
323 |
|
public void realRun() throws Exception { |
324 |
|
start.await(); |
325 |
|
barrier.await(); |
326 |
|
}}); |
327 |
|
|
307 |
– |
t1.start(); |
308 |
– |
t2.start(); |
328 |
|
start.await(); |
329 |
|
barrier.await(); |
330 |
< |
t1.join(); |
331 |
< |
t2.join(); |
330 |
> |
awaitTermination(t1); |
331 |
> |
awaitTermination(t2); |
332 |
|
assertFalse(barrier.isBroken()); |
333 |
|
assertEquals(0, barrier.getNumberWaiting()); |
334 |
|
if (i == 1) barrier.reset(); |
341 |
|
* Reset of a barrier after interruption reinitializes it. |
342 |
|
*/ |
343 |
|
public void testResetAfterInterrupt() throws Exception { |
325 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
344 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
345 |
|
for (int i = 0; i < 2; i++) { |
346 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
347 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
348 |
|
public void realRun() throws Exception { |
349 |
|
start.await(); |
360 |
|
t2.start(); |
361 |
|
start.await(); |
362 |
|
t1.interrupt(); |
363 |
< |
t1.join(); |
364 |
< |
t2.join(); |
363 |
> |
awaitTermination(t1); |
364 |
> |
awaitTermination(t2); |
365 |
|
assertTrue(barrier.isBroken()); |
366 |
|
assertEquals(0, barrier.getNumberWaiting()); |
367 |
|
barrier.reset(); |
374 |
|
* Reset of a barrier after timeout reinitializes it. |
375 |
|
*/ |
376 |
|
public void testResetAfterTimeout() throws Exception { |
358 |
– |
final CyclicBarrier start = new CyclicBarrier(2); |
377 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
378 |
|
for (int i = 0; i < 2; i++) { |
379 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
380 |
< |
public void realRun() throws Exception { |
363 |
< |
start.await(); |
364 |
< |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
365 |
< |
}}; |
366 |
< |
|
367 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
379 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
380 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
381 |
|
public void realRun() throws Exception { |
382 |
< |
start.await(); |
383 |
< |
barrier.await(); |
384 |
< |
}}; |
382 |
> |
try { |
383 |
> |
barrier.await(); |
384 |
> |
shouldThrow(); |
385 |
> |
} catch (BrokenBarrierException success) {} |
386 |
> |
}}); |
387 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
388 |
> |
public void realRun() throws Exception { |
389 |
> |
while (barrier.getNumberWaiting() == 0) |
390 |
> |
Thread.yield(); |
391 |
> |
long startTime = System.nanoTime(); |
392 |
> |
try { |
393 |
> |
barrier.await(timeoutMillis(), MILLISECONDS); |
394 |
> |
shouldThrow(); |
395 |
> |
} catch (TimeoutException success) {} |
396 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
397 |
> |
}}); |
398 |
|
|
399 |
< |
t1.start(); |
400 |
< |
t2.start(); |
401 |
< |
t1.join(); |
376 |
< |
t2.join(); |
399 |
> |
awaitTermination(t1); |
400 |
> |
awaitTermination(t2); |
401 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
402 |
|
assertTrue(barrier.isBroken()); |
403 |
|
assertEquals(0, barrier.getNumberWaiting()); |
404 |
|
barrier.reset(); |
407 |
|
} |
408 |
|
} |
409 |
|
|
385 |
– |
|
410 |
|
/** |
411 |
|
* Reset of a barrier after a failed command reinitializes it. |
412 |
|
*/ |
413 |
|
public void testResetAfterCommandException() throws Exception { |
390 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
414 |
|
final CyclicBarrier barrier = |
415 |
|
new CyclicBarrier(3, new Runnable() { |
416 |
|
public void run() { |
417 |
|
throw new NullPointerException(); }}); |
418 |
|
for (int i = 0; i < 2; i++) { |
419 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
420 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
421 |
|
public void realRun() throws Exception { |
422 |
|
start.await(); |
437 |
|
barrier.await(); |
438 |
|
shouldThrow(); |
439 |
|
} catch (NullPointerException success) {} |
440 |
< |
t1.join(); |
441 |
< |
t2.join(); |
440 |
> |
awaitTermination(t1); |
441 |
> |
awaitTermination(t2); |
442 |
|
assertTrue(barrier.isBroken()); |
443 |
|
assertEquals(0, barrier.getNumberWaiting()); |
444 |
|
barrier.reset(); |