ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CyclicBarrierTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.4 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 9 | Line 9 | import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11  
12 < public class CyclicBarrierTest extends TestCase{
13 <    
12 > public class CyclicBarrierTest extends JSR166TestCase{
13      public static void main(String[] args) {
14          junit.textui.TestRunner.run (suite());  
15      }
17    
18    
16      public static Test suite() {
17          return new TestSuite(CyclicBarrierTest.class);
18      }
22    
23    private static long SHORT_DELAY_MS = 100;
24    private static long MEDIUM_DELAY_MS = 1000;
25    private static long LONG_DELAY_MS = 10000;
19  
20 <    public void testConstructor1(){
21 <        try{
20 >    private volatile int countAction;
21 >    private class MyAction implements Runnable {
22 >        public void run() { ++countAction; }
23 >    }
24 >    
25 >    /**
26 >     * Creating with negative parties throws IAE
27 >     */
28 >    public void testConstructor1() {
29 >        try {
30              new CyclicBarrier(-1, (Runnable)null);
31 <            fail("should throw");
31 >            shouldThrow();
32          } catch(IllegalArgumentException e){}
33      }
34  
35 <    public void testConstructor2(){
36 <        try{
35 >    /**
36 >     * Creating with negative parties and no action throws IAE
37 >     */
38 >    public void testConstructor2() {
39 >        try {
40              new CyclicBarrier(-1);
41 <            fail("should throw");
41 >            shouldThrow();
42          } catch(IllegalArgumentException e){}
43      }
44  
45 <    public void testConstructor3(){
45 >    /**
46 >     * getParties returns the number of parties given in constructor
47 >     */
48 >    public void testGetParties() {
49          CyclicBarrier b = new CyclicBarrier(2);
50          assertEquals(2, b.getParties());
51          assertEquals(0, b.getNumberWaiting());
52      }
53  
54 +    /**
55 +     * A 1-party barrier triggers properly
56 +     */
57      public void testSingleParty() {
58          try {
59              CyclicBarrier b = new CyclicBarrier(1);
# Line 54 | Line 64 | public class CyclicBarrierTest extends T
64              assertEquals(0, b.getNumberWaiting());
65          }
66          catch(Exception e) {
67 <            fail("unexpected exception");
67 >            unexpectedException();
68          }
69      }
70      
71 <    private volatile int countAction;
72 <    private class MyAction implements Runnable {
73 <        public void run() { ++countAction; }
64 <    }
65 <
71 >    /**
72 >     * The supplied barrier action is run at barrier
73 >     */
74      public void testBarrierAction() {
75          try {
76              countAction = 0;
# Line 75 | Line 83 | public class CyclicBarrierTest extends T
83              assertEquals(countAction, 2);
84          }
85          catch(Exception e) {
86 <            fail("unexpected exception");
86 >            unexpectedException();
87          }
88      }
89  
90 <
91 <    public void testTwoParties(){
90 >    /**
91 >     * A 2-party/thread barrier triggers properly
92 >     */
93 >    public void testTwoParties() {
94          final CyclicBarrier b = new CyclicBarrier(2);
95          Thread t = new Thread(new Runnable() {
96 <                public void run(){
96 >                public void run() {
97                      try {
98                          b.await();
99                          b.await();
100                          b.await();
101                          b.await();
102                      } catch(Exception e){
103 <                        fail("unexpected exception");
103 >                        threadUnexpectedException();
104                      }}});
105  
106          try {
# Line 101 | Line 111 | public class CyclicBarrierTest extends T
111              b.await();
112              t.join();
113          } catch(Exception e){
114 <            fail("unexpected exception");
114 >            unexpectedException();
115          }
116      }
117  
118  
119 <    public void testAwait1_Interrupted_BrokenBarrier(){
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{
126 >                public void run() {
127 >                    try {
128                          c.await();
129 <                        fail("should throw");
129 >                        threadShouldThrow();
130                      } catch(InterruptedException success){}                
131                      catch(Exception b){
132 <                        fail("should throw IE");
132 >                        threadUnexpectedException();
133                      }
134                  }
135              });
136 <        Thread t2 = new Thread(new Runnable(){
137 <                public void run(){
138 <                    try{
136 >        Thread t2 = new Thread(new Runnable() {
137 >                public void run() {
138 >                    try {
139                          c.await();
140 <                        fail("should throw");                        
140 >                        threadShouldThrow();                        
141                      } catch(BrokenBarrierException success){
142                      } catch(Exception i){
143 <                        fail("should throw BBE");
143 >                        threadUnexpectedException();
144                      }
145                  }
146              });
# Line 138 | Line 152 | public class CyclicBarrierTest extends T
152              t1.join();
153              t2.join();
154          } catch(InterruptedException e){
155 <            fail("unexpected exception");
155 >            unexpectedException();
156          }
157      }
158  
159 <    public void testAwait2_Interrupted_BrokenBarrier(){
159 >    /**
160 >     * An interruption in one party causes others waiting in timed await to
161 >     * throw BrokenBarrierException
162 >     */
163 >    public void testAwait2_Interrupted_BrokenBarrier() {
164        final CyclicBarrier c = new CyclicBarrier(3);
165          Thread t1 = new Thread(new Runnable() {
166 <                public void run(){
167 <                    try{
166 >                public void run() {
167 >                    try {
168                          c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
169 <                        fail("should throw");
169 >                        threadShouldThrow();
170                      } catch(InterruptedException success){
171                      } catch(Exception b){
172 <                        fail("should throw IE");
172 >                        threadUnexpectedException();
173                      }
174                  }
175              });
176 <        Thread t2 = new Thread(new Runnable(){
177 <                public void run(){
178 <                    try{
176 >        Thread t2 = new Thread(new Runnable() {
177 >                public void run() {
178 >                    try {
179                          c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
180 <                        fail("should throw");                        
180 >                        threadShouldThrow();                        
181                      } catch(BrokenBarrierException success){
182                      } catch(Exception i){
183 <                        fail("should throw BBE");
183 >                        threadUnexpectedException();
184                      }
185                  }
186              });
# Line 174 | Line 192 | public class CyclicBarrierTest extends T
192              t1.join();
193              t2.join();
194          } catch(InterruptedException e){
195 <            fail("unexpected exception");
195 >            unexpectedException();
196          }
197      }
198      
199 <    public void testAwait3_TimeOutException(){
199 >    /**
200 >     * A timeout in timed await throws TimeoutException
201 >     */
202 >    public void testAwait3_TimeOutException() {
203          final CyclicBarrier c = new CyclicBarrier(2);
204          Thread t = new Thread(new Runnable() {
205 <                public void run(){
206 <                    try{
205 >                public void run() {
206 >                    try {
207                          c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
208 <                        fail("should throw");
208 >                        threadShouldThrow();
209                      } catch(TimeoutException success){
210                      } catch(Exception b){
211 <                        fail("should throw TOE");
211 >                        threadUnexpectedException();
212                          
213                      }
214                  }
# Line 196 | Line 217 | public class CyclicBarrierTest extends T
217              t.start();
218              t.join();
219          } catch(InterruptedException e){
220 <            fail("unexpected exception");
220 >            unexpectedException();
221          }
222      }
223      
224 +    /**
225 +     * A reset of an active barrier causes waiting threads to throw
226 +     * BrokenBarrierException
227 +     */
228 +    public void testReset_BrokenBarrier() {
229 +        final CyclicBarrier c = new CyclicBarrier(3);
230 +        Thread t1 = new Thread(new Runnable() {
231 +                public void run() {
232 +                    try {
233 +                        c.await();
234 +                        threadShouldThrow();
235 +                    } catch(BrokenBarrierException success){}                
236 +                    catch(Exception b){
237 +                        threadUnexpectedException();
238 +                    }
239 +                }
240 +            });
241 +        Thread t2 = new Thread(new Runnable() {
242 +                public void run() {
243 +                    try {
244 +                        c.await();
245 +                        threadShouldThrow();                        
246 +                    } catch(BrokenBarrierException success){
247 +                    } catch(Exception i){
248 +                        threadUnexpectedException();
249 +                    }
250 +                }
251 +            });
252 +        try {
253 +            t1.start();
254 +            t2.start();
255 +            Thread.sleep(SHORT_DELAY_MS);
256 +            c.reset();
257 +            t1.join();
258 +            t2.join();
259 +        } catch(InterruptedException e){
260 +            unexpectedException();
261 +        }
262 +    }
263 +
264 +    /**
265 +     * A reset before threads enter barrier does not throw
266 +     * BrokenBarrierException
267 +     */
268 +    public void testReset_NoBrokenBarrier() {
269 +        final CyclicBarrier c = new CyclicBarrier(3);
270 +        Thread t1 = new Thread(new Runnable() {
271 +                public void run() {
272 +                    try {
273 +                        c.await();
274 +                    } catch(Exception b){
275 +                        threadUnexpectedException();
276 +                    }
277 +                }
278 +            });
279 +        Thread t2 = new Thread(new Runnable() {
280 +                public void run() {
281 +                    try {
282 +                        c.await();
283 +                    } catch(Exception i){
284 +                        threadUnexpectedException();
285 +                    }
286 +                }
287 +            });
288 +        try {
289 +            c.reset();
290 +            t1.start();
291 +            t2.start();
292 +            c.await();
293 +            t1.join();
294 +            t2.join();
295 +        } catch(Exception e){
296 +            unexpectedException();
297 +        }
298 +    }
299 +
300   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines