ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
Revision: 1.3
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +68 -44 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11    
12 dl 1.2 public class CyclicBarrierTest extends JSR166TestCase{
13 dl 1.1 public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(CyclicBarrierTest.class);
18     }
19 dl 1.3
20     private volatile int countAction;
21     private class MyAction implements Runnable {
22     public void run() { ++countAction; }
23     }
24 dl 1.1
25 dl 1.3 /**
26     *
27     */
28     public void testConstructor1() {
29     try {
30 dl 1.1 new CyclicBarrier(-1, (Runnable)null);
31 dl 1.3 shouldThrow();
32 dl 1.1 } catch(IllegalArgumentException e){}
33     }
34    
35 dl 1.3 /**
36     *
37     */
38     public void testConstructor2() {
39     try {
40 dl 1.1 new CyclicBarrier(-1);
41 dl 1.3 shouldThrow();
42 dl 1.1 } catch(IllegalArgumentException e){}
43     }
44    
45 dl 1.3 /**
46     *
47     */
48     public void testConstructor3() {
49 dl 1.1 CyclicBarrier b = new CyclicBarrier(2);
50     assertEquals(2, b.getParties());
51     assertEquals(0, b.getNumberWaiting());
52     }
53    
54 dl 1.3 /**
55     *
56     */
57 dl 1.1 public void testSingleParty() {
58     try {
59     CyclicBarrier b = new CyclicBarrier(1);
60     assertEquals(1, b.getParties());
61     assertEquals(0, b.getNumberWaiting());
62     b.await();
63     b.await();
64     assertEquals(0, b.getNumberWaiting());
65     }
66     catch(Exception e) {
67 dl 1.3 unexpectedException();
68 dl 1.1 }
69     }
70    
71 dl 1.3 /**
72     *
73     */
74 dl 1.1 public void testBarrierAction() {
75     try {
76     countAction = 0;
77     CyclicBarrier b = new CyclicBarrier(1, new MyAction());
78     assertEquals(1, b.getParties());
79     assertEquals(0, b.getNumberWaiting());
80     b.await();
81     b.await();
82     assertEquals(0, b.getNumberWaiting());
83     assertEquals(countAction, 2);
84     }
85     catch(Exception e) {
86 dl 1.3 unexpectedException();
87 dl 1.1 }
88     }
89    
90    
91 dl 1.3 /**
92     *
93     */
94     public void testTwoParties() {
95 dl 1.1 final CyclicBarrier b = new CyclicBarrier(2);
96     Thread t = new Thread(new Runnable() {
97 dl 1.3 public void run() {
98 dl 1.1 try {
99     b.await();
100     b.await();
101     b.await();
102     b.await();
103     } catch(Exception e){
104 dl 1.3 threadUnexpectedException();
105 dl 1.1 }}});
106    
107     try {
108     t.start();
109     b.await();
110     b.await();
111     b.await();
112     b.await();
113     t.join();
114     } catch(Exception e){
115 dl 1.3 unexpectedException();
116 dl 1.1 }
117     }
118    
119    
120 dl 1.3 /**
121     *
122     */
123     public void testAwait1_Interrupted_BrokenBarrier() {
124 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
125     Thread t1 = new Thread(new Runnable() {
126 dl 1.3 public void run() {
127     try {
128 dl 1.1 c.await();
129 dl 1.3 threadShouldThrow();
130 dl 1.1 } catch(InterruptedException success){}
131     catch(Exception b){
132 dl 1.2 threadFail("should throw IE");
133 dl 1.1 }
134     }
135     });
136 dl 1.3 Thread t2 = new Thread(new Runnable() {
137     public void run() {
138     try {
139 dl 1.1 c.await();
140 dl 1.3 threadShouldThrow();
141 dl 1.1 } catch(BrokenBarrierException success){
142     } catch(Exception i){
143 dl 1.2 threadFail("should throw BBE");
144 dl 1.1 }
145     }
146     });
147     try {
148     t1.start();
149     t2.start();
150     Thread.sleep(SHORT_DELAY_MS);
151     t1.interrupt();
152     t1.join();
153     t2.join();
154     } catch(InterruptedException e){
155 dl 1.3 unexpectedException();
156 dl 1.1 }
157     }
158    
159 dl 1.3 /**
160     *
161     */
162     public void testAwait2_Interrupted_BrokenBarrier() {
163 dl 1.1 final CyclicBarrier c = new CyclicBarrier(3);
164     Thread t1 = new Thread(new Runnable() {
165 dl 1.3 public void run() {
166     try {
167 dl 1.1 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
168 dl 1.3 threadShouldThrow();
169 dl 1.1 } catch(InterruptedException success){
170     } catch(Exception b){
171 dl 1.2 threadFail("should throw IE");
172 dl 1.1 }
173     }
174     });
175 dl 1.3 Thread t2 = new Thread(new Runnable() {
176     public void run() {
177     try {
178 dl 1.1 c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
179 dl 1.3 threadShouldThrow();
180 dl 1.1 } catch(BrokenBarrierException success){
181     } catch(Exception i){
182 dl 1.2 threadFail("should throw BBE");
183 dl 1.1 }
184     }
185     });
186     try {
187     t1.start();
188     t2.start();
189     Thread.sleep(SHORT_DELAY_MS);
190     t1.interrupt();
191     t1.join();
192     t2.join();
193     } catch(InterruptedException e){
194 dl 1.3 unexpectedException();
195 dl 1.1 }
196     }
197    
198 dl 1.3 /**
199     *
200     */
201     public void testAwait3_TimeOutException() {
202 dl 1.1 final CyclicBarrier c = new CyclicBarrier(2);
203     Thread t = new Thread(new Runnable() {
204 dl 1.3 public void run() {
205     try {
206 dl 1.1 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
207 dl 1.3 threadShouldThrow();
208 dl 1.1 } catch(TimeoutException success){
209     } catch(Exception b){
210 dl 1.2 threadFail("should throw TOE");
211 dl 1.1
212     }
213     }
214     });
215     try {
216     t.start();
217     t.join();
218     } catch(InterruptedException e){
219 dl 1.3 unexpectedException();
220 dl 1.1 }
221     }
222    
223     }