27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
+ |
* Spin-waits till the number of waiters == numberOfWaiters. |
31 |
+ |
*/ |
32 |
+ |
void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) { |
33 |
+ |
long startTime = System.nanoTime(); |
34 |
+ |
while (barrier.getNumberWaiting() != numberOfWaiters) { |
35 |
+ |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
36 |
+ |
fail("timed out"); |
37 |
+ |
Thread.yield(); |
38 |
+ |
} |
39 |
+ |
} |
40 |
+ |
|
41 |
+ |
/** |
42 |
|
* Creating with negative parties throws IAE |
43 |
|
*/ |
44 |
|
public void testConstructor1() { |
98 |
|
*/ |
99 |
|
public void testTwoParties() throws Exception { |
100 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
101 |
< |
Thread t = new Thread(new CheckedRunnable() { |
101 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
102 |
|
public void realRun() throws Exception { |
103 |
|
b.await(); |
104 |
|
b.await(); |
106 |
|
b.await(); |
107 |
|
}}); |
108 |
|
|
97 |
– |
t.start(); |
109 |
|
b.await(); |
110 |
|
b.await(); |
111 |
|
b.await(); |
112 |
|
b.await(); |
113 |
< |
t.join(); |
113 |
> |
awaitTermination(t); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
|
* An interruption in one party causes others waiting in await to |
118 |
|
* throw BrokenBarrierException |
119 |
|
*/ |
120 |
< |
public void testAwait1_Interrupted_BrokenBarrier() throws Exception { |
120 |
> |
public void testAwait1_Interrupted_BrokenBarrier() { |
121 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
122 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
123 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
124 |
|
public void realRun() throws Exception { |
125 |
+ |
pleaseInterrupt.countDown(); |
126 |
|
c.await(); |
127 |
|
}}; |
128 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
129 |
|
public void realRun() throws Exception { |
130 |
+ |
pleaseInterrupt.countDown(); |
131 |
|
c.await(); |
132 |
|
}}; |
133 |
|
|
134 |
|
t1.start(); |
135 |
|
t2.start(); |
136 |
< |
delay(SHORT_DELAY_MS); |
136 |
> |
await(pleaseInterrupt); |
137 |
|
t1.interrupt(); |
138 |
< |
t1.join(); |
139 |
< |
t2.join(); |
138 |
> |
awaitTermination(t1); |
139 |
> |
awaitTermination(t2); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
145 |
|
*/ |
146 |
|
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
147 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
148 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
149 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
150 |
|
public void realRun() throws Exception { |
151 |
+ |
pleaseInterrupt.countDown(); |
152 |
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
153 |
|
}}; |
154 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
155 |
|
public void realRun() throws Exception { |
156 |
+ |
pleaseInterrupt.countDown(); |
157 |
|
c.await(LONG_DELAY_MS, MILLISECONDS); |
158 |
|
}}; |
159 |
|
|
160 |
|
t1.start(); |
161 |
|
t2.start(); |
162 |
< |
delay(SHORT_DELAY_MS); |
162 |
> |
await(pleaseInterrupt); |
163 |
|
t1.interrupt(); |
164 |
< |
t1.join(); |
165 |
< |
t2.join(); |
164 |
> |
awaitTermination(t1); |
165 |
> |
awaitTermination(t2); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
170 |
|
*/ |
171 |
|
public void testAwait3_TimeoutException() throws InterruptedException { |
172 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
173 |
< |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
173 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
174 |
|
public void realRun() throws Exception { |
175 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
176 |
< |
}}; |
175 |
> |
long startTime = System.nanoTime(); |
176 |
> |
try { |
177 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
178 |
> |
shouldThrow(); |
179 |
> |
} catch (TimeoutException success) {} |
180 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
181 |
> |
}}); |
182 |
|
|
183 |
< |
t.start(); |
162 |
< |
t.join(); |
183 |
> |
awaitTermination(t); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
189 |
|
*/ |
190 |
|
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
191 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
192 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
192 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
193 |
|
public void realRun() throws Exception { |
194 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
195 |
< |
}}; |
196 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
194 |
> |
try { |
195 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
196 |
> |
shouldThrow(); |
197 |
> |
} catch (BrokenBarrierException success) {} |
198 |
> |
}}); |
199 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
200 |
|
public void realRun() throws Exception { |
201 |
< |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
202 |
< |
}}; |
201 |
> |
awaitNumberWaiting(c, 1); |
202 |
> |
long startTime = System.nanoTime(); |
203 |
> |
try { |
204 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (TimeoutException success) {} |
207 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
208 |
> |
}}); |
209 |
|
|
210 |
< |
t1.start(); |
211 |
< |
t2.start(); |
182 |
< |
t1.join(); |
183 |
< |
t2.join(); |
210 |
> |
awaitTermination(t1); |
211 |
> |
awaitTermination(t2); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
217 |
|
*/ |
218 |
|
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
219 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
220 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
220 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
221 |
|
public void realRun() throws Exception { |
222 |
< |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
223 |
< |
}}; |
224 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
222 |
> |
try { |
223 |
> |
c.await(); |
224 |
> |
shouldThrow(); |
225 |
> |
} catch (BrokenBarrierException success) {} |
226 |
> |
}}); |
227 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
228 |
|
public void realRun() throws Exception { |
229 |
< |
c.await(); |
230 |
< |
}}; |
229 |
> |
awaitNumberWaiting(c, 1); |
230 |
> |
long startTime = System.nanoTime(); |
231 |
> |
try { |
232 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
233 |
> |
shouldThrow(); |
234 |
> |
} catch (TimeoutException success) {} |
235 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
236 |
> |
}}); |
237 |
|
|
238 |
< |
t1.start(); |
239 |
< |
t2.start(); |
203 |
< |
t1.join(); |
204 |
< |
t2.join(); |
238 |
> |
awaitTermination(t1); |
239 |
> |
awaitTermination(t2); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
245 |
|
*/ |
246 |
|
public void testReset_BrokenBarrier() throws InterruptedException { |
247 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
248 |
+ |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
249 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
250 |
|
public void realRun() throws Exception { |
251 |
+ |
pleaseReset.countDown(); |
252 |
|
c.await(); |
253 |
|
}}; |
254 |
|
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
255 |
|
public void realRun() throws Exception { |
256 |
+ |
pleaseReset.countDown(); |
257 |
|
c.await(); |
258 |
|
}}; |
259 |
|
|
260 |
|
t1.start(); |
261 |
|
t2.start(); |
262 |
< |
delay(SHORT_DELAY_MS); |
262 |
> |
await(pleaseReset); |
263 |
> |
|
264 |
> |
awaitNumberWaiting(c, 2); |
265 |
|
c.reset(); |
266 |
< |
t1.join(); |
267 |
< |
t2.join(); |
266 |
> |
awaitTermination(t1); |
267 |
> |
awaitTermination(t2); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
273 |
|
*/ |
274 |
|
public void testReset_NoBrokenBarrier() throws Exception { |
275 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
276 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
276 |
> |
c.reset(); |
277 |
> |
|
278 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
279 |
|
public void realRun() throws Exception { |
280 |
|
c.await(); |
281 |
|
}}); |
282 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
282 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
283 |
|
public void realRun() throws Exception { |
284 |
|
c.await(); |
285 |
|
}}); |
286 |
|
|
245 |
– |
c.reset(); |
246 |
– |
t1.start(); |
247 |
– |
t2.start(); |
287 |
|
c.await(); |
288 |
< |
t1.join(); |
289 |
< |
t2.join(); |
288 |
> |
awaitTermination(t1); |
289 |
> |
awaitTermination(t2); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
295 |
|
public void testReset_Leakage() throws InterruptedException { |
296 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
297 |
|
final AtomicBoolean done = new AtomicBoolean(); |
298 |
< |
Thread t = new Thread(new CheckedRunnable() { |
298 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
299 |
|
public void realRun() { |
300 |
|
while (!done.get()) { |
301 |
|
try { |
309 |
|
catch (InterruptedException ok) {} |
310 |
|
}}}); |
311 |
|
|
273 |
– |
t.start(); |
312 |
|
for (int i = 0; i < 4; i++) { |
313 |
|
delay(timeoutMillis()); |
314 |
|
t.interrupt(); |
322 |
|
* Reset of a non-broken barrier does not break barrier |
323 |
|
*/ |
324 |
|
public void testResetWithoutBreakage() throws Exception { |
287 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
325 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
326 |
|
for (int i = 0; i < 3; i++) { |
327 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
327 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
328 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
329 |
|
public void realRun() throws Exception { |
330 |
|
start.await(); |
331 |
|
barrier.await(); |
332 |
|
}}); |
333 |
|
|
334 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
334 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
335 |
|
public void realRun() throws Exception { |
336 |
|
start.await(); |
337 |
|
barrier.await(); |
338 |
|
}}); |
339 |
|
|
302 |
– |
t1.start(); |
303 |
– |
t2.start(); |
340 |
|
start.await(); |
341 |
|
barrier.await(); |
342 |
< |
t1.join(); |
343 |
< |
t2.join(); |
342 |
> |
awaitTermination(t1); |
343 |
> |
awaitTermination(t2); |
344 |
|
assertFalse(barrier.isBroken()); |
345 |
|
assertEquals(0, barrier.getNumberWaiting()); |
346 |
|
if (i == 1) barrier.reset(); |
353 |
|
* Reset of a barrier after interruption reinitializes it. |
354 |
|
*/ |
355 |
|
public void testResetAfterInterrupt() throws Exception { |
320 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
356 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
357 |
|
for (int i = 0; i < 2; i++) { |
358 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
359 |
|
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
360 |
|
public void realRun() throws Exception { |
361 |
|
start.await(); |
372 |
|
t2.start(); |
373 |
|
start.await(); |
374 |
|
t1.interrupt(); |
375 |
< |
t1.join(); |
376 |
< |
t2.join(); |
375 |
> |
awaitTermination(t1); |
376 |
> |
awaitTermination(t2); |
377 |
|
assertTrue(barrier.isBroken()); |
378 |
|
assertEquals(0, barrier.getNumberWaiting()); |
379 |
|
barrier.reset(); |
386 |
|
* Reset of a barrier after timeout reinitializes it. |
387 |
|
*/ |
388 |
|
public void testResetAfterTimeout() throws Exception { |
353 |
– |
final CyclicBarrier start = new CyclicBarrier(2); |
389 |
|
final CyclicBarrier barrier = new CyclicBarrier(3); |
390 |
|
for (int i = 0; i < 2; i++) { |
391 |
< |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
392 |
< |
public void realRun() throws Exception { |
358 |
< |
start.await(); |
359 |
< |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
360 |
< |
}}; |
361 |
< |
|
362 |
< |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
391 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
392 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
393 |
|
public void realRun() throws Exception { |
394 |
< |
start.await(); |
395 |
< |
barrier.await(); |
396 |
< |
}}; |
394 |
> |
try { |
395 |
> |
barrier.await(); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (BrokenBarrierException success) {} |
398 |
> |
}}); |
399 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
400 |
> |
public void realRun() throws Exception { |
401 |
> |
awaitNumberWaiting(barrier, 1); |
402 |
> |
long startTime = System.nanoTime(); |
403 |
> |
try { |
404 |
> |
barrier.await(timeoutMillis(), MILLISECONDS); |
405 |
> |
shouldThrow(); |
406 |
> |
} catch (TimeoutException success) {} |
407 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
408 |
> |
}}); |
409 |
|
|
410 |
< |
t1.start(); |
411 |
< |
t2.start(); |
412 |
< |
t1.join(); |
371 |
< |
t2.join(); |
410 |
> |
awaitTermination(t1); |
411 |
> |
awaitTermination(t2); |
412 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
413 |
|
assertTrue(barrier.isBroken()); |
414 |
|
assertEquals(0, barrier.getNumberWaiting()); |
415 |
|
barrier.reset(); |
422 |
|
* Reset of a barrier after a failed command reinitializes it. |
423 |
|
*/ |
424 |
|
public void testResetAfterCommandException() throws Exception { |
384 |
– |
final CyclicBarrier start = new CyclicBarrier(3); |
425 |
|
final CyclicBarrier barrier = |
426 |
|
new CyclicBarrier(3, new Runnable() { |
427 |
|
public void run() { |
428 |
|
throw new NullPointerException(); }}); |
429 |
|
for (int i = 0; i < 2; i++) { |
430 |
+ |
final CyclicBarrier start = new CyclicBarrier(3); |
431 |
|
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
432 |
|
public void realRun() throws Exception { |
433 |
|
start.await(); |
443 |
|
t1.start(); |
444 |
|
t2.start(); |
445 |
|
start.await(); |
446 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
446 |
> |
awaitNumberWaiting(barrier, 2); |
447 |
|
try { |
448 |
|
barrier.await(); |
449 |
|
shouldThrow(); |
450 |
|
} catch (NullPointerException success) {} |
451 |
< |
t1.join(); |
452 |
< |
t2.join(); |
451 |
> |
awaitTermination(t1); |
452 |
> |
awaitTermination(t2); |
453 |
|
assertTrue(barrier.isBroken()); |
454 |
|
assertEquals(0, barrier.getNumberWaiting()); |
455 |
|
barrier.reset(); |