1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.util.concurrent.locks.*; |
13 |
|
import java.util.concurrent.atomic.*; |
14 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 |
|
|
16 |
|
public class CyclicBarrierTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(CyclicBarrierTest.class); |
33 |
|
try { |
34 |
|
new CyclicBarrier(-1, (Runnable)null); |
35 |
|
shouldThrow(); |
36 |
< |
} catch (IllegalArgumentException e) {} |
36 |
> |
} catch (IllegalArgumentException success) {} |
37 |
|
} |
38 |
|
|
39 |
|
/** |
43 |
|
try { |
44 |
|
new CyclicBarrier(-1); |
45 |
|
shouldThrow(); |
46 |
< |
} catch (IllegalArgumentException e) {} |
46 |
> |
} catch (IllegalArgumentException success) {} |
47 |
|
} |
48 |
|
|
49 |
|
/** |
58 |
|
/** |
59 |
|
* A 1-party barrier triggers after single await |
60 |
|
*/ |
61 |
< |
public void testSingleParty() { |
62 |
< |
try { |
63 |
< |
CyclicBarrier b = new CyclicBarrier(1); |
64 |
< |
assertEquals(1, b.getParties()); |
65 |
< |
assertEquals(0, b.getNumberWaiting()); |
66 |
< |
b.await(); |
67 |
< |
b.await(); |
67 |
< |
assertEquals(0, b.getNumberWaiting()); |
68 |
< |
} |
69 |
< |
catch (Exception e) { |
70 |
< |
unexpectedException(); |
71 |
< |
} |
61 |
> |
public void testSingleParty() throws Exception { |
62 |
> |
CyclicBarrier b = new CyclicBarrier(1); |
63 |
> |
assertEquals(1, b.getParties()); |
64 |
> |
assertEquals(0, b.getNumberWaiting()); |
65 |
> |
b.await(); |
66 |
> |
b.await(); |
67 |
> |
assertEquals(0, b.getNumberWaiting()); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
|
* The supplied barrier action is run at barrier |
72 |
|
*/ |
73 |
< |
public void testBarrierAction() { |
74 |
< |
try { |
75 |
< |
countAction = 0; |
76 |
< |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
77 |
< |
assertEquals(1, b.getParties()); |
78 |
< |
assertEquals(0, b.getNumberWaiting()); |
79 |
< |
b.await(); |
80 |
< |
b.await(); |
81 |
< |
assertEquals(0, b.getNumberWaiting()); |
86 |
< |
assertEquals(countAction, 2); |
87 |
< |
} |
88 |
< |
catch (Exception e) { |
89 |
< |
unexpectedException(); |
90 |
< |
} |
73 |
> |
public void testBarrierAction() throws Exception { |
74 |
> |
countAction = 0; |
75 |
> |
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
76 |
> |
assertEquals(1, b.getParties()); |
77 |
> |
assertEquals(0, b.getNumberWaiting()); |
78 |
> |
b.await(); |
79 |
> |
b.await(); |
80 |
> |
assertEquals(0, b.getNumberWaiting()); |
81 |
> |
assertEquals(countAction, 2); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
|
* A 2-party/thread barrier triggers after both threads invoke await |
86 |
|
*/ |
87 |
< |
public void testTwoParties() { |
87 |
> |
public void testTwoParties() throws Exception { |
88 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
89 |
< |
Thread t = new Thread(new Runnable() { |
90 |
< |
public void run() { |
91 |
< |
try { |
92 |
< |
b.await(); |
93 |
< |
b.await(); |
94 |
< |
b.await(); |
95 |
< |
b.await(); |
96 |
< |
} catch (Exception e) { |
97 |
< |
threadUnexpectedException(); |
98 |
< |
}}}); |
99 |
< |
|
100 |
< |
try { |
101 |
< |
t.start(); |
102 |
< |
b.await(); |
112 |
< |
b.await(); |
113 |
< |
b.await(); |
114 |
< |
b.await(); |
115 |
< |
t.join(); |
116 |
< |
} catch (Exception e) { |
117 |
< |
unexpectedException(); |
118 |
< |
} |
89 |
> |
Thread t = new Thread(new CheckedRunnable() { |
90 |
> |
public void realRun() throws Exception { |
91 |
> |
b.await(); |
92 |
> |
b.await(); |
93 |
> |
b.await(); |
94 |
> |
b.await(); |
95 |
> |
}}); |
96 |
> |
|
97 |
> |
t.start(); |
98 |
> |
b.await(); |
99 |
> |
b.await(); |
100 |
> |
b.await(); |
101 |
> |
b.await(); |
102 |
> |
t.join(); |
103 |
|
} |
104 |
|
|
121 |
– |
|
105 |
|
/** |
106 |
|
* An interruption in one party causes others waiting in await to |
107 |
|
* throw BrokenBarrierException |
108 |
|
*/ |
109 |
< |
public void testAwait1_Interrupted_BrokenBarrier() { |
109 |
> |
public void testAwait1_Interrupted_BrokenBarrier() throws Exception { |
110 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
111 |
< |
Thread t1 = new Thread(new Runnable() { |
112 |
< |
public void run() { |
113 |
< |
try { |
114 |
< |
c.await(); |
115 |
< |
threadShouldThrow(); |
116 |
< |
} catch (InterruptedException success) {} |
117 |
< |
catch (Exception b) { |
118 |
< |
threadUnexpectedException(); |
119 |
< |
} |
120 |
< |
} |
121 |
< |
}); |
122 |
< |
Thread t2 = new Thread(new Runnable() { |
123 |
< |
public void run() { |
124 |
< |
try { |
125 |
< |
c.await(); |
143 |
< |
threadShouldThrow(); |
144 |
< |
} catch (BrokenBarrierException success) { |
145 |
< |
} catch (Exception i) { |
146 |
< |
threadUnexpectedException(); |
147 |
< |
} |
148 |
< |
} |
149 |
< |
}); |
150 |
< |
try { |
151 |
< |
t1.start(); |
152 |
< |
t2.start(); |
153 |
< |
Thread.sleep(SHORT_DELAY_MS); |
154 |
< |
t1.interrupt(); |
155 |
< |
t1.join(); |
156 |
< |
t2.join(); |
157 |
< |
} catch (InterruptedException e) { |
158 |
< |
unexpectedException(); |
159 |
< |
} |
111 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
112 |
> |
public void realRun() throws Exception { |
113 |
> |
c.await(); |
114 |
> |
}}; |
115 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
116 |
> |
public void realRun() throws Exception { |
117 |
> |
c.await(); |
118 |
> |
}}; |
119 |
> |
|
120 |
> |
t1.start(); |
121 |
> |
t2.start(); |
122 |
> |
delay(SHORT_DELAY_MS); |
123 |
> |
t1.interrupt(); |
124 |
> |
t1.join(); |
125 |
> |
t2.join(); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* An interruption in one party causes others waiting in timed await to |
130 |
|
* throw BrokenBarrierException |
131 |
|
*/ |
132 |
< |
public void testAwait2_Interrupted_BrokenBarrier() { |
132 |
> |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
133 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
134 |
< |
Thread t1 = new Thread(new Runnable() { |
135 |
< |
public void run() { |
136 |
< |
try { |
137 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
138 |
< |
threadShouldThrow(); |
139 |
< |
} catch (InterruptedException success) { |
140 |
< |
} catch (Exception b) { |
141 |
< |
threadUnexpectedException(); |
142 |
< |
} |
143 |
< |
} |
144 |
< |
}); |
145 |
< |
Thread t2 = new Thread(new Runnable() { |
146 |
< |
public void run() { |
147 |
< |
try { |
148 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
183 |
< |
threadShouldThrow(); |
184 |
< |
} catch (BrokenBarrierException success) { |
185 |
< |
} catch (Exception i) { |
186 |
< |
threadUnexpectedException(); |
187 |
< |
} |
188 |
< |
} |
189 |
< |
}); |
190 |
< |
try { |
191 |
< |
t1.start(); |
192 |
< |
t2.start(); |
193 |
< |
Thread.sleep(SHORT_DELAY_MS); |
194 |
< |
t1.interrupt(); |
195 |
< |
t1.join(); |
196 |
< |
t2.join(); |
197 |
< |
} catch (InterruptedException e) { |
198 |
< |
unexpectedException(); |
199 |
< |
} |
134 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
135 |
> |
public void realRun() throws Exception { |
136 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
137 |
> |
}}; |
138 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
139 |
> |
public void realRun() throws Exception { |
140 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
141 |
> |
}}; |
142 |
> |
|
143 |
> |
t1.start(); |
144 |
> |
t2.start(); |
145 |
> |
delay(SHORT_DELAY_MS); |
146 |
> |
t1.interrupt(); |
147 |
> |
t1.join(); |
148 |
> |
t2.join(); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
|
* A timeout in timed await throws TimeoutException |
153 |
|
*/ |
154 |
< |
public void testAwait3_TimeOutException() { |
154 |
> |
public void testAwait3_TimeoutException() throws InterruptedException { |
155 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
156 |
< |
Thread t = new Thread(new Runnable() { |
157 |
< |
public void run() { |
158 |
< |
try { |
159 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
211 |
< |
threadShouldThrow(); |
212 |
< |
} catch (TimeoutException success) { |
213 |
< |
} catch (Exception b) { |
214 |
< |
threadUnexpectedException(); |
156 |
> |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
157 |
> |
public void realRun() throws Exception { |
158 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
159 |
> |
}}; |
160 |
|
|
161 |
< |
} |
162 |
< |
} |
218 |
< |
}); |
219 |
< |
try { |
220 |
< |
t.start(); |
221 |
< |
t.join(); |
222 |
< |
} catch (InterruptedException e) { |
223 |
< |
unexpectedException(); |
224 |
< |
} |
161 |
> |
t.start(); |
162 |
> |
t.join(); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
|
* A timeout in one party causes others waiting in timed await to |
167 |
|
* throw BrokenBarrierException |
168 |
|
*/ |
169 |
< |
public void testAwait4_Timeout_BrokenBarrier() { |
169 |
> |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
170 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
171 |
< |
Thread t1 = new Thread(new Runnable() { |
172 |
< |
public void run() { |
173 |
< |
try { |
174 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
175 |
< |
threadShouldThrow(); |
176 |
< |
} catch (TimeoutException success) { |
177 |
< |
} catch (Exception b) { |
178 |
< |
threadUnexpectedException(); |
179 |
< |
} |
180 |
< |
} |
181 |
< |
}); |
182 |
< |
Thread t2 = new Thread(new Runnable() { |
183 |
< |
public void run() { |
246 |
< |
try { |
247 |
< |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
248 |
< |
threadShouldThrow(); |
249 |
< |
} catch (BrokenBarrierException success) { |
250 |
< |
} catch (Exception i) { |
251 |
< |
threadUnexpectedException(); |
252 |
< |
} |
253 |
< |
} |
254 |
< |
}); |
255 |
< |
try { |
256 |
< |
t1.start(); |
257 |
< |
t2.start(); |
258 |
< |
t1.join(); |
259 |
< |
t2.join(); |
260 |
< |
} catch (InterruptedException e) { |
261 |
< |
unexpectedException(); |
262 |
< |
} |
171 |
> |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
172 |
> |
public void realRun() throws Exception { |
173 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
174 |
> |
}}; |
175 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
176 |
> |
public void realRun() throws Exception { |
177 |
> |
c.await(MEDIUM_DELAY_MS, MILLISECONDS); |
178 |
> |
}}; |
179 |
> |
|
180 |
> |
t1.start(); |
181 |
> |
t2.start(); |
182 |
> |
t1.join(); |
183 |
> |
t2.join(); |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
|
* A timeout in one party causes others waiting in await to |
188 |
|
* throw BrokenBarrierException |
189 |
|
*/ |
190 |
< |
public void testAwait5_Timeout_BrokenBarrier() { |
190 |
> |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
191 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
192 |
< |
Thread t1 = new Thread(new Runnable() { |
193 |
< |
public void run() { |
194 |
< |
try { |
195 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
196 |
< |
threadShouldThrow(); |
197 |
< |
} catch (TimeoutException success) { |
198 |
< |
} catch (Exception b) { |
199 |
< |
threadUnexpectedException(); |
200 |
< |
} |
201 |
< |
} |
202 |
< |
}); |
203 |
< |
Thread t2 = new Thread(new Runnable() { |
204 |
< |
public void run() { |
284 |
< |
try { |
285 |
< |
c.await(); |
286 |
< |
threadShouldThrow(); |
287 |
< |
} catch (BrokenBarrierException success) { |
288 |
< |
} catch (Exception i) { |
289 |
< |
threadUnexpectedException(); |
290 |
< |
} |
291 |
< |
} |
292 |
< |
}); |
293 |
< |
try { |
294 |
< |
t1.start(); |
295 |
< |
t2.start(); |
296 |
< |
t1.join(); |
297 |
< |
t2.join(); |
298 |
< |
} catch (InterruptedException e) { |
299 |
< |
unexpectedException(); |
300 |
< |
} |
192 |
> |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
193 |
> |
public void realRun() throws Exception { |
194 |
> |
c.await(SHORT_DELAY_MS, MILLISECONDS); |
195 |
> |
}}; |
196 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
197 |
> |
public void realRun() throws Exception { |
198 |
> |
c.await(); |
199 |
> |
}}; |
200 |
> |
|
201 |
> |
t1.start(); |
202 |
> |
t2.start(); |
203 |
> |
t1.join(); |
204 |
> |
t2.join(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* A reset of an active barrier causes waiting threads to throw |
209 |
|
* BrokenBarrierException |
210 |
|
*/ |
211 |
< |
public void testReset_BrokenBarrier() { |
211 |
> |
public void testReset_BrokenBarrier() throws InterruptedException { |
212 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
213 |
< |
Thread t1 = new Thread(new Runnable() { |
214 |
< |
public void run() { |
215 |
< |
try { |
216 |
< |
c.await(); |
217 |
< |
threadShouldThrow(); |
218 |
< |
} catch (BrokenBarrierException success) {} |
219 |
< |
catch (Exception b) { |
220 |
< |
threadUnexpectedException(); |
221 |
< |
} |
222 |
< |
} |
223 |
< |
}); |
224 |
< |
Thread t2 = new Thread(new Runnable() { |
225 |
< |
public void run() { |
226 |
< |
try { |
227 |
< |
c.await(); |
324 |
< |
threadShouldThrow(); |
325 |
< |
} catch (BrokenBarrierException success) { |
326 |
< |
} catch (Exception i) { |
327 |
< |
threadUnexpectedException(); |
328 |
< |
} |
329 |
< |
} |
330 |
< |
}); |
331 |
< |
try { |
332 |
< |
t1.start(); |
333 |
< |
t2.start(); |
334 |
< |
Thread.sleep(SHORT_DELAY_MS); |
335 |
< |
c.reset(); |
336 |
< |
t1.join(); |
337 |
< |
t2.join(); |
338 |
< |
} catch (InterruptedException e) { |
339 |
< |
unexpectedException(); |
340 |
< |
} |
213 |
> |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
214 |
> |
public void realRun() throws Exception { |
215 |
> |
c.await(); |
216 |
> |
}}; |
217 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
218 |
> |
public void realRun() throws Exception { |
219 |
> |
c.await(); |
220 |
> |
}}; |
221 |
> |
|
222 |
> |
t1.start(); |
223 |
> |
t2.start(); |
224 |
> |
delay(SHORT_DELAY_MS); |
225 |
> |
c.reset(); |
226 |
> |
t1.join(); |
227 |
> |
t2.join(); |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
|
* A reset before threads enter barrier does not throw |
232 |
|
* BrokenBarrierException |
233 |
|
*/ |
234 |
< |
public void testReset_NoBrokenBarrier() { |
234 |
> |
public void testReset_NoBrokenBarrier() throws Exception { |
235 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
236 |
< |
Thread t1 = new Thread(new Runnable() { |
237 |
< |
public void run() { |
238 |
< |
try { |
239 |
< |
c.await(); |
240 |
< |
} catch (Exception b) { |
241 |
< |
threadUnexpectedException(); |
242 |
< |
} |
243 |
< |
} |
244 |
< |
}); |
245 |
< |
Thread t2 = new Thread(new Runnable() { |
246 |
< |
public void run() { |
247 |
< |
try { |
248 |
< |
c.await(); |
249 |
< |
} catch (Exception i) { |
250 |
< |
threadUnexpectedException(); |
364 |
< |
} |
365 |
< |
} |
366 |
< |
}); |
367 |
< |
try { |
368 |
< |
c.reset(); |
369 |
< |
t1.start(); |
370 |
< |
t2.start(); |
371 |
< |
c.await(); |
372 |
< |
t1.join(); |
373 |
< |
t2.join(); |
374 |
< |
} catch (Exception e) { |
375 |
< |
unexpectedException(); |
376 |
< |
} |
236 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
237 |
> |
public void realRun() throws Exception { |
238 |
> |
c.await(); |
239 |
> |
}}); |
240 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
241 |
> |
public void realRun() throws Exception { |
242 |
> |
c.await(); |
243 |
> |
}}); |
244 |
> |
|
245 |
> |
c.reset(); |
246 |
> |
t1.start(); |
247 |
> |
t2.start(); |
248 |
> |
c.await(); |
249 |
> |
t1.join(); |
250 |
> |
t2.join(); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
|
* All threads block while a barrier is broken. |
255 |
|
*/ |
256 |
< |
public void testReset_Leakage() { |
257 |
< |
try { |
258 |
< |
final CyclicBarrier c = new CyclicBarrier(2); |
259 |
< |
final AtomicBoolean done = new AtomicBoolean(); |
260 |
< |
Thread t = new Thread() { |
261 |
< |
public void run() { |
262 |
< |
while (!done.get()) { |
263 |
< |
try { |
264 |
< |
while (c.isBroken()) |
391 |
< |
c.reset(); |
392 |
< |
|
393 |
< |
c.await(); |
394 |
< |
threadFail("await should not return"); |
395 |
< |
} |
396 |
< |
catch (BrokenBarrierException e) { |
397 |
< |
} |
398 |
< |
catch (InterruptedException ie) { |
399 |
< |
} |
400 |
< |
} |
401 |
< |
} |
402 |
< |
}; |
256 |
> |
public void testReset_Leakage() throws InterruptedException { |
257 |
> |
final CyclicBarrier c = new CyclicBarrier(2); |
258 |
> |
final AtomicBoolean done = new AtomicBoolean(); |
259 |
> |
Thread t = new Thread(new CheckedRunnable() { |
260 |
> |
public void realRun() { |
261 |
> |
while (!done.get()) { |
262 |
> |
try { |
263 |
> |
while (c.isBroken()) |
264 |
> |
c.reset(); |
265 |
|
|
266 |
< |
t.start(); |
267 |
< |
for ( int i = 0; i < 4; i++) { |
268 |
< |
Thread.sleep(SHORT_DELAY_MS); |
269 |
< |
t.interrupt(); |
270 |
< |
} |
271 |
< |
done.set(true); |
266 |
> |
c.await(); |
267 |
> |
shouldThrow(); |
268 |
> |
} |
269 |
> |
catch (BrokenBarrierException ok) {} |
270 |
> |
catch (InterruptedException ok) {} |
271 |
> |
}}}); |
272 |
> |
|
273 |
> |
t.start(); |
274 |
> |
for (int i = 0; i < 4; i++) { |
275 |
> |
delay(timeoutMillis()); |
276 |
|
t.interrupt(); |
277 |
|
} |
278 |
< |
catch (Exception ex) { |
279 |
< |
unexpectedException(); |
280 |
< |
} |
278 |
> |
done.set(true); |
279 |
> |
t.interrupt(); |
280 |
> |
awaitTermination(t); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
|
* Reset of a non-broken barrier does not break barrier |
285 |
|
*/ |
286 |
< |
public void testResetWithoutBreakage() { |
287 |
< |
try { |
288 |
< |
final CyclicBarrier start = new CyclicBarrier(3); |
289 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
290 |
< |
for (int i = 0; i < 3; i++) { |
291 |
< |
Thread t1 = new Thread(new Runnable() { |
292 |
< |
public void run() { |
293 |
< |
try { start.await(); } |
294 |
< |
catch (Exception ie) { |
295 |
< |
threadFail("start barrier"); |
296 |
< |
} |
297 |
< |
try { barrier.await(); } |
298 |
< |
catch (Throwable thrown) { |
299 |
< |
unexpectedException(); |
300 |
< |
}}}); |
301 |
< |
|
302 |
< |
Thread t2 = new Thread(new Runnable() { |
303 |
< |
public void run() { |
304 |
< |
try { start.await(); } |
305 |
< |
catch (Exception ie) { |
306 |
< |
threadFail("start barrier"); |
307 |
< |
} |
308 |
< |
try { barrier.await(); } |
309 |
< |
catch (Throwable thrown) { |
310 |
< |
unexpectedException(); |
311 |
< |
}}}); |
312 |
< |
|
447 |
< |
|
448 |
< |
t1.start(); |
449 |
< |
t2.start(); |
450 |
< |
try { start.await(); } |
451 |
< |
catch (Exception ie) { threadFail("start barrier"); } |
452 |
< |
barrier.await(); |
453 |
< |
t1.join(); |
454 |
< |
t2.join(); |
455 |
< |
assertFalse(barrier.isBroken()); |
456 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
457 |
< |
if (i == 1) barrier.reset(); |
458 |
< |
assertFalse(barrier.isBroken()); |
459 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
460 |
< |
} |
461 |
< |
} |
462 |
< |
catch (Exception ex) { |
463 |
< |
unexpectedException(); |
286 |
> |
public void testResetWithoutBreakage() throws Exception { |
287 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
288 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
289 |
> |
for (int i = 0; i < 3; i++) { |
290 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
291 |
> |
public void realRun() throws Exception { |
292 |
> |
start.await(); |
293 |
> |
barrier.await(); |
294 |
> |
}}); |
295 |
> |
|
296 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
297 |
> |
public void realRun() throws Exception { |
298 |
> |
start.await(); |
299 |
> |
barrier.await(); |
300 |
> |
}}); |
301 |
> |
|
302 |
> |
t1.start(); |
303 |
> |
t2.start(); |
304 |
> |
start.await(); |
305 |
> |
barrier.await(); |
306 |
> |
t1.join(); |
307 |
> |
t2.join(); |
308 |
> |
assertFalse(barrier.isBroken()); |
309 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
310 |
> |
if (i == 1) barrier.reset(); |
311 |
> |
assertFalse(barrier.isBroken()); |
312 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
|
* Reset of a barrier after interruption reinitializes it. |
318 |
|
*/ |
319 |
< |
public void testResetAfterInterrupt() { |
320 |
< |
try { |
321 |
< |
final CyclicBarrier start = new CyclicBarrier(3); |
322 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
323 |
< |
for (int i = 0; i < 2; i++) { |
324 |
< |
Thread t1 = new Thread(new Runnable() { |
325 |
< |
public void run() { |
326 |
< |
try { start.await(); } |
327 |
< |
catch (Exception ie) { |
328 |
< |
threadFail("start barrier"); |
329 |
< |
} |
330 |
< |
try { barrier.await(); } |
331 |
< |
catch (InterruptedException ok) {} |
332 |
< |
catch (Throwable thrown) { |
333 |
< |
unexpectedException(); |
334 |
< |
}}}); |
335 |
< |
|
336 |
< |
Thread t2 = new Thread(new Runnable() { |
337 |
< |
public void run() { |
338 |
< |
try { start.await(); } |
339 |
< |
catch (Exception ie) { |
340 |
< |
threadFail("start barrier"); |
341 |
< |
} |
342 |
< |
try { barrier.await(); } |
343 |
< |
catch (BrokenBarrierException ok) {} |
344 |
< |
catch (Throwable thrown) { |
345 |
< |
unexpectedException(); |
497 |
< |
}}}); |
498 |
< |
|
499 |
< |
t1.start(); |
500 |
< |
t2.start(); |
501 |
< |
try { start.await(); } |
502 |
< |
catch (Exception ie) { threadFail("start barrier"); } |
503 |
< |
t1.interrupt(); |
504 |
< |
t1.join(); |
505 |
< |
t2.join(); |
506 |
< |
assertTrue(barrier.isBroken()); |
507 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
508 |
< |
barrier.reset(); |
509 |
< |
assertFalse(barrier.isBroken()); |
510 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
511 |
< |
} |
512 |
< |
} |
513 |
< |
catch (Exception ex) { |
514 |
< |
unexpectedException(); |
319 |
> |
public void testResetAfterInterrupt() throws Exception { |
320 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
321 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
322 |
> |
for (int i = 0; i < 2; i++) { |
323 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
324 |
> |
public void realRun() throws Exception { |
325 |
> |
start.await(); |
326 |
> |
barrier.await(); |
327 |
> |
}}; |
328 |
> |
|
329 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
330 |
> |
public void realRun() throws Exception { |
331 |
> |
start.await(); |
332 |
> |
barrier.await(); |
333 |
> |
}}; |
334 |
> |
|
335 |
> |
t1.start(); |
336 |
> |
t2.start(); |
337 |
> |
start.await(); |
338 |
> |
t1.interrupt(); |
339 |
> |
t1.join(); |
340 |
> |
t2.join(); |
341 |
> |
assertTrue(barrier.isBroken()); |
342 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
343 |
> |
barrier.reset(); |
344 |
> |
assertFalse(barrier.isBroken()); |
345 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
346 |
|
} |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
|
* Reset of a barrier after timeout reinitializes it. |
351 |
|
*/ |
352 |
< |
public void testResetAfterTimeout() { |
353 |
< |
try { |
354 |
< |
final CyclicBarrier start = new CyclicBarrier(3); |
355 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
356 |
< |
for (int i = 0; i < 2; i++) { |
357 |
< |
Thread t1 = new Thread(new Runnable() { |
358 |
< |
public void run() { |
359 |
< |
try { start.await(); } |
360 |
< |
catch (Exception ie) { |
361 |
< |
threadFail("start barrier"); |
362 |
< |
} |
363 |
< |
try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } |
364 |
< |
catch (TimeoutException ok) {} |
365 |
< |
catch (Throwable thrown) { |
366 |
< |
unexpectedException(); |
367 |
< |
}}}); |
368 |
< |
|
369 |
< |
Thread t2 = new Thread(new Runnable() { |
370 |
< |
public void run() { |
371 |
< |
try { start.await(); } |
372 |
< |
catch (Exception ie) { |
373 |
< |
threadFail("start barrier"); |
374 |
< |
} |
375 |
< |
try { barrier.await(); } |
376 |
< |
catch (BrokenBarrierException ok) {} |
546 |
< |
catch (Throwable thrown) { |
547 |
< |
unexpectedException(); |
548 |
< |
}}}); |
549 |
< |
|
550 |
< |
t1.start(); |
551 |
< |
t2.start(); |
552 |
< |
try { start.await(); } |
553 |
< |
catch (Exception ie) { threadFail("start barrier"); } |
554 |
< |
t1.join(); |
555 |
< |
t2.join(); |
556 |
< |
assertTrue(barrier.isBroken()); |
557 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
558 |
< |
barrier.reset(); |
559 |
< |
assertFalse(barrier.isBroken()); |
560 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
561 |
< |
} |
562 |
< |
} |
563 |
< |
catch (Exception ex) { |
564 |
< |
unexpectedException(); |
352 |
> |
public void testResetAfterTimeout() throws Exception { |
353 |
> |
final CyclicBarrier start = new CyclicBarrier(2); |
354 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
355 |
> |
for (int i = 0; i < 2; i++) { |
356 |
> |
Thread t1 = new ThreadShouldThrow(TimeoutException.class) { |
357 |
> |
public void realRun() throws Exception { |
358 |
> |
start.await(); |
359 |
> |
barrier.await(SHORT_DELAY_MS, MILLISECONDS); |
360 |
> |
}}; |
361 |
> |
|
362 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
363 |
> |
public void realRun() throws Exception { |
364 |
> |
start.await(); |
365 |
> |
barrier.await(); |
366 |
> |
}}; |
367 |
> |
|
368 |
> |
t1.start(); |
369 |
> |
t2.start(); |
370 |
> |
t1.join(); |
371 |
> |
t2.join(); |
372 |
> |
assertTrue(barrier.isBroken()); |
373 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
374 |
> |
barrier.reset(); |
375 |
> |
assertFalse(barrier.isBroken()); |
376 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
377 |
|
} |
378 |
|
} |
379 |
|
|
568 |
– |
|
380 |
|
/** |
381 |
|
* Reset of a barrier after a failed command reinitializes it. |
382 |
|
*/ |
383 |
< |
public void testResetAfterCommandException() { |
384 |
< |
try { |
385 |
< |
final CyclicBarrier start = new CyclicBarrier(3); |
386 |
< |
final CyclicBarrier barrier = |
387 |
< |
new CyclicBarrier(3, new Runnable() { |
388 |
< |
public void run() { |
389 |
< |
throw new NullPointerException(); }}); |
390 |
< |
for (int i = 0; i < 2; i++) { |
391 |
< |
Thread t1 = new Thread(new Runnable() { |
392 |
< |
public void run() { |
393 |
< |
try { start.await(); } |
394 |
< |
catch (Exception ie) { |
395 |
< |
threadFail("start barrier"); |
396 |
< |
} |
397 |
< |
try { barrier.await(); } |
398 |
< |
catch (BrokenBarrierException ok) {} |
399 |
< |
catch (Throwable thrown) { |
400 |
< |
unexpectedException(); |
401 |
< |
}}}); |
402 |
< |
|
403 |
< |
Thread t2 = new Thread(new Runnable() { |
404 |
< |
public void run() { |
405 |
< |
try { start.await(); } |
406 |
< |
catch (Exception ie) { |
407 |
< |
threadFail("start barrier"); |
408 |
< |
} |
409 |
< |
try { barrier.await(); } |
410 |
< |
catch (BrokenBarrierException ok) {} |
411 |
< |
catch (Throwable thrown) { |
412 |
< |
unexpectedException(); |
413 |
< |
}}}); |
414 |
< |
|
415 |
< |
t1.start(); |
416 |
< |
t2.start(); |
606 |
< |
try { start.await(); } |
607 |
< |
catch (Exception ie) { threadFail("start barrier"); } |
608 |
< |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
609 |
< |
try { barrier.await(); } |
610 |
< |
catch (Exception ok) { } |
611 |
< |
t1.join(); |
612 |
< |
t2.join(); |
613 |
< |
assertTrue(barrier.isBroken()); |
614 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
615 |
< |
barrier.reset(); |
616 |
< |
assertFalse(barrier.isBroken()); |
617 |
< |
assertEquals(0, barrier.getNumberWaiting()); |
618 |
< |
} |
619 |
< |
} |
620 |
< |
catch (Exception ex) { |
621 |
< |
unexpectedException(); |
383 |
> |
public void testResetAfterCommandException() throws Exception { |
384 |
> |
final CyclicBarrier start = new CyclicBarrier(3); |
385 |
> |
final CyclicBarrier barrier = |
386 |
> |
new CyclicBarrier(3, new Runnable() { |
387 |
> |
public void run() { |
388 |
> |
throw new NullPointerException(); }}); |
389 |
> |
for (int i = 0; i < 2; i++) { |
390 |
> |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
391 |
> |
public void realRun() throws Exception { |
392 |
> |
start.await(); |
393 |
> |
barrier.await(); |
394 |
> |
}}; |
395 |
> |
|
396 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
397 |
> |
public void realRun() throws Exception { |
398 |
> |
start.await(); |
399 |
> |
barrier.await(); |
400 |
> |
}}; |
401 |
> |
|
402 |
> |
t1.start(); |
403 |
> |
t2.start(); |
404 |
> |
start.await(); |
405 |
> |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
406 |
> |
try { |
407 |
> |
barrier.await(); |
408 |
> |
shouldThrow(); |
409 |
> |
} catch (NullPointerException success) {} |
410 |
> |
t1.join(); |
411 |
> |
t2.join(); |
412 |
> |
assertTrue(barrier.isBroken()); |
413 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
414 |
> |
barrier.reset(); |
415 |
> |
assertFalse(barrier.isBroken()); |
416 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
417 |
|
} |
418 |
|
} |
419 |
|
} |