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 |
jsr166 |
1.16 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.22 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
|
11 |
jsr166 |
1.21 |
import java.util.concurrent.CountDownLatch; |
12 |
jsr166 |
1.28 |
import java.util.concurrent.ThreadLocalRandom; |
13 |
jsr166 |
1.22 |
|
14 |
|
|
import junit.framework.Test; |
15 |
|
|
import junit.framework.TestSuite; |
16 |
dl |
1.1 |
|
17 |
dl |
1.2 |
public class CountDownLatchTest extends JSR166TestCase { |
18 |
dl |
1.1 |
public static void main(String[] args) { |
19 |
jsr166 |
1.23 |
main(suite(), args); |
20 |
dl |
1.1 |
} |
21 |
|
|
public static Test suite() { |
22 |
jsr166 |
1.11 |
return new TestSuite(CountDownLatchTest.class); |
23 |
dl |
1.1 |
} |
24 |
|
|
|
25 |
dl |
1.3 |
/** |
26 |
jsr166 |
1.26 |
* negative constructor argument throws IllegalArgumentException |
27 |
dl |
1.3 |
*/ |
28 |
|
|
public void testConstructor() { |
29 |
dl |
1.2 |
try { |
30 |
|
|
new CountDownLatch(-1); |
31 |
dl |
1.3 |
shouldThrow(); |
32 |
jsr166 |
1.10 |
} catch (IllegalArgumentException success) {} |
33 |
dl |
1.2 |
} |
34 |
dl |
1.1 |
|
35 |
dl |
1.3 |
/** |
36 |
dl |
1.4 |
* getCount returns initial count and decreases after countDown |
37 |
dl |
1.3 |
*/ |
38 |
|
|
public void testGetCount() { |
39 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
40 |
|
|
assertEquals(2, l.getCount()); |
41 |
|
|
l.countDown(); |
42 |
|
|
assertEquals(1, l.getCount()); |
43 |
dl |
1.1 |
} |
44 |
|
|
|
45 |
dl |
1.3 |
/** |
46 |
dl |
1.5 |
* countDown decrements count when positive and has no effect when zero |
47 |
dl |
1.4 |
*/ |
48 |
dl |
1.5 |
public void testCountDown() { |
49 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(1); |
50 |
|
|
assertEquals(1, l.getCount()); |
51 |
|
|
l.countDown(); |
52 |
|
|
assertEquals(0, l.getCount()); |
53 |
|
|
l.countDown(); |
54 |
|
|
assertEquals(0, l.getCount()); |
55 |
dl |
1.4 |
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* await returns after countDown to zero, but not before |
59 |
dl |
1.3 |
*/ |
60 |
jsr166 |
1.20 |
public void testAwait() { |
61 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
62 |
jsr166 |
1.20 |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
63 |
dl |
1.1 |
|
64 |
jsr166 |
1.20 |
Thread t = newStartedThread(new CheckedRunnable() { |
65 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
66 |
jsr166 |
1.20 |
assertEquals(2, l.getCount()); |
67 |
|
|
pleaseCountDown.countDown(); |
68 |
jsr166 |
1.13 |
l.await(); |
69 |
jsr166 |
1.15 |
assertEquals(0, l.getCount()); |
70 |
jsr166 |
1.13 |
}}); |
71 |
|
|
|
72 |
jsr166 |
1.20 |
await(pleaseCountDown); |
73 |
|
|
assertEquals(2, l.getCount()); |
74 |
jsr166 |
1.13 |
l.countDown(); |
75 |
jsr166 |
1.20 |
assertEquals(1, l.getCount()); |
76 |
jsr166 |
1.28 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
77 |
jsr166 |
1.13 |
l.countDown(); |
78 |
jsr166 |
1.20 |
assertEquals(0, l.getCount()); |
79 |
|
|
awaitTermination(t); |
80 |
dl |
1.1 |
} |
81 |
jsr166 |
1.8 |
|
82 |
dl |
1.3 |
/** |
83 |
dl |
1.4 |
* timed await returns after countDown to zero |
84 |
dl |
1.3 |
*/ |
85 |
jsr166 |
1.20 |
public void testTimedAwait() { |
86 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
87 |
jsr166 |
1.20 |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
88 |
dl |
1.1 |
|
89 |
jsr166 |
1.20 |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
91 |
jsr166 |
1.20 |
assertEquals(2, l.getCount()); |
92 |
|
|
pleaseCountDown.countDown(); |
93 |
|
|
assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); |
94 |
|
|
assertEquals(0, l.getCount()); |
95 |
jsr166 |
1.13 |
}}); |
96 |
|
|
|
97 |
jsr166 |
1.20 |
await(pleaseCountDown); |
98 |
|
|
assertEquals(2, l.getCount()); |
99 |
jsr166 |
1.13 |
l.countDown(); |
100 |
jsr166 |
1.20 |
assertEquals(1, l.getCount()); |
101 |
jsr166 |
1.28 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
102 |
jsr166 |
1.13 |
l.countDown(); |
103 |
jsr166 |
1.20 |
assertEquals(0, l.getCount()); |
104 |
|
|
awaitTermination(t); |
105 |
dl |
1.1 |
} |
106 |
jsr166 |
1.8 |
|
107 |
dl |
1.3 |
/** |
108 |
jsr166 |
1.25 |
* await throws InterruptedException if interrupted before counted down |
109 |
dl |
1.3 |
*/ |
110 |
jsr166 |
1.20 |
public void testAwait_Interruptible() { |
111 |
dl |
1.1 |
final CountDownLatch l = new CountDownLatch(1); |
112 |
jsr166 |
1.20 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
113 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
114 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
115 |
jsr166 |
1.20 |
Thread.currentThread().interrupt(); |
116 |
|
|
try { |
117 |
|
|
l.await(); |
118 |
|
|
shouldThrow(); |
119 |
|
|
} catch (InterruptedException success) {} |
120 |
|
|
assertFalse(Thread.interrupted()); |
121 |
|
|
|
122 |
|
|
pleaseInterrupt.countDown(); |
123 |
|
|
try { |
124 |
|
|
l.await(); |
125 |
|
|
shouldThrow(); |
126 |
|
|
} catch (InterruptedException success) {} |
127 |
|
|
assertFalse(Thread.interrupted()); |
128 |
|
|
|
129 |
|
|
assertEquals(1, l.getCount()); |
130 |
jsr166 |
1.13 |
}}); |
131 |
|
|
|
132 |
jsr166 |
1.20 |
await(pleaseInterrupt); |
133 |
jsr166 |
1.28 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
134 |
jsr166 |
1.13 |
t.interrupt(); |
135 |
jsr166 |
1.20 |
awaitTermination(t); |
136 |
dl |
1.1 |
} |
137 |
|
|
|
138 |
dl |
1.3 |
/** |
139 |
jsr166 |
1.25 |
* timed await throws InterruptedException if interrupted before counted down |
140 |
dl |
1.3 |
*/ |
141 |
jsr166 |
1.20 |
public void testTimedAwait_Interruptible() { |
142 |
jsr166 |
1.28 |
final int initialCount = ThreadLocalRandom.current().nextInt(1, 3); |
143 |
|
|
final CountDownLatch l = new CountDownLatch(initialCount); |
144 |
jsr166 |
1.20 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
145 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
146 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
147 |
jsr166 |
1.20 |
Thread.currentThread().interrupt(); |
148 |
|
|
try { |
149 |
jsr166 |
1.28 |
l.await(randomTimeout(), randomTimeUnit()); |
150 |
jsr166 |
1.20 |
shouldThrow(); |
151 |
|
|
} catch (InterruptedException success) {} |
152 |
|
|
assertFalse(Thread.interrupted()); |
153 |
|
|
|
154 |
|
|
pleaseInterrupt.countDown(); |
155 |
|
|
try { |
156 |
jsr166 |
1.29 |
l.await(LONGER_DELAY_MS, MILLISECONDS); |
157 |
jsr166 |
1.20 |
shouldThrow(); |
158 |
|
|
} catch (InterruptedException success) {} |
159 |
|
|
assertFalse(Thread.interrupted()); |
160 |
|
|
|
161 |
jsr166 |
1.28 |
assertEquals(initialCount, l.getCount()); |
162 |
jsr166 |
1.13 |
}}); |
163 |
|
|
|
164 |
jsr166 |
1.20 |
await(pleaseInterrupt); |
165 |
jsr166 |
1.28 |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
166 |
jsr166 |
1.13 |
t.interrupt(); |
167 |
jsr166 |
1.20 |
awaitTermination(t); |
168 |
dl |
1.1 |
} |
169 |
|
|
|
170 |
dl |
1.3 |
/** |
171 |
dl |
1.4 |
* timed await times out if not counted down before timeout |
172 |
dl |
1.3 |
*/ |
173 |
jsr166 |
1.13 |
public void testAwaitTimeout() throws InterruptedException { |
174 |
dl |
1.1 |
final CountDownLatch l = new CountDownLatch(1); |
175 |
jsr166 |
1.20 |
Thread t = newStartedThread(new CheckedRunnable() { |
176 |
jsr166 |
1.13 |
public void realRun() throws InterruptedException { |
177 |
jsr166 |
1.20 |
assertEquals(1, l.getCount()); |
178 |
jsr166 |
1.27 |
|
179 |
|
|
long startTime = System.nanoTime(); |
180 |
jsr166 |
1.20 |
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
181 |
jsr166 |
1.27 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
182 |
|
|
|
183 |
jsr166 |
1.20 |
assertEquals(1, l.getCount()); |
184 |
jsr166 |
1.13 |
}}); |
185 |
|
|
|
186 |
jsr166 |
1.20 |
awaitTermination(t); |
187 |
|
|
assertEquals(1, l.getCount()); |
188 |
dl |
1.1 |
} |
189 |
|
|
|
190 |
dl |
1.7 |
/** |
191 |
|
|
* toString indicates current count |
192 |
|
|
*/ |
193 |
|
|
public void testToString() { |
194 |
|
|
CountDownLatch s = new CountDownLatch(2); |
195 |
jsr166 |
1.18 |
assertTrue(s.toString().contains("Count = 2")); |
196 |
dl |
1.7 |
s.countDown(); |
197 |
jsr166 |
1.18 |
assertTrue(s.toString().contains("Count = 1")); |
198 |
dl |
1.7 |
s.countDown(); |
199 |
jsr166 |
1.18 |
assertTrue(s.toString().contains("Count = 0")); |
200 |
dl |
1.7 |
} |
201 |
|
|
|
202 |
dl |
1.1 |
} |