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 |
|
|
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(); |
162 |
< |
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(); |
182 |
< |
t1.join(); |
183 |
< |
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(); |
203 |
< |
t1.join(); |
204 |
< |
t2.join(); |
228 |
> |
awaitTermination(t1); |
229 |
> |
awaitTermination(t2); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
234 |
|
* BrokenBarrierException |
235 |
|
*/ |
236 |
|
public void testReset_BrokenBarrier() throws InterruptedException { |
237 |
+ |
Thread.interrupted(); // ensure current thread not interrupted |
238 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
239 |
+ |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
240 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
241 |
|
public void realRun() throws Exception { |
242 |
+ |
pleaseReset.countDown(); |
243 |
|
c.await(); |
244 |
|
}}; |
245 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
246 |
|
public void realRun() throws Exception { |
247 |
+ |
pleaseReset.countDown(); |
248 |
|
c.await(); |
249 |
|
}}; |
250 |
|
|
251 |
|
t1.start(); |
252 |
|
t2.start(); |
253 |
< |
delay(SHORT_DELAY_MS); |
253 |
> |
await(pleaseReset); |
254 |
|
c.reset(); |
255 |
< |
t1.join(); |
256 |
< |
t2.join(); |
255 |
> |
awaitTermination(t1); |
256 |
> |
awaitTermination(t2); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
262 |
|
*/ |
263 |
|
public void testReset_NoBrokenBarrier() throws Exception { |
264 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
265 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
265 |
> |
c.reset(); |
266 |
> |
|
267 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
268 |
|
public void realRun() throws Exception { |
269 |
|
c.await(); |
270 |
|
}}); |
271 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
271 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
272 |
|
public void realRun() throws Exception { |
273 |
|
c.await(); |
274 |
|
}}); |
275 |
|
|
245 |
– |
c.reset(); |
246 |
– |
t1.start(); |
247 |
– |
t2.start(); |
276 |
|
c.await(); |
277 |
< |
t1.join(); |
278 |
< |
t2.join(); |
277 |
> |
awaitTermination(t1); |
278 |
> |
awaitTermination(t2); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
284 |
|
public void testReset_Leakage() throws InterruptedException { |
285 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
286 |
|
final AtomicBoolean done = new AtomicBoolean(); |
287 |
< |
Thread t = new Thread(new CheckedRunnable() { |
287 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
288 |
|
public void realRun() { |
289 |
|
while (!done.get()) { |
290 |
|
try { |
298 |
|
catch (InterruptedException ok) {} |
299 |
|
}}}); |
300 |
|
|
273 |
– |
t.start(); |
301 |
|
for (int i = 0; i < 4; i++) { |
302 |
|
delay(timeoutMillis()); |
303 |
|
t.interrupt(); |
311 |
|
* Reset of a non-broken barrier does not break barrier |
312 |
|
*/ |
313 |
|
public void testResetWithoutBreakage() throws Exception { |
287 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
314 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
315 |
|
for (int i = 0; i < 3; i++) { |
316 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
316 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
317 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
318 |
|
public void realRun() throws Exception { |
319 |
|
start.await(); |
320 |
|
barrier.await(); |
321 |
|
}}); |
322 |
|
|
323 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
323 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
324 |
|
public void realRun() throws Exception { |
325 |
|
start.await(); |
326 |
|
barrier.await(); |
327 |
|
}}); |
328 |
|
|
302 |
– |
t1.start(); |
303 |
– |
t2.start(); |
329 |
|
start.await(); |
330 |
|
barrier.await(); |
331 |
< |
t1.join(); |
332 |
< |
t2.join(); |
331 |
> |
awaitTermination(t1); |
332 |
> |
awaitTermination(t2); |
333 |
|
assertFalse(barrier.isBroken()); |
334 |
|
assertEquals(0, barrier.getNumberWaiting()); |
335 |
|
if (i == 1) barrier.reset(); |
342 |
|
* Reset of a barrier after interruption reinitializes it. |
343 |
|
*/ |
344 |
|
public void testResetAfterInterrupt() throws Exception { |
320 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
345 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
346 |
|
for (int i = 0; i < 2; i++) { |
347 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
348 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
349 |
|
public void realRun() throws Exception { |
350 |
|
start.await(); |
361 |
|
t2.start(); |
362 |
|
start.await(); |
363 |
|
t1.interrupt(); |
364 |
< |
t1.join(); |
365 |
< |
t2.join(); |
364 |
> |
awaitTermination(t1); |
365 |
> |
awaitTermination(t2); |
366 |
|
assertTrue(barrier.isBroken()); |
367 |
|
assertEquals(0, barrier.getNumberWaiting()); |
368 |
|
barrier.reset(); |
375 |
|
* Reset of a barrier after timeout reinitializes it. |
376 |
|
*/ |
377 |
|
public void testResetAfterTimeout() throws Exception { |
353 |
– |
final CyclicBarrier start = new CyclicBarrier(2); |
378 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
379 |
|
for (int i = 0; i < 2; i++) { |
380 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
381 |
< |
public void realRun() throws Exception { |
358 |
< |
start.await(); |
359 |
< |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
360 |
< |
}}; |
361 |
< |
|
362 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
380 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
381 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
382 |
|
public void realRun() throws Exception { |
383 |
< |
start.await(); |
384 |
< |
barrier.await(); |
385 |
< |
}}; |
383 |
> |
try { |
384 |
> |
barrier.await(); |
385 |
> |
shouldThrow(); |
386 |
> |
} catch (BrokenBarrierException success) {} |
387 |
> |
}}); |
388 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws Exception { |
390 |
> |
while (barrier.getNumberWaiting() == 0) |
391 |
> |
Thread.yield(); |
392 |
> |
long startTime = System.nanoTime(); |
393 |
> |
try { |
394 |
> |
barrier.await(timeoutMillis(), MILLISECONDS); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (TimeoutException success) {} |
397 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
398 |
> |
}}); |
399 |
|
|
400 |
< |
t1.start(); |
401 |
< |
t2.start(); |
402 |
< |
t1.join(); |
371 |
< |
t2.join(); |
400 |
> |
awaitTermination(t1); |
401 |
> |
awaitTermination(t2); |
402 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
403 |
|
assertTrue(barrier.isBroken()); |
404 |
|
assertEquals(0, barrier.getNumberWaiting()); |
405 |
|
barrier.reset(); |
412 |
|
* Reset of a barrier after a failed command reinitializes it. |
413 |
|
*/ |
414 |
|
public void testResetAfterCommandException() throws Exception { |
384 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
415 |
|
final CyclicBarrier barrier = |
416 |
|
new CyclicBarrier(3, new Runnable() { |
417 |
|
public void run() { |
418 |
|
throw new NullPointerException(); }}); |
419 |
|
for (int i = 0; i < 2; i++) { |
420 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
421 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
422 |
|
public void realRun() throws Exception { |
423 |
|
start.await(); |
438 |
|
barrier.await(); |
439 |
|
shouldThrow(); |
440 |
|
} catch (NullPointerException success) {} |
441 |
< |
t1.join(); |
442 |
< |
t2.join(); |
441 |
> |
awaitTermination(t1); |
442 |
> |
awaitTermination(t2); |
443 |
|
assertTrue(barrier.isBroken()); |
444 |
|
assertEquals(0, barrier.getNumberWaiting()); |
445 |
|
barrier.reset(); |