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