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); |
21 |
> |
return new TestSuite(CyclicBarrierTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
private volatile int countAction; |
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 |
|
/** |
51 |
|
*/ |
52 |
|
public void testGetParties() { |
53 |
|
CyclicBarrier b = new CyclicBarrier(2); |
54 |
< |
assertEquals(2, b.getParties()); |
54 |
> |
assertEquals(2, b.getParties()); |
55 |
|
assertEquals(0, b.getNumberWaiting()); |
56 |
|
} |
57 |
|
|
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(); |
111 |
< |
b.await(); |
112 |
< |
b.await(); |
113 |
< |
b.await(); |
114 |
< |
b.await(); |
115 |
< |
t.join(); |
116 |
< |
} catch (Exception e) { |
117 |
< |
unexpectedException(); |
118 |
< |
} |
89 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
> |
public void realRun() throws Exception { |
91 |
> |
b.await(); |
92 |
> |
b.await(); |
93 |
> |
b.await(); |
94 |
> |
b.await(); |
95 |
> |
}}); |
96 |
> |
|
97 |
> |
b.await(); |
98 |
> |
b.await(); |
99 |
> |
b.await(); |
100 |
> |
b.await(); |
101 |
> |
awaitTermination(t); |
102 |
|
} |
103 |
|
|
121 |
– |
|
104 |
|
/** |
105 |
|
* An interruption in one party causes others waiting in await to |
106 |
|
* throw BrokenBarrierException |
107 |
|
*/ |
108 |
|
public void testAwait1_Interrupted_BrokenBarrier() { |
109 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
110 |
< |
Thread t1 = new Thread(new Runnable() { |
111 |
< |
public void run() { |
112 |
< |
try { |
113 |
< |
c.await(); |
114 |
< |
threadShouldThrow(); |
115 |
< |
} catch (InterruptedException success) {} |
116 |
< |
catch (Exception b) { |
117 |
< |
threadUnexpectedException(); |
118 |
< |
} |
119 |
< |
} |
120 |
< |
}); |
121 |
< |
Thread t2 = new Thread(new Runnable() { |
122 |
< |
public void run() { |
123 |
< |
try { |
124 |
< |
c.await(); |
125 |
< |
threadShouldThrow(); |
126 |
< |
} catch (BrokenBarrierException success) { |
127 |
< |
} 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 |
< |
} |
110 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
111 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
112 |
> |
public void realRun() throws Exception { |
113 |
> |
pleaseInterrupt.countDown(); |
114 |
> |
c.await(); |
115 |
> |
}}; |
116 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
117 |
> |
public void realRun() throws Exception { |
118 |
> |
pleaseInterrupt.countDown(); |
119 |
> |
c.await(); |
120 |
> |
}}; |
121 |
> |
|
122 |
> |
t1.start(); |
123 |
> |
t2.start(); |
124 |
> |
await(pleaseInterrupt); |
125 |
> |
t1.interrupt(); |
126 |
> |
awaitTermination(t1); |
127 |
> |
awaitTermination(t2); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
|
* An interruption in one party causes others waiting in timed await to |
132 |
|
* throw BrokenBarrierException |
133 |
|
*/ |
134 |
< |
public void testAwait2_Interrupted_BrokenBarrier() { |
134 |
> |
public void testAwait2_Interrupted_BrokenBarrier() throws Exception { |
135 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
136 |
< |
Thread t1 = new Thread(new Runnable() { |
137 |
< |
public void run() { |
138 |
< |
try { |
139 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
140 |
< |
threadShouldThrow(); |
141 |
< |
} catch (InterruptedException success) { |
142 |
< |
} catch (Exception b) { |
143 |
< |
threadUnexpectedException(); |
144 |
< |
} |
145 |
< |
} |
146 |
< |
}); |
147 |
< |
Thread t2 = new Thread(new Runnable() { |
148 |
< |
public void run() { |
149 |
< |
try { |
150 |
< |
c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
< |
threadShouldThrow(); |
152 |
< |
} catch (BrokenBarrierException success) { |
153 |
< |
} 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 |
< |
} |
136 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
137 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
138 |
> |
public void realRun() throws Exception { |
139 |
> |
pleaseInterrupt.countDown(); |
140 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
141 |
> |
}}; |
142 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
143 |
> |
public void realRun() throws Exception { |
144 |
> |
pleaseInterrupt.countDown(); |
145 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
146 |
> |
}}; |
147 |
> |
|
148 |
> |
t1.start(); |
149 |
> |
t2.start(); |
150 |
> |
await(pleaseInterrupt); |
151 |
> |
t1.interrupt(); |
152 |
> |
awaitTermination(t1); |
153 |
> |
awaitTermination(t2); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* A timeout in timed await throws TimeoutException |
158 |
|
*/ |
159 |
< |
public void testAwait3_TimeOutException() { |
159 |
> |
public void testAwait3_TimeoutException() throws InterruptedException { |
160 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
161 |
< |
Thread t = new Thread(new Runnable() { |
162 |
< |
public void run() { |
163 |
< |
try { |
164 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
165 |
< |
threadShouldThrow(); |
166 |
< |
} catch (TimeoutException success) { |
167 |
< |
} catch (Exception b) { |
168 |
< |
threadUnexpectedException(); |
161 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
162 |
> |
public void realRun() throws Exception { |
163 |
> |
long startTime = System.nanoTime(); |
164 |
> |
try { |
165 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
166 |
> |
shouldThrow(); |
167 |
> |
} catch (TimeoutException success) {} |
168 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
169 |
> |
}}); |
170 |
|
|
171 |
< |
} |
217 |
< |
} |
218 |
< |
}); |
219 |
< |
try { |
220 |
< |
t.start(); |
221 |
< |
t.join(); |
222 |
< |
} catch (InterruptedException e) { |
223 |
< |
unexpectedException(); |
224 |
< |
} |
171 |
> |
awaitTermination(t); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* A timeout in one party causes others waiting in timed await to |
176 |
|
* throw BrokenBarrierException |
177 |
|
*/ |
178 |
< |
public void testAwait4_Timeout_BrokenBarrier() { |
178 |
> |
public void testAwait4_Timeout_BrokenBarrier() throws InterruptedException { |
179 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
180 |
< |
Thread t1 = new Thread(new Runnable() { |
181 |
< |
public void run() { |
182 |
< |
try { |
183 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
184 |
< |
threadShouldThrow(); |
185 |
< |
} catch (TimeoutException success) { |
186 |
< |
} catch (Exception b) { |
187 |
< |
threadUnexpectedException(); |
188 |
< |
} |
189 |
< |
} |
190 |
< |
}); |
191 |
< |
Thread t2 = new Thread(new Runnable() { |
192 |
< |
public void run() { |
193 |
< |
try { |
194 |
< |
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
195 |
< |
threadShouldThrow(); |
196 |
< |
} catch (BrokenBarrierException success) { |
197 |
< |
} catch (Exception i) { |
198 |
< |
threadUnexpectedException(); |
199 |
< |
} |
200 |
< |
} |
254 |
< |
}); |
255 |
< |
try { |
256 |
< |
t1.start(); |
257 |
< |
t2.start(); |
258 |
< |
t1.join(); |
259 |
< |
t2.join(); |
260 |
< |
} catch (InterruptedException e) { |
261 |
< |
unexpectedException(); |
262 |
< |
} |
180 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
181 |
> |
public void realRun() throws Exception { |
182 |
> |
try { |
183 |
> |
c.await(LONG_DELAY_MS, MILLISECONDS); |
184 |
> |
shouldThrow(); |
185 |
> |
} catch (BrokenBarrierException success) {} |
186 |
> |
}}); |
187 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
188 |
> |
public void realRun() throws Exception { |
189 |
> |
while (c.getNumberWaiting() == 0) |
190 |
> |
Thread.yield(); |
191 |
> |
long startTime = System.nanoTime(); |
192 |
> |
try { |
193 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
194 |
> |
shouldThrow(); |
195 |
> |
} catch (TimeoutException success) {} |
196 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
197 |
> |
}}); |
198 |
> |
|
199 |
> |
awaitTermination(t1); |
200 |
> |
awaitTermination(t2); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* A timeout in one party causes others waiting in await to |
205 |
|
* throw BrokenBarrierException |
206 |
|
*/ |
207 |
< |
public void testAwait5_Timeout_BrokenBarrier() { |
207 |
> |
public void testAwait5_Timeout_BrokenBarrier() throws InterruptedException { |
208 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
209 |
< |
Thread t1 = new Thread(new Runnable() { |
210 |
< |
public void run() { |
211 |
< |
try { |
212 |
< |
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
213 |
< |
threadShouldThrow(); |
214 |
< |
} catch (TimeoutException success) { |
215 |
< |
} catch (Exception b) { |
216 |
< |
threadUnexpectedException(); |
217 |
< |
} |
218 |
< |
} |
219 |
< |
}); |
220 |
< |
Thread t2 = new Thread(new Runnable() { |
221 |
< |
public void run() { |
222 |
< |
try { |
223 |
< |
c.await(); |
224 |
< |
threadShouldThrow(); |
225 |
< |
} catch (BrokenBarrierException success) { |
226 |
< |
} catch (Exception i) { |
227 |
< |
threadUnexpectedException(); |
228 |
< |
} |
229 |
< |
} |
292 |
< |
}); |
293 |
< |
try { |
294 |
< |
t1.start(); |
295 |
< |
t2.start(); |
296 |
< |
t1.join(); |
297 |
< |
t2.join(); |
298 |
< |
} catch (InterruptedException e) { |
299 |
< |
unexpectedException(); |
300 |
< |
} |
209 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
210 |
> |
public void realRun() throws Exception { |
211 |
> |
try { |
212 |
> |
c.await(); |
213 |
> |
shouldThrow(); |
214 |
> |
} catch (BrokenBarrierException success) {} |
215 |
> |
}}); |
216 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
217 |
> |
public void realRun() throws Exception { |
218 |
> |
while (c.getNumberWaiting() == 0) |
219 |
> |
Thread.yield(); |
220 |
> |
long startTime = System.nanoTime(); |
221 |
> |
try { |
222 |
> |
c.await(timeoutMillis(), MILLISECONDS); |
223 |
> |
shouldThrow(); |
224 |
> |
} catch (TimeoutException success) {} |
225 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
226 |
> |
}}); |
227 |
> |
|
228 |
> |
awaitTermination(t1); |
229 |
> |
awaitTermination(t2); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
|
* A reset of an active barrier causes waiting threads to throw |
234 |
|
* BrokenBarrierException |
235 |
|
*/ |
236 |
< |
public void testReset_BrokenBarrier() { |
236 |
> |
public void testReset_BrokenBarrier() throws InterruptedException { |
237 |
> |
Thread.interrupted(); // ensure current thread not interrupted |
238 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
239 |
< |
Thread t1 = new Thread(new Runnable() { |
240 |
< |
public void run() { |
241 |
< |
try { |
242 |
< |
c.await(); |
243 |
< |
threadShouldThrow(); |
244 |
< |
} catch (BrokenBarrierException success) {} |
245 |
< |
catch (Exception b) { |
246 |
< |
threadUnexpectedException(); |
247 |
< |
} |
248 |
< |
} |
249 |
< |
}); |
250 |
< |
Thread t2 = new Thread(new Runnable() { |
251 |
< |
public void run() { |
252 |
< |
try { |
253 |
< |
c.await(); |
254 |
< |
threadShouldThrow(); |
255 |
< |
} catch (BrokenBarrierException success) { |
256 |
< |
} 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 |
< |
} |
239 |
> |
final CountDownLatch pleaseReset = new CountDownLatch(2); |
240 |
> |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
241 |
> |
public void realRun() throws Exception { |
242 |
> |
pleaseReset.countDown(); |
243 |
> |
c.await(); |
244 |
> |
}}; |
245 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
246 |
> |
public void realRun() throws Exception { |
247 |
> |
pleaseReset.countDown(); |
248 |
> |
c.await(); |
249 |
> |
}}; |
250 |
> |
|
251 |
> |
t1.start(); |
252 |
> |
t2.start(); |
253 |
> |
await(pleaseReset); |
254 |
> |
c.reset(); |
255 |
> |
awaitTermination(t1); |
256 |
> |
awaitTermination(t2); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
|
* A reset before threads enter barrier does not throw |
261 |
|
* BrokenBarrierException |
262 |
|
*/ |
263 |
< |
public void testReset_NoBrokenBarrier() { |
263 |
> |
public void testReset_NoBrokenBarrier() throws Exception { |
264 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
265 |
< |
Thread t1 = new Thread(new Runnable() { |
266 |
< |
public void run() { |
267 |
< |
try { |
268 |
< |
c.await(); |
269 |
< |
} catch (Exception b) { |
270 |
< |
threadUnexpectedException(); |
271 |
< |
} |
272 |
< |
} |
273 |
< |
}); |
274 |
< |
Thread t2 = new Thread(new Runnable() { |
275 |
< |
public void run() { |
276 |
< |
try { |
277 |
< |
c.await(); |
278 |
< |
} catch (Exception i) { |
363 |
< |
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 |
< |
} |
265 |
> |
c.reset(); |
266 |
> |
|
267 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
268 |
> |
public void realRun() throws Exception { |
269 |
> |
c.await(); |
270 |
> |
}}); |
271 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
272 |
> |
public void realRun() throws Exception { |
273 |
> |
c.await(); |
274 |
> |
}}); |
275 |
> |
|
276 |
> |
c.await(); |
277 |
> |
awaitTermination(t1); |
278 |
> |
awaitTermination(t2); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
|
* All threads block while a barrier is broken. |
283 |
|
*/ |
284 |
< |
public void testReset_Leakage() { |
285 |
< |
try { |
286 |
< |
final CyclicBarrier c = new CyclicBarrier(2); |
287 |
< |
final AtomicBoolean done = new AtomicBoolean(); |
288 |
< |
Thread t = new Thread() { |
289 |
< |
public void run() { |
290 |
< |
while (!done.get()) { |
291 |
< |
try { |
292 |
< |
while (c.isBroken()) |
293 |
< |
c.reset(); |
294 |
< |
|
295 |
< |
c.await(); |
394 |
< |
threadFail("await should not return"); |
395 |
< |
} |
396 |
< |
catch (BrokenBarrierException e) { |
397 |
< |
} |
398 |
< |
catch (InterruptedException ie) { |
399 |
< |
} |
400 |
< |
} |
284 |
> |
public void testReset_Leakage() throws InterruptedException { |
285 |
> |
final CyclicBarrier c = new CyclicBarrier(2); |
286 |
> |
final AtomicBoolean done = new AtomicBoolean(); |
287 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
288 |
> |
public void realRun() { |
289 |
> |
while (!done.get()) { |
290 |
> |
try { |
291 |
> |
while (c.isBroken()) |
292 |
> |
c.reset(); |
293 |
> |
|
294 |
> |
c.await(); |
295 |
> |
shouldThrow(); |
296 |
|
} |
297 |
< |
}; |
297 |
> |
catch (BrokenBarrierException ok) {} |
298 |
> |
catch (InterruptedException ok) {} |
299 |
> |
}}}); |
300 |
|
|
301 |
< |
t.start(); |
302 |
< |
for ( int i = 0; i < 4; i++) { |
406 |
< |
Thread.sleep(SHORT_DELAY_MS); |
407 |
< |
t.interrupt(); |
408 |
< |
} |
409 |
< |
done.set(true); |
301 |
> |
for (int i = 0; i < 4; i++) { |
302 |
> |
delay(timeoutMillis()); |
303 |
|
t.interrupt(); |
304 |
|
} |
305 |
< |
catch (Exception ex) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
305 |
> |
done.set(true); |
306 |
> |
t.interrupt(); |
307 |
> |
awaitTermination(t); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* Reset of a non-broken barrier does not break barrier |
312 |
|
*/ |
313 |
< |
public void testResetWithoutBreakage() { |
314 |
< |
try { |
313 |
> |
public void testResetWithoutBreakage() throws Exception { |
314 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
315 |
> |
for (int i = 0; i < 3; i++) { |
316 |
|
final CyclicBarrier start = new CyclicBarrier(3); |
317 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
318 |
< |
for (int i = 0; i < 3; i++) { |
319 |
< |
Thread t1 = new Thread(new Runnable() { |
320 |
< |
public void run() { |
321 |
< |
try { start.await(); } |
322 |
< |
catch (Exception ie) { |
323 |
< |
threadFail("start barrier"); |
324 |
< |
} |
325 |
< |
try { barrier.await(); } |
326 |
< |
catch (Throwable thrown) { |
327 |
< |
unexpectedException(); |
328 |
< |
}}}); |
329 |
< |
|
330 |
< |
Thread t2 = new Thread(new Runnable() { |
331 |
< |
public void run() { |
332 |
< |
try { start.await(); } |
333 |
< |
catch (Exception ie) { |
334 |
< |
threadFail("start barrier"); |
335 |
< |
} |
336 |
< |
try { barrier.await(); } |
337 |
< |
catch (Throwable thrown) { |
444 |
< |
unexpectedException(); |
445 |
< |
}}}); |
446 |
< |
|
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(); |
317 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
318 |
> |
public void realRun() throws Exception { |
319 |
> |
start.await(); |
320 |
> |
barrier.await(); |
321 |
> |
}}); |
322 |
> |
|
323 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
324 |
> |
public void realRun() throws Exception { |
325 |
> |
start.await(); |
326 |
> |
barrier.await(); |
327 |
> |
}}); |
328 |
> |
|
329 |
> |
start.await(); |
330 |
> |
barrier.await(); |
331 |
> |
awaitTermination(t1); |
332 |
> |
awaitTermination(t2); |
333 |
> |
assertFalse(barrier.isBroken()); |
334 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
335 |
> |
if (i == 1) barrier.reset(); |
336 |
> |
assertFalse(barrier.isBroken()); |
337 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
338 |
|
} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
|
* Reset of a barrier after interruption reinitializes it. |
343 |
|
*/ |
344 |
< |
public void testResetAfterInterrupt() { |
345 |
< |
try { |
344 |
> |
public void testResetAfterInterrupt() throws Exception { |
345 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
346 |
> |
for (int i = 0; i < 2; i++) { |
347 |
|
final CyclicBarrier start = new CyclicBarrier(3); |
348 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
349 |
< |
for (int i = 0; i < 2; i++) { |
350 |
< |
Thread t1 = new Thread(new Runnable() { |
351 |
< |
public void run() { |
352 |
< |
try { start.await(); } |
353 |
< |
catch (Exception ie) { |
354 |
< |
threadFail("start barrier"); |
355 |
< |
} |
356 |
< |
try { barrier.await(); } |
357 |
< |
catch (InterruptedException ok) {} |
358 |
< |
catch (Throwable thrown) { |
359 |
< |
unexpectedException(); |
360 |
< |
}}}); |
361 |
< |
|
362 |
< |
Thread t2 = new Thread(new Runnable() { |
363 |
< |
public void run() { |
364 |
< |
try { start.await(); } |
365 |
< |
catch (Exception ie) { |
366 |
< |
threadFail("start barrier"); |
367 |
< |
} |
368 |
< |
try { barrier.await(); } |
369 |
< |
catch (BrokenBarrierException ok) {} |
370 |
< |
catch (Throwable thrown) { |
496 |
< |
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(); |
348 |
> |
Thread t1 = new ThreadShouldThrow(InterruptedException.class) { |
349 |
> |
public void realRun() throws Exception { |
350 |
> |
start.await(); |
351 |
> |
barrier.await(); |
352 |
> |
}}; |
353 |
> |
|
354 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
355 |
> |
public void realRun() throws Exception { |
356 |
> |
start.await(); |
357 |
> |
barrier.await(); |
358 |
> |
}}; |
359 |
> |
|
360 |
> |
t1.start(); |
361 |
> |
t2.start(); |
362 |
> |
start.await(); |
363 |
> |
t1.interrupt(); |
364 |
> |
awaitTermination(t1); |
365 |
> |
awaitTermination(t2); |
366 |
> |
assertTrue(barrier.isBroken()); |
367 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
368 |
> |
barrier.reset(); |
369 |
> |
assertFalse(barrier.isBroken()); |
370 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
371 |
|
} |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
|
* Reset of a barrier after timeout reinitializes it. |
376 |
|
*/ |
377 |
< |
public void testResetAfterTimeout() { |
378 |
< |
try { |
379 |
< |
final CyclicBarrier start = new CyclicBarrier(3); |
380 |
< |
final CyclicBarrier barrier = new CyclicBarrier(3); |
381 |
< |
for (int i = 0; i < 2; i++) { |
382 |
< |
Thread t1 = new Thread(new Runnable() { |
383 |
< |
public void run() { |
384 |
< |
try { start.await(); } |
385 |
< |
catch (Exception ie) { |
386 |
< |
threadFail("start barrier"); |
387 |
< |
} |
388 |
< |
try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } |
389 |
< |
catch (TimeoutException ok) {} |
390 |
< |
catch (Throwable thrown) { |
391 |
< |
unexpectedException(); |
392 |
< |
}}}); |
393 |
< |
|
394 |
< |
Thread t2 = new Thread(new Runnable() { |
395 |
< |
public void run() { |
396 |
< |
try { start.await(); } |
397 |
< |
catch (Exception ie) { |
398 |
< |
threadFail("start barrier"); |
399 |
< |
} |
400 |
< |
try { barrier.await(); } |
401 |
< |
catch (BrokenBarrierException ok) {} |
402 |
< |
catch (Throwable thrown) { |
403 |
< |
unexpectedException(); |
404 |
< |
}}}); |
405 |
< |
|
406 |
< |
t1.start(); |
407 |
< |
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(); |
377 |
> |
public void testResetAfterTimeout() throws Exception { |
378 |
> |
final CyclicBarrier barrier = new CyclicBarrier(3); |
379 |
> |
for (int i = 0; i < 2; i++) { |
380 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
381 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
382 |
> |
public void realRun() throws Exception { |
383 |
> |
try { |
384 |
> |
barrier.await(); |
385 |
> |
shouldThrow(); |
386 |
> |
} catch (BrokenBarrierException success) {} |
387 |
> |
}}); |
388 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws Exception { |
390 |
> |
while (barrier.getNumberWaiting() == 0) |
391 |
> |
Thread.yield(); |
392 |
> |
long startTime = System.nanoTime(); |
393 |
> |
try { |
394 |
> |
barrier.await(timeoutMillis(), MILLISECONDS); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (TimeoutException success) {} |
397 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
398 |
> |
}}); |
399 |
> |
|
400 |
> |
awaitTermination(t1); |
401 |
> |
awaitTermination(t2); |
402 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
403 |
> |
assertTrue(barrier.isBroken()); |
404 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
405 |
> |
barrier.reset(); |
406 |
> |
assertFalse(barrier.isBroken()); |
407 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
408 |
|
} |
409 |
|
} |
410 |
|
|
568 |
– |
|
411 |
|
/** |
412 |
|
* Reset of a barrier after a failed command reinitializes it. |
413 |
|
*/ |
414 |
< |
public void testResetAfterCommandException() { |
415 |
< |
try { |
414 |
> |
public void testResetAfterCommandException() throws Exception { |
415 |
> |
final CyclicBarrier barrier = |
416 |
> |
new CyclicBarrier(3, new Runnable() { |
417 |
> |
public void run() { |
418 |
> |
throw new NullPointerException(); }}); |
419 |
> |
for (int i = 0; i < 2; i++) { |
420 |
|
final CyclicBarrier start = new CyclicBarrier(3); |
421 |
< |
final CyclicBarrier barrier = |
422 |
< |
new CyclicBarrier(3, new Runnable() { |
423 |
< |
public void run() { |
424 |
< |
throw new NullPointerException(); }}); |
425 |
< |
for (int i = 0; i < 2; i++) { |
426 |
< |
Thread t1 = new Thread(new Runnable() { |
427 |
< |
public void run() { |
428 |
< |
try { start.await(); } |
429 |
< |
catch (Exception ie) { |
430 |
< |
threadFail("start barrier"); |
431 |
< |
} |
432 |
< |
try { barrier.await(); } |
433 |
< |
catch (BrokenBarrierException ok) {} |
434 |
< |
catch (Throwable thrown) { |
435 |
< |
unexpectedException(); |
436 |
< |
}}}); |
437 |
< |
|
438 |
< |
Thread t2 = new Thread(new Runnable() { |
439 |
< |
public void run() { |
440 |
< |
try { start.await(); } |
441 |
< |
catch (Exception ie) { |
442 |
< |
threadFail("start barrier"); |
443 |
< |
} |
444 |
< |
try { barrier.await(); } |
445 |
< |
catch (BrokenBarrierException ok) {} |
446 |
< |
catch (Throwable thrown) { |
447 |
< |
unexpectedException(); |
602 |
< |
}}}); |
603 |
< |
|
604 |
< |
t1.start(); |
605 |
< |
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(); |
421 |
> |
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) { |
422 |
> |
public void realRun() throws Exception { |
423 |
> |
start.await(); |
424 |
> |
barrier.await(); |
425 |
> |
}}; |
426 |
> |
|
427 |
> |
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) { |
428 |
> |
public void realRun() throws Exception { |
429 |
> |
start.await(); |
430 |
> |
barrier.await(); |
431 |
> |
}}; |
432 |
> |
|
433 |
> |
t1.start(); |
434 |
> |
t2.start(); |
435 |
> |
start.await(); |
436 |
> |
while (barrier.getNumberWaiting() < 2) { Thread.yield(); } |
437 |
> |
try { |
438 |
> |
barrier.await(); |
439 |
> |
shouldThrow(); |
440 |
> |
} catch (NullPointerException success) {} |
441 |
> |
awaitTermination(t1); |
442 |
> |
awaitTermination(t2); |
443 |
> |
assertTrue(barrier.isBroken()); |
444 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
445 |
> |
barrier.reset(); |
446 |
> |
assertFalse(barrier.isBroken()); |
447 |
> |
assertEquals(0, barrier.getNumberWaiting()); |
448 |
|
} |
449 |
|
} |
450 |
|
} |