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

Comparing jsr166/src/test/tck/CountDownLatchTest.java (file contents):
Revision 1.2 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.12 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 1 | Line 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.
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 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13  
14   public class CountDownLatchTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run (suite());
17      }
18      public static Test suite() {
19 <        return new TestSuite(CountDownLatchTest.class);
19 >        return new TestSuite(CountDownLatchTest.class);
20      }
21  
22 <    public void testConstructor(){
22 >    /**
23 >     * negative constructor argument throws IAE
24 >     */
25 >    public void testConstructor() {
26          try {
27              new CountDownLatch(-1);
28 <            fail("should throw IllegalArgumentException");
29 <        } catch(IllegalArgumentException success){}
28 >            shouldThrow();
29 >        } catch (IllegalArgumentException success) {}
30      }
31  
32 <    public void testGetCount(){
33 <        final CountDownLatch l = new CountDownLatch(2);
34 <        assertEquals(2, l.getCount());
35 <        l.countDown();
36 <        assertEquals(1, l.getCount());
37 <    }
38 <
39 <    public void testAwait(){
40 <        final CountDownLatch l = new CountDownLatch(2);
41 <
42 <        Thread t = new Thread(new Runnable(){
43 <                public void run(){
44 <                    try {
45 <                        l.await();
46 <                    } catch(InterruptedException e){
47 <                        threadFail("unexpected exception");
32 >    /**
33 >     * getCount returns initial count and decreases after countDown
34 >     */
35 >    public void testGetCount() {
36 >        final CountDownLatch l = new CountDownLatch(2);
37 >        assertEquals(2, l.getCount());
38 >        l.countDown();
39 >        assertEquals(1, l.getCount());
40 >    }
41 >
42 >    /**
43 >     * countDown decrements count when positive and has no effect when zero
44 >     */
45 >    public void testCountDown() {
46 >        final CountDownLatch l = new CountDownLatch(1);
47 >        assertEquals(1, l.getCount());
48 >        l.countDown();
49 >        assertEquals(0, l.getCount());
50 >        l.countDown();
51 >        assertEquals(0, l.getCount());
52 >    }
53 >
54 >    /**
55 >     * await returns after countDown to zero, but not before
56 >     */
57 >    public void testAwait() {
58 >        final CountDownLatch l = new CountDownLatch(2);
59 >
60 >        Thread t = new Thread(new Runnable() {
61 >                public void run() {
62 >                    try {
63 >                        threadAssertTrue(l.getCount() > 0);
64 >                        l.await();
65 >                        threadAssertTrue(l.getCount() == 0);
66 >                    } catch (InterruptedException e) {
67 >                        threadUnexpectedException();
68                      }
69 <                }
70 <            });
71 <        t.start();
72 <        try {
69 >                }
70 >            });
71 >        t.start();
72 >        try {
73              assertEquals(l.getCount(), 2);
74              Thread.sleep(SHORT_DELAY_MS);
75              l.countDown();
# Line 52 | Line 77 | public class CountDownLatchTest extends
77              l.countDown();
78              assertEquals(l.getCount(), 0);
79              t.join();
80 <        } catch (InterruptedException e){
81 <            fail("unexpected exception");
80 >        } catch (InterruptedException e) {
81 >            unexpectedException();
82          }
83      }
59    
84  
61    public void testTimedAwait(){
62        final CountDownLatch l = new CountDownLatch(2);
85  
86 <        Thread t = new Thread(new Runnable(){
87 <                public void run(){
88 <                    try {
89 <                        threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
90 <                    } catch(InterruptedException e){
91 <                        threadFail("unexpected exception");
86 >    /**
87 >     * timed await returns after countDown to zero
88 >     */
89 >    public void testTimedAwait() {
90 >        final CountDownLatch l = new CountDownLatch(2);
91 >
92 >        Thread t = new Thread(new Runnable() {
93 >                public void run() {
94 >                    try {
95 >                        threadAssertTrue(l.getCount() > 0);
96 >                        threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS));
97 >                    } catch (InterruptedException e) {
98 >                        threadUnexpectedException();
99                      }
100 <                }
101 <            });
102 <        t.start();
103 <        try {
100 >                }
101 >            });
102 >        t.start();
103 >        try {
104              assertEquals(l.getCount(), 2);
105              Thread.sleep(SHORT_DELAY_MS);
106              l.countDown();
# Line 79 | Line 108 | public class CountDownLatchTest extends
108              l.countDown();
109              assertEquals(l.getCount(), 0);
110              t.join();
111 <        } catch (InterruptedException e){
112 <            fail("unexpected exception");
111 >        } catch (InterruptedException e) {
112 >            unexpectedException();
113          }
114      }
86    
87
115  
116 <
117 <    public void testAwait_InterruptedException(){
116 >    /**
117 >     * await throws IE if interrupted before counted down
118 >     */
119 >    public void testAwait_InterruptedException() {
120          final CountDownLatch l = new CountDownLatch(1);
121 <        Thread t = new Thread(new Runnable(){
122 <                public void run(){
121 >        Thread t = new Thread(new Runnable() {
122 >                public void run() {
123                      try {
124 +                        threadAssertTrue(l.getCount() > 0);
125                          l.await();
126 <                        threadFail("should throw");
127 <                    } catch(InterruptedException success){}
126 >                        threadShouldThrow();
127 >                    } catch (InterruptedException success) {}
128                  }
129              });
130 <        t.start();
131 <        try {
130 >        t.start();
131 >        try {
132              assertEquals(l.getCount(), 1);
133              t.interrupt();
134              t.join();
135 <        } catch (InterruptedException e){
136 <            fail("unexpected exception");
135 >        } catch (InterruptedException e) {
136 >            unexpectedException();
137          }
138      }
139  
140 <    public void testTimedAwait_InterruptedException(){
140 >    /**
141 >     * timed await throws IE if interrupted before counted down
142 >     */
143 >    public void testTimedAwait_InterruptedException() {
144          final CountDownLatch l = new CountDownLatch(1);
145 <        Thread t = new Thread(new Runnable(){
146 <                public void run(){
145 >        Thread t = new Thread(new Runnable() {
146 >                public void run() {
147                      try {
148 <                        l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
149 <                        threadFail("should throw");                        
150 <                    } catch(InterruptedException success){}
148 >                        threadAssertTrue(l.getCount() > 0);
149 >                        l.await(MEDIUM_DELAY_MS, MILLISECONDS);
150 >                        threadShouldThrow();
151 >                    } catch (InterruptedException success) {}
152                  }
153              });
154          t.start();
# Line 123 | Line 157 | public class CountDownLatchTest extends
157              assertEquals(l.getCount(), 1);
158              t.interrupt();
159              t.join();
160 <        } catch (InterruptedException e){
161 <            fail("unexpected exception");
160 >        } catch (InterruptedException e) {
161 >            unexpectedException();
162          }
163      }
164  
165 <    public void testAwaitTimeout(){
165 >    /**
166 >     * timed await times out if not counted down before timeout
167 >     */
168 >    public void testAwaitTimeout() {
169          final CountDownLatch l = new CountDownLatch(1);
170 <        Thread t = new Thread(new Runnable(){
171 <                public void run(){
170 >        Thread t = new Thread(new Runnable() {
171 >                public void run() {
172                      try {
173 <                        threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
174 <                    } catch(InterruptedException ie){
175 <                        threadFail("unexpected exception");
173 >                        threadAssertTrue(l.getCount() > 0);
174 >                        threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
175 >                        threadAssertTrue(l.getCount() > 0);
176 >                    } catch (InterruptedException ie) {
177 >                        threadUnexpectedException();
178                      }
179                  }
180              });
# Line 143 | Line 182 | public class CountDownLatchTest extends
182          try {
183              assertEquals(l.getCount(), 1);
184              t.join();
185 <        } catch (InterruptedException e){
186 <            fail("unexpected exception");
185 >        } catch (InterruptedException e) {
186 >            unexpectedException();
187          }
188      }
189  
190 +    /**
191 +     * toString indicates current count
192 +     */
193 +    public void testToString() {
194 +        CountDownLatch s = new CountDownLatch(2);
195 +        String us = s.toString();
196 +        assertTrue(us.indexOf("Count = 2") >= 0);
197 +        s.countDown();
198 +        String s1 = s.toString();
199 +        assertTrue(s1.indexOf("Count = 1") >= 0);
200 +        s.countDown();
201 +        String s2 = s.toString();
202 +        assertTrue(s2.indexOf("Count = 0") >= 0);
203 +    }
204 +
205   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines