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