ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.7
Committed: Fri Jan 9 14:45:58 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.6: +2 -2 lines
Log Message:
Added tests

File Contents

# Content
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
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
13 public class CyclicBarrierTest extends JSR166TestCase{
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17 public static Test suite() {
18 return new TestSuite(CyclicBarrierTest.class);
19 }
20
21 private volatile int countAction;
22 private class MyAction implements Runnable {
23 public void run() { ++countAction; }
24 }
25
26 /**
27 * Creating with negative parties throws IAE
28 */
29 public void testConstructor1() {
30 try {
31 new CyclicBarrier(-1, (Runnable)null);
32 shouldThrow();
33 } catch(IllegalArgumentException e){}
34 }
35
36 /**
37 * Creating with negative parties and no action throws IAE
38 */
39 public void testConstructor2() {
40 try {
41 new CyclicBarrier(-1);
42 shouldThrow();
43 } catch(IllegalArgumentException e){}
44 }
45
46 /**
47 * getParties returns the number of parties given in constructor
48 */
49 public void testGetParties() {
50 CyclicBarrier b = new CyclicBarrier(2);
51 assertEquals(2, b.getParties());
52 assertEquals(0, b.getNumberWaiting());
53 }
54
55 /**
56 * A 1-party barrier triggers after single await
57 */
58 public void testSingleParty() {
59 try {
60 CyclicBarrier b = new CyclicBarrier(1);
61 assertEquals(1, b.getParties());
62 assertEquals(0, b.getNumberWaiting());
63 b.await();
64 b.await();
65 assertEquals(0, b.getNumberWaiting());
66 }
67 catch(Exception e) {
68 unexpectedException();
69 }
70 }
71
72 /**
73 * The supplied barrier action is run at barrier
74 */
75 public void testBarrierAction() {
76 try {
77 countAction = 0;
78 CyclicBarrier b = new CyclicBarrier(1, new MyAction());
79 assertEquals(1, b.getParties());
80 assertEquals(0, b.getNumberWaiting());
81 b.await();
82 b.await();
83 assertEquals(0, b.getNumberWaiting());
84 assertEquals(countAction, 2);
85 }
86 catch(Exception e) {
87 unexpectedException();
88 }
89 }
90
91 /**
92 * A 2-party/thread barrier triggers after both threads invoke await
93 */
94 public void testTwoParties() {
95 final CyclicBarrier b = new CyclicBarrier(2);
96 Thread t = new Thread(new Runnable() {
97 public void run() {
98 try {
99 b.await();
100 b.await();
101 b.await();
102 b.await();
103 } catch(Exception e){
104 threadUnexpectedException();
105 }}});
106
107 try {
108 t.start();
109 b.await();
110 b.await();
111 b.await();
112 b.await();
113 t.join();
114 } catch(Exception e){
115 unexpectedException();
116 }
117 }
118
119
120 /**
121 * An interruption in one party causes others waiting in await to
122 * throw BrokenBarrierException
123 */
124 public void testAwait1_Interrupted_BrokenBarrier() {
125 final CyclicBarrier c = new CyclicBarrier(3);
126 Thread t1 = new Thread(new Runnable() {
127 public void run() {
128 try {
129 c.await();
130 threadShouldThrow();
131 } catch(InterruptedException success){}
132 catch(Exception b){
133 threadUnexpectedException();
134 }
135 }
136 });
137 Thread t2 = new Thread(new Runnable() {
138 public void run() {
139 try {
140 c.await();
141 threadShouldThrow();
142 } catch(BrokenBarrierException success){
143 } catch(Exception i){
144 threadUnexpectedException();
145 }
146 }
147 });
148 try {
149 t1.start();
150 t2.start();
151 Thread.sleep(SHORT_DELAY_MS);
152 t1.interrupt();
153 t1.join();
154 t2.join();
155 } catch(InterruptedException e){
156 unexpectedException();
157 }
158 }
159
160 /**
161 * An interruption in one party causes others waiting in timed await to
162 * throw BrokenBarrierException
163 */
164 public void testAwait2_Interrupted_BrokenBarrier() {
165 final CyclicBarrier c = new CyclicBarrier(3);
166 Thread t1 = new Thread(new Runnable() {
167 public void run() {
168 try {
169 c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
170 threadShouldThrow();
171 } catch(InterruptedException success){
172 } catch(Exception b){
173 threadUnexpectedException();
174 }
175 }
176 });
177 Thread t2 = new Thread(new Runnable() {
178 public void run() {
179 try {
180 c.await(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
181 threadShouldThrow();
182 } catch(BrokenBarrierException success){
183 } catch(Exception i){
184 threadUnexpectedException();
185 }
186 }
187 });
188 try {
189 t1.start();
190 t2.start();
191 Thread.sleep(SHORT_DELAY_MS);
192 t1.interrupt();
193 t1.join();
194 t2.join();
195 } catch(InterruptedException e){
196 unexpectedException();
197 }
198 }
199
200 /**
201 * A timeout in timed await throws TimeoutException
202 */
203 public void testAwait3_TimeOutException() {
204 final CyclicBarrier c = new CyclicBarrier(2);
205 Thread t = new Thread(new Runnable() {
206 public void run() {
207 try {
208 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
209 threadShouldThrow();
210 } catch(TimeoutException success){
211 } catch(Exception b){
212 threadUnexpectedException();
213
214 }
215 }
216 });
217 try {
218 t.start();
219 t.join();
220 } catch(InterruptedException e){
221 unexpectedException();
222 }
223 }
224
225 /**
226 * A timeout in one party causes others waiting in timed await to
227 * throw BrokenBarrierException
228 */
229 public void testAwait4_Timeout_BrokenBarrier() {
230 final CyclicBarrier c = new CyclicBarrier(3);
231 Thread t1 = new Thread(new Runnable() {
232 public void run() {
233 try {
234 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
235 threadShouldThrow();
236 } catch(TimeoutException success){
237 } catch(Exception b){
238 threadUnexpectedException();
239 }
240 }
241 });
242 Thread t2 = new Thread(new Runnable() {
243 public void run() {
244 try {
245 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
246 threadShouldThrow();
247 } catch(BrokenBarrierException success){
248 } catch(Exception i){
249 threadUnexpectedException();
250 }
251 }
252 });
253 try {
254 t1.start();
255 t2.start();
256 t1.join();
257 t2.join();
258 } catch(InterruptedException e){
259 unexpectedException();
260 }
261 }
262
263 /**
264 * A timeout in one party causes others waiting in await to
265 * throw BrokenBarrierException
266 */
267 public void testAwait5_Timeout_BrokenBarrier() {
268 final CyclicBarrier c = new CyclicBarrier(3);
269 Thread t1 = new Thread(new Runnable() {
270 public void run() {
271 try {
272 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
273 threadShouldThrow();
274 } catch(TimeoutException success){
275 } catch(Exception b){
276 threadUnexpectedException();
277 }
278 }
279 });
280 Thread t2 = new Thread(new Runnable() {
281 public void run() {
282 try {
283 c.await();
284 threadShouldThrow();
285 } catch(BrokenBarrierException success){
286 } catch(Exception i){
287 threadUnexpectedException();
288 }
289 }
290 });
291 try {
292 t1.start();
293 t2.start();
294 t1.join();
295 t2.join();
296 } catch(InterruptedException e){
297 unexpectedException();
298 }
299 }
300
301 /**
302 * A reset of an active barrier causes waiting threads to throw
303 * BrokenBarrierException
304 */
305 public void testReset_BrokenBarrier() {
306 final CyclicBarrier c = new CyclicBarrier(3);
307 Thread t1 = new Thread(new Runnable() {
308 public void run() {
309 try {
310 c.await();
311 threadShouldThrow();
312 } catch(BrokenBarrierException success){}
313 catch(Exception b){
314 threadUnexpectedException();
315 }
316 }
317 });
318 Thread t2 = new Thread(new Runnable() {
319 public void run() {
320 try {
321 c.await();
322 threadShouldThrow();
323 } catch(BrokenBarrierException success){
324 } catch(Exception i){
325 threadUnexpectedException();
326 }
327 }
328 });
329 try {
330 t1.start();
331 t2.start();
332 Thread.sleep(SHORT_DELAY_MS);
333 c.reset();
334 t1.join();
335 t2.join();
336 } catch(InterruptedException e){
337 unexpectedException();
338 }
339 }
340
341 /**
342 * A reset before threads enter barrier does not throw
343 * BrokenBarrierException
344 */
345 public void testReset_NoBrokenBarrier() {
346 final CyclicBarrier c = new CyclicBarrier(3);
347 Thread t1 = new Thread(new Runnable() {
348 public void run() {
349 try {
350 c.await();
351 } catch(Exception b){
352 threadUnexpectedException();
353 }
354 }
355 });
356 Thread t2 = new Thread(new Runnable() {
357 public void run() {
358 try {
359 c.await();
360 } catch(Exception i){
361 threadUnexpectedException();
362 }
363 }
364 });
365 try {
366 c.reset();
367 t1.start();
368 t2.start();
369 c.await();
370 t1.join();
371 t2.join();
372 } catch(Exception e){
373 unexpectedException();
374 }
375 }
376
377 }