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