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