1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
|
|
|
13 |
dl |
1.2 |
public class CyclicBarrierTest extends JSR166TestCase{ |
14 |
dl |
1.1 |
public static void main(String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(CyclicBarrierTest.class); |
19 |
|
|
} |
20 |
dl |
1.3 |
|
21 |
|
|
private volatile int countAction; |
22 |
|
|
private class MyAction implements Runnable { |
23 |
|
|
public void run() { ++countAction; } |
24 |
|
|
} |
25 |
dl |
1.1 |
|
26 |
dl |
1.3 |
/** |
27 |
dl |
1.4 |
* Creating with negative parties throws IAE |
28 |
dl |
1.3 |
*/ |
29 |
|
|
public void testConstructor1() { |
30 |
|
|
try { |
31 |
dl |
1.1 |
new CyclicBarrier(-1, (Runnable)null); |
32 |
dl |
1.3 |
shouldThrow(); |
33 |
dl |
1.1 |
} catch(IllegalArgumentException e){} |
34 |
|
|
} |
35 |
|
|
|
36 |
dl |
1.3 |
/** |
37 |
dl |
1.4 |
* Creating with negative parties and no action throws IAE |
38 |
dl |
1.3 |
*/ |
39 |
|
|
public void testConstructor2() { |
40 |
|
|
try { |
41 |
dl |
1.1 |
new CyclicBarrier(-1); |
42 |
dl |
1.3 |
shouldThrow(); |
43 |
dl |
1.1 |
} catch(IllegalArgumentException e){} |
44 |
|
|
} |
45 |
|
|
|
46 |
dl |
1.3 |
/** |
47 |
dl |
1.4 |
* getParties returns the number of parties given in constructor |
48 |
dl |
1.3 |
*/ |
49 |
dl |
1.4 |
public void testGetParties() { |
50 |
dl |
1.1 |
CyclicBarrier b = new CyclicBarrier(2); |
51 |
|
|
assertEquals(2, b.getParties()); |
52 |
|
|
assertEquals(0, b.getNumberWaiting()); |
53 |
|
|
} |
54 |
|
|
|
55 |
dl |
1.3 |
/** |
56 |
dl |
1.5 |
* A 1-party barrier triggers after single await |
57 |
dl |
1.3 |
*/ |
58 |
dl |
1.1 |
public void testSingleParty() { |
59 |
|
|
try { |
60 |
|
|
CyclicBarrier b = new CyclicBarrier(1); |
61 |
|
|
assertEquals(1, b.getParties()); |
62 |
|
|
assertEquals(0, b.getNumberWaiting()); |
63 |
|
|
b.await(); |
64 |
|
|
b.await(); |
65 |
|
|
assertEquals(0, b.getNumberWaiting()); |
66 |
|
|
} |
67 |
|
|
catch(Exception e) { |
68 |
dl |
1.3 |
unexpectedException(); |
69 |
dl |
1.1 |
} |
70 |
|
|
} |
71 |
|
|
|
72 |
dl |
1.3 |
/** |
73 |
dl |
1.4 |
* The supplied barrier action is run at barrier |
74 |
dl |
1.3 |
*/ |
75 |
dl |
1.1 |
public void testBarrierAction() { |
76 |
|
|
try { |
77 |
|
|
countAction = 0; |
78 |
|
|
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
79 |
|
|
assertEquals(1, b.getParties()); |
80 |
|
|
assertEquals(0, b.getNumberWaiting()); |
81 |
|
|
b.await(); |
82 |
|
|
b.await(); |
83 |
|
|
assertEquals(0, b.getNumberWaiting()); |
84 |
|
|
assertEquals(countAction, 2); |
85 |
|
|
} |
86 |
|
|
catch(Exception e) { |
87 |
dl |
1.3 |
unexpectedException(); |
88 |
dl |
1.1 |
} |
89 |
|
|
} |
90 |
|
|
|
91 |
dl |
1.3 |
/** |
92 |
dl |
1.5 |
* A 2-party/thread barrier triggers after both threads invoke await |
93 |
dl |
1.3 |
*/ |
94 |
|
|
public void testTwoParties() { |
95 |
dl |
1.1 |
final CyclicBarrier b = new CyclicBarrier(2); |
96 |
|
|
Thread t = new Thread(new Runnable() { |
97 |
dl |
1.3 |
public void run() { |
98 |
dl |
1.1 |
try { |
99 |
|
|
b.await(); |
100 |
|
|
b.await(); |
101 |
|
|
b.await(); |
102 |
|
|
b.await(); |
103 |
|
|
} catch(Exception e){ |
104 |
dl |
1.3 |
threadUnexpectedException(); |
105 |
dl |
1.1 |
}}}); |
106 |
|
|
|
107 |
|
|
try { |
108 |
|
|
t.start(); |
109 |
|
|
b.await(); |
110 |
|
|
b.await(); |
111 |
|
|
b.await(); |
112 |
|
|
b.await(); |
113 |
|
|
t.join(); |
114 |
|
|
} catch(Exception e){ |
115 |
dl |
1.3 |
unexpectedException(); |
116 |
dl |
1.1 |
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
dl |
1.3 |
/** |
121 |
dl |
1.4 |
* An interruption in one party causes others waiting in await to |
122 |
|
|
* throw BrokenBarrierException |
123 |
dl |
1.3 |
*/ |
124 |
|
|
public void testAwait1_Interrupted_BrokenBarrier() { |
125 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(3); |
126 |
|
|
Thread t1 = new Thread(new Runnable() { |
127 |
dl |
1.3 |
public void run() { |
128 |
|
|
try { |
129 |
dl |
1.1 |
c.await(); |
130 |
dl |
1.3 |
threadShouldThrow(); |
131 |
dl |
1.1 |
} catch(InterruptedException success){} |
132 |
|
|
catch(Exception b){ |
133 |
dl |
1.4 |
threadUnexpectedException(); |
134 |
dl |
1.1 |
} |
135 |
|
|
} |
136 |
|
|
}); |
137 |
dl |
1.3 |
Thread t2 = new Thread(new Runnable() { |
138 |
|
|
public void run() { |
139 |
|
|
try { |
140 |
dl |
1.1 |
c.await(); |
141 |
dl |
1.3 |
threadShouldThrow(); |
142 |
dl |
1.1 |
} catch(BrokenBarrierException success){ |
143 |
|
|
} catch(Exception i){ |
144 |
dl |
1.4 |
threadUnexpectedException(); |
145 |
dl |
1.1 |
} |
146 |
|
|
} |
147 |
|
|
}); |
148 |
|
|
try { |
149 |
|
|
t1.start(); |
150 |
|
|
t2.start(); |
151 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
152 |
|
|
t1.interrupt(); |
153 |
|
|
t1.join(); |
154 |
|
|
t2.join(); |
155 |
|
|
} catch(InterruptedException e){ |
156 |
dl |
1.3 |
unexpectedException(); |
157 |
dl |
1.1 |
} |
158 |
|
|
} |
159 |
|
|
|
160 |
dl |
1.3 |
/** |
161 |
dl |
1.4 |
* An interruption in one party causes others waiting in timed await to |
162 |
|
|
* throw BrokenBarrierException |
163 |
dl |
1.3 |
*/ |
164 |
|
|
public void testAwait2_Interrupted_BrokenBarrier() { |
165 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(3); |
166 |
|
|
Thread t1 = new Thread(new Runnable() { |
167 |
dl |
1.3 |
public void run() { |
168 |
|
|
try { |
169 |
dl |
1.7 |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
170 |
dl |
1.3 |
threadShouldThrow(); |
171 |
dl |
1.1 |
} catch(InterruptedException success){ |
172 |
|
|
} catch(Exception b){ |
173 |
dl |
1.4 |
threadUnexpectedException(); |
174 |
dl |
1.1 |
} |
175 |
|
|
} |
176 |
|
|
}); |
177 |
dl |
1.3 |
Thread t2 = new Thread(new Runnable() { |
178 |
|
|
public void run() { |
179 |
|
|
try { |
180 |
dl |
1.7 |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
181 |
dl |
1.3 |
threadShouldThrow(); |
182 |
dl |
1.1 |
} catch(BrokenBarrierException success){ |
183 |
|
|
} catch(Exception i){ |
184 |
dl |
1.4 |
threadUnexpectedException(); |
185 |
dl |
1.1 |
} |
186 |
|
|
} |
187 |
|
|
}); |
188 |
|
|
try { |
189 |
|
|
t1.start(); |
190 |
|
|
t2.start(); |
191 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
192 |
|
|
t1.interrupt(); |
193 |
|
|
t1.join(); |
194 |
|
|
t2.join(); |
195 |
|
|
} catch(InterruptedException e){ |
196 |
dl |
1.3 |
unexpectedException(); |
197 |
dl |
1.1 |
} |
198 |
|
|
} |
199 |
|
|
|
200 |
dl |
1.3 |
/** |
201 |
dl |
1.4 |
* A timeout in timed await throws TimeoutException |
202 |
dl |
1.3 |
*/ |
203 |
|
|
public void testAwait3_TimeOutException() { |
204 |
dl |
1.1 |
final CyclicBarrier c = new CyclicBarrier(2); |
205 |
|
|
Thread t = new Thread(new Runnable() { |
206 |
dl |
1.3 |
public void run() { |
207 |
|
|
try { |
208 |
dl |
1.1 |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
209 |
dl |
1.3 |
threadShouldThrow(); |
210 |
dl |
1.1 |
} catch(TimeoutException success){ |
211 |
|
|
} catch(Exception b){ |
212 |
dl |
1.4 |
threadUnexpectedException(); |
213 |
dl |
1.1 |
|
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
}); |
217 |
|
|
try { |
218 |
|
|
t.start(); |
219 |
|
|
t.join(); |
220 |
dl |
1.5 |
} catch(InterruptedException e){ |
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 |
dl |
1.1 |
} catch(InterruptedException e){ |
297 |
dl |
1.3 |
unexpectedException(); |
298 |
dl |
1.1 |
} |
299 |
|
|
} |
300 |
|
|
|
301 |
dl |
1.4 |
/** |
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 |
dl |
1.1 |
} |