ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.4
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +26 -10 lines
Log Message:
improve tck javadocs; rename and add a few tests

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 CountDownLatchTest 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(CountDownLatchTest.class);
18     }
19    
20 dl 1.3 /**
21 dl 1.4 * negative constructor argument throws IAE
22 dl 1.3 */
23     public void testConstructor() {
24 dl 1.2 try {
25     new CountDownLatch(-1);
26 dl 1.3 shouldThrow();
27 dl 1.2 } catch(IllegalArgumentException success){}
28     }
29 dl 1.1
30 dl 1.3 /**
31 dl 1.4 * getCount returns initial count and decreases after countDown
32 dl 1.3 */
33     public void testGetCount() {
34 dl 1.1 final CountDownLatch l = new CountDownLatch(2);
35     assertEquals(2, l.getCount());
36     l.countDown();
37     assertEquals(1, l.getCount());
38     }
39    
40 dl 1.3 /**
41 dl 1.4 * countDown has no effect when count is zero
42     */
43     public void testcountDown() {
44     final CountDownLatch l = new CountDownLatch(1);
45     assertEquals(1, l.getCount());
46     l.countDown();
47     assertEquals(0, l.getCount());
48     l.countDown();
49     assertEquals(0, l.getCount());
50     }
51    
52     /**
53     * await returns after countDown to zero, but not before
54 dl 1.3 */
55     public void testAwait() {
56 dl 1.1 final CountDownLatch l = new CountDownLatch(2);
57    
58 dl 1.3 Thread t = new Thread(new Runnable() {
59     public void run() {
60 dl 1.2 try {
61 dl 1.4 threadAssertTrue(l.getCount() > 0);
62 dl 1.1 l.await();
63 dl 1.4 threadAssertTrue(l.getCount() == 0);
64 dl 1.2 } catch(InterruptedException e){
65 dl 1.3 threadUnexpectedException();
66 dl 1.1 }
67     }
68     });
69     t.start();
70 dl 1.2 try {
71 dl 1.1 assertEquals(l.getCount(), 2);
72     Thread.sleep(SHORT_DELAY_MS);
73     l.countDown();
74     assertEquals(l.getCount(), 1);
75     l.countDown();
76     assertEquals(l.getCount(), 0);
77     t.join();
78 dl 1.2 } catch (InterruptedException e){
79 dl 1.3 unexpectedException();
80 dl 1.1 }
81     }
82    
83    
84 dl 1.3 /**
85 dl 1.4 * timed await returns after countDown to zero
86 dl 1.3 */
87     public void testTimedAwait() {
88 dl 1.2 final CountDownLatch l = new CountDownLatch(2);
89 dl 1.1
90 dl 1.3 Thread t = new Thread(new Runnable() {
91     public void run() {
92 dl 1.2 try {
93 dl 1.4 threadAssertTrue(l.getCount() > 0);
94 dl 1.2 threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
95     } catch(InterruptedException e){
96 dl 1.3 threadUnexpectedException();
97 dl 1.2 }
98     }
99     });
100     t.start();
101     try {
102     assertEquals(l.getCount(), 2);
103     Thread.sleep(SHORT_DELAY_MS);
104     l.countDown();
105     assertEquals(l.getCount(), 1);
106     l.countDown();
107     assertEquals(l.getCount(), 0);
108     t.join();
109     } catch (InterruptedException e){
110 dl 1.3 unexpectedException();
111 dl 1.2 }
112 dl 1.1 }
113 dl 1.2
114 dl 1.3 /**
115 dl 1.4 * await throws IE ig interrupted before counted down
116 dl 1.3 */
117     public void testAwait_InterruptedException() {
118 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
119 dl 1.3 Thread t = new Thread(new Runnable() {
120     public void run() {
121 dl 1.2 try {
122 dl 1.4 threadAssertTrue(l.getCount() > 0);
123 dl 1.1 l.await();
124 dl 1.3 threadShouldThrow();
125 dl 1.2 } catch(InterruptedException success){}
126 dl 1.1 }
127     });
128     t.start();
129 dl 1.2 try {
130 dl 1.1 assertEquals(l.getCount(), 1);
131     t.interrupt();
132     t.join();
133 dl 1.2 } catch (InterruptedException e){
134 dl 1.3 unexpectedException();
135 dl 1.1 }
136     }
137    
138 dl 1.3 /**
139 dl 1.4 * timed await throws IE ig interrupted before counted down
140 dl 1.3 */
141     public void testTimedAwait_InterruptedException() {
142 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
143 dl 1.3 Thread t = new Thread(new Runnable() {
144     public void run() {
145 dl 1.2 try {
146 dl 1.4 threadAssertTrue(l.getCount() > 0);
147 dl 1.1 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
148 dl 1.3 threadShouldThrow();
149 dl 1.2 } catch(InterruptedException success){}
150 dl 1.1 }
151     });
152     t.start();
153 dl 1.2 try {
154 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
155     assertEquals(l.getCount(), 1);
156     t.interrupt();
157     t.join();
158 dl 1.2 } catch (InterruptedException e){
159 dl 1.3 unexpectedException();
160 dl 1.1 }
161     }
162    
163 dl 1.3 /**
164 dl 1.4 * timed await times out if not counted down before timeout
165 dl 1.3 */
166     public void testAwaitTimeout() {
167 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
168 dl 1.3 Thread t = new Thread(new Runnable() {
169     public void run() {
170 dl 1.2 try {
171 dl 1.4 threadAssertTrue(l.getCount() > 0);
172 dl 1.2 threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
173 dl 1.4 threadAssertTrue(l.getCount() > 0);
174 dl 1.2 } catch(InterruptedException ie){
175 dl 1.3 threadUnexpectedException();
176 dl 1.1 }
177     }
178     });
179     t.start();
180 dl 1.2 try {
181 dl 1.1 assertEquals(l.getCount(), 1);
182     t.join();
183 dl 1.2 } catch (InterruptedException e){
184 dl 1.3 unexpectedException();
185 dl 1.1 }
186     }
187    
188     }