23 |
|
} |
24 |
|
|
25 |
|
/** |
26 |
< |
* |
26 |
> |
* Creating with negative parties throws IAE |
27 |
|
*/ |
28 |
|
public void testConstructor1() { |
29 |
|
try { |
33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
< |
* |
36 |
> |
* Creating with negative parties and no action throws IAE |
37 |
|
*/ |
38 |
|
public void testConstructor2() { |
39 |
|
try { |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* getParties returns the number of parties given in constructor |
47 |
|
*/ |
48 |
< |
public void testConstructor3() { |
48 |
> |
public void testGetParties() { |
49 |
|
CyclicBarrier b = new CyclicBarrier(2); |
50 |
|
assertEquals(2, b.getParties()); |
51 |
|
assertEquals(0, b.getNumberWaiting()); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* |
55 |
> |
* A 1-party barrier triggers after single await |
56 |
|
*/ |
57 |
|
public void testSingleParty() { |
58 |
|
try { |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* |
72 |
> |
* The supplied barrier action is run at barrier |
73 |
|
*/ |
74 |
|
public void testBarrierAction() { |
75 |
|
try { |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
– |
|
90 |
|
/** |
91 |
< |
* |
91 |
> |
* A 2-party/thread barrier triggers after both threads invoke await |
92 |
|
*/ |
93 |
|
public void testTwoParties() { |
94 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
117 |
|
|
118 |
|
|
119 |
|
/** |
120 |
< |
* |
120 |
> |
* An interruption in one party causes others waiting in await to |
121 |
> |
* throw BrokenBarrierException |
122 |
|
*/ |
123 |
|
public void testAwait1_Interrupted_BrokenBarrier() { |
124 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
129 |
|
threadShouldThrow(); |
130 |
|
} catch(InterruptedException success){} |
131 |
|
catch(Exception b){ |
132 |
< |
threadFail("should throw IE"); |
132 |
> |
threadUnexpectedException(); |
133 |
|
} |
134 |
|
} |
135 |
|
}); |
140 |
|
threadShouldThrow(); |
141 |
|
} catch(BrokenBarrierException success){ |
142 |
|
} catch(Exception i){ |
143 |
< |
threadFail("should throw BBE"); |
143 |
> |
threadUnexpectedException(); |
144 |
|
} |
145 |
|
} |
146 |
|
}); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* |
160 |
> |
* An interruption in one party causes others waiting in timed await to |
161 |
> |
* throw BrokenBarrierException |
162 |
|
*/ |
163 |
|
public void testAwait2_Interrupted_BrokenBarrier() { |
164 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
169 |
|
threadShouldThrow(); |
170 |
|
} catch(InterruptedException success){ |
171 |
|
} catch(Exception b){ |
172 |
< |
threadFail("should throw IE"); |
172 |
> |
threadUnexpectedException(); |
173 |
|
} |
174 |
|
} |
175 |
|
}); |
180 |
|
threadShouldThrow(); |
181 |
|
} catch(BrokenBarrierException success){ |
182 |
|
} catch(Exception i){ |
183 |
< |
threadFail("should throw BBE"); |
183 |
> |
threadUnexpectedException(); |
184 |
|
} |
185 |
|
} |
186 |
|
}); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
< |
* |
200 |
> |
* A timeout in timed await throws TimeoutException |
201 |
|
*/ |
202 |
|
public void testAwait3_TimeOutException() { |
203 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
208 |
|
threadShouldThrow(); |
209 |
|
} catch(TimeoutException success){ |
210 |
|
} catch(Exception b){ |
211 |
< |
threadFail("should throw TOE"); |
211 |
> |
threadUnexpectedException(); |
212 |
|
|
213 |
|
} |
214 |
|
} |
220 |
|
unexpectedException(); |
221 |
|
} |
222 |
|
} |
223 |
+ |
|
224 |
+ |
/** |
225 |
+ |
* A timeout in one party causes others waiting in timed await to |
226 |
+ |
* throw BrokenBarrierException |
227 |
+ |
*/ |
228 |
+ |
public void testAwait4_Timeout_BrokenBarrier() { |
229 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
230 |
+ |
Thread t1 = new Thread(new Runnable() { |
231 |
+ |
public void run() { |
232 |
+ |
try { |
233 |
+ |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
234 |
+ |
threadShouldThrow(); |
235 |
+ |
} catch(TimeoutException success){ |
236 |
+ |
} catch(Exception b){ |
237 |
+ |
threadUnexpectedException(); |
238 |
+ |
} |
239 |
+ |
} |
240 |
+ |
}); |
241 |
+ |
Thread t2 = new Thread(new Runnable() { |
242 |
+ |
public void run() { |
243 |
+ |
try { |
244 |
+ |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
245 |
+ |
threadShouldThrow(); |
246 |
+ |
} catch(BrokenBarrierException success){ |
247 |
+ |
} catch(Exception i){ |
248 |
+ |
threadUnexpectedException(); |
249 |
+ |
} |
250 |
+ |
} |
251 |
+ |
}); |
252 |
+ |
try { |
253 |
+ |
t1.start(); |
254 |
+ |
t2.start(); |
255 |
+ |
t1.join(); |
256 |
+ |
t2.join(); |
257 |
+ |
} catch(InterruptedException e){ |
258 |
+ |
unexpectedException(); |
259 |
+ |
} |
260 |
+ |
} |
261 |
+ |
|
262 |
+ |
/** |
263 |
+ |
* A timeout in one party causes others waiting in await to |
264 |
+ |
* throw BrokenBarrierException |
265 |
+ |
*/ |
266 |
+ |
public void testAwait5_Timeout_BrokenBarrier() { |
267 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
268 |
+ |
Thread t1 = new Thread(new Runnable() { |
269 |
+ |
public void run() { |
270 |
+ |
try { |
271 |
+ |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
272 |
+ |
threadShouldThrow(); |
273 |
+ |
} catch(TimeoutException success){ |
274 |
+ |
} catch(Exception b){ |
275 |
+ |
threadUnexpectedException(); |
276 |
+ |
} |
277 |
+ |
} |
278 |
+ |
}); |
279 |
+ |
Thread t2 = new Thread(new Runnable() { |
280 |
+ |
public void run() { |
281 |
+ |
try { |
282 |
+ |
c.await(); |
283 |
+ |
threadShouldThrow(); |
284 |
+ |
} catch(BrokenBarrierException success){ |
285 |
+ |
} catch(Exception i){ |
286 |
+ |
threadUnexpectedException(); |
287 |
+ |
} |
288 |
+ |
} |
289 |
+ |
}); |
290 |
+ |
try { |
291 |
+ |
t1.start(); |
292 |
+ |
t2.start(); |
293 |
+ |
t1.join(); |
294 |
+ |
t2.join(); |
295 |
+ |
} catch(InterruptedException e){ |
296 |
+ |
unexpectedException(); |
297 |
+ |
} |
298 |
+ |
} |
299 |
|
|
300 |
+ |
/** |
301 |
+ |
* A reset of an active barrier causes waiting threads to throw |
302 |
+ |
* BrokenBarrierException |
303 |
+ |
*/ |
304 |
+ |
public void testReset_BrokenBarrier() { |
305 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
306 |
+ |
Thread t1 = new Thread(new Runnable() { |
307 |
+ |
public void run() { |
308 |
+ |
try { |
309 |
+ |
c.await(); |
310 |
+ |
threadShouldThrow(); |
311 |
+ |
} catch(BrokenBarrierException success){} |
312 |
+ |
catch(Exception b){ |
313 |
+ |
threadUnexpectedException(); |
314 |
+ |
} |
315 |
+ |
} |
316 |
+ |
}); |
317 |
+ |
Thread t2 = new Thread(new Runnable() { |
318 |
+ |
public void run() { |
319 |
+ |
try { |
320 |
+ |
c.await(); |
321 |
+ |
threadShouldThrow(); |
322 |
+ |
} catch(BrokenBarrierException success){ |
323 |
+ |
} catch(Exception i){ |
324 |
+ |
threadUnexpectedException(); |
325 |
+ |
} |
326 |
+ |
} |
327 |
+ |
}); |
328 |
+ |
try { |
329 |
+ |
t1.start(); |
330 |
+ |
t2.start(); |
331 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
332 |
+ |
c.reset(); |
333 |
+ |
t1.join(); |
334 |
+ |
t2.join(); |
335 |
+ |
} catch(InterruptedException e){ |
336 |
+ |
unexpectedException(); |
337 |
+ |
} |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
/** |
341 |
+ |
* A reset before threads enter barrier does not throw |
342 |
+ |
* BrokenBarrierException |
343 |
+ |
*/ |
344 |
+ |
public void testReset_NoBrokenBarrier() { |
345 |
+ |
final CyclicBarrier c = new CyclicBarrier(3); |
346 |
+ |
Thread t1 = new Thread(new Runnable() { |
347 |
+ |
public void run() { |
348 |
+ |
try { |
349 |
+ |
c.await(); |
350 |
+ |
} catch(Exception b){ |
351 |
+ |
threadUnexpectedException(); |
352 |
+ |
} |
353 |
+ |
} |
354 |
+ |
}); |
355 |
+ |
Thread t2 = new Thread(new Runnable() { |
356 |
+ |
public void run() { |
357 |
+ |
try { |
358 |
+ |
c.await(); |
359 |
+ |
} catch(Exception i){ |
360 |
+ |
threadUnexpectedException(); |
361 |
+ |
} |
362 |
+ |
} |
363 |
+ |
}); |
364 |
+ |
try { |
365 |
+ |
c.reset(); |
366 |
+ |
t1.start(); |
367 |
+ |
t2.start(); |
368 |
+ |
c.await(); |
369 |
+ |
t1.join(); |
370 |
+ |
t2.join(); |
371 |
+ |
} catch(Exception e){ |
372 |
+ |
unexpectedException(); |
373 |
+ |
} |
374 |
+ |
} |
375 |
+ |
|
376 |
|
} |