1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.CountDownLatch; |
12 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
13 |
+ |
|
14 |
+ |
import junit.framework.Test; |
15 |
+ |
import junit.framework.TestSuite; |
16 |
+ |
|
17 |
|
public class CountDownLatchTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run(suite()); |
19 |
> |
main(suite(), args); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(CountDownLatchTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
/** |
26 |
< |
* negative constructor argument throws IAE |
26 |
> |
* negative constructor argument throws IllegalArgumentException |
27 |
|
*/ |
28 |
|
public void testConstructor() { |
29 |
|
try { |
57 |
|
/** |
58 |
|
* await returns after countDown to zero, but not before |
59 |
|
*/ |
60 |
< |
public void testAwait() throws InterruptedException { |
60 |
> |
public void testAwait() { |
61 |
|
final CountDownLatch l = new CountDownLatch(2); |
62 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
63 |
|
|
64 |
< |
Thread t = new Thread(new CheckedRunnable() { |
64 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
65 |
|
public void realRun() throws InterruptedException { |
66 |
< |
assertTrue(l.getCount() > 0); |
66 |
> |
assertEquals(2, l.getCount()); |
67 |
> |
pleaseCountDown.countDown(); |
68 |
|
l.await(); |
69 |
|
assertEquals(0, l.getCount()); |
70 |
|
}}); |
71 |
|
|
72 |
< |
t.start(); |
73 |
< |
assertEquals(l.getCount(), 2); |
69 |
< |
Thread.sleep(SHORT_DELAY_MS); |
72 |
> |
await(pleaseCountDown); |
73 |
> |
assertEquals(2, l.getCount()); |
74 |
|
l.countDown(); |
75 |
< |
assertEquals(l.getCount(), 1); |
75 |
> |
assertEquals(1, l.getCount()); |
76 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
77 |
|
l.countDown(); |
78 |
< |
assertEquals(l.getCount(), 0); |
79 |
< |
t.join(); |
78 |
> |
assertEquals(0, l.getCount()); |
79 |
> |
awaitTermination(t); |
80 |
|
} |
81 |
|
|
77 |
– |
|
82 |
|
/** |
83 |
|
* timed await returns after countDown to zero |
84 |
|
*/ |
85 |
< |
public void testTimedAwait() throws InterruptedException { |
85 |
> |
public void testTimedAwait() { |
86 |
|
final CountDownLatch l = new CountDownLatch(2); |
87 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
88 |
|
|
89 |
< |
Thread t = new Thread(new CheckedRunnable() { |
89 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
90 |
|
public void realRun() throws InterruptedException { |
91 |
< |
assertTrue(l.getCount() > 0); |
92 |
< |
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
91 |
> |
assertEquals(2, l.getCount()); |
92 |
> |
pleaseCountDown.countDown(); |
93 |
> |
assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); |
94 |
> |
assertEquals(0, l.getCount()); |
95 |
|
}}); |
96 |
|
|
97 |
< |
t.start(); |
98 |
< |
assertEquals(l.getCount(), 2); |
92 |
< |
Thread.sleep(SHORT_DELAY_MS); |
97 |
> |
await(pleaseCountDown); |
98 |
> |
assertEquals(2, l.getCount()); |
99 |
|
l.countDown(); |
100 |
< |
assertEquals(l.getCount(), 1); |
100 |
> |
assertEquals(1, l.getCount()); |
101 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
102 |
|
l.countDown(); |
103 |
< |
assertEquals(l.getCount(), 0); |
104 |
< |
t.join(); |
103 |
> |
assertEquals(0, l.getCount()); |
104 |
> |
awaitTermination(t); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* await throws IE if interrupted before counted down |
108 |
> |
* await throws InterruptedException if interrupted before counted down |
109 |
|
*/ |
110 |
< |
public void testAwait_InterruptedException() throws InterruptedException { |
110 |
> |
public void testAwait_Interruptible() { |
111 |
|
final CountDownLatch l = new CountDownLatch(1); |
112 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
112 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
113 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
114 |
|
public void realRun() throws InterruptedException { |
115 |
< |
assertTrue(l.getCount() > 0); |
116 |
< |
l.await(); |
115 |
> |
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 |
|
}}); |
131 |
|
|
132 |
< |
t.start(); |
133 |
< |
assertEquals(l.getCount(), 1); |
132 |
> |
await(pleaseInterrupt); |
133 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
134 |
|
t.interrupt(); |
135 |
< |
t.join(); |
135 |
> |
awaitTermination(t); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* timed await throws IE if interrupted before counted down |
139 |
> |
* timed await throws InterruptedException if interrupted before counted down |
140 |
|
*/ |
141 |
< |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
142 |
< |
final CountDownLatch l = new CountDownLatch(1); |
143 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
141 |
> |
public void testTimedAwait_Interruptible() { |
142 |
> |
final int initialCount = ThreadLocalRandom.current().nextInt(1, 3); |
143 |
> |
final CountDownLatch l = new CountDownLatch(initialCount); |
144 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
145 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
146 |
|
public void realRun() throws InterruptedException { |
147 |
< |
assertTrue(l.getCount() > 0); |
148 |
< |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
147 |
> |
Thread.currentThread().interrupt(); |
148 |
> |
try { |
149 |
> |
l.await(randomTimeout(), randomTimeUnit()); |
150 |
> |
shouldThrow(); |
151 |
> |
} catch (InterruptedException success) {} |
152 |
> |
assertFalse(Thread.interrupted()); |
153 |
> |
|
154 |
> |
pleaseInterrupt.countDown(); |
155 |
> |
try { |
156 |
> |
l.await(LONGER_DELAY_MS, MILLISECONDS); |
157 |
> |
shouldThrow(); |
158 |
> |
} catch (InterruptedException success) {} |
159 |
> |
assertFalse(Thread.interrupted()); |
160 |
> |
|
161 |
> |
assertEquals(initialCount, l.getCount()); |
162 |
|
}}); |
163 |
|
|
164 |
< |
t.start(); |
165 |
< |
Thread.sleep(SHORT_DELAY_MS); |
130 |
< |
assertEquals(l.getCount(), 1); |
164 |
> |
await(pleaseInterrupt); |
165 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
166 |
|
t.interrupt(); |
167 |
< |
t.join(); |
167 |
> |
awaitTermination(t); |
168 |
|
} |
169 |
|
|
170 |
|
/** |
172 |
|
*/ |
173 |
|
public void testAwaitTimeout() throws InterruptedException { |
174 |
|
final CountDownLatch l = new CountDownLatch(1); |
175 |
< |
Thread t = new Thread(new CheckedRunnable() { |
175 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
176 |
|
public void realRun() throws InterruptedException { |
177 |
< |
assertTrue(l.getCount() > 0); |
178 |
< |
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
179 |
< |
assertTrue(l.getCount() > 0); |
177 |
> |
assertEquals(1, l.getCount()); |
178 |
> |
|
179 |
> |
long startTime = System.nanoTime(); |
180 |
> |
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
181 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
182 |
> |
|
183 |
> |
assertEquals(1, l.getCount()); |
184 |
|
}}); |
185 |
|
|
186 |
< |
t.start(); |
187 |
< |
assertEquals(l.getCount(), 1); |
149 |
< |
t.join(); |
186 |
> |
awaitTermination(t); |
187 |
> |
assertEquals(1, l.getCount()); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
192 |
|
*/ |
193 |
|
public void testToString() { |
194 |
|
CountDownLatch s = new CountDownLatch(2); |
195 |
< |
String us = s.toString(); |
158 |
< |
assertTrue(us.indexOf("Count = 2") >= 0); |
195 |
> |
assertTrue(s.toString().contains("Count = 2")); |
196 |
|
s.countDown(); |
197 |
< |
String s1 = s.toString(); |
161 |
< |
assertTrue(s1.indexOf("Count = 1") >= 0); |
197 |
> |
assertTrue(s.toString().contains("Count = 1")); |
198 |
|
s.countDown(); |
199 |
< |
String s2 = s.toString(); |
164 |
< |
assertTrue(s2.indexOf("Count = 0") >= 0); |
199 |
> |
assertTrue(s.toString().contains("Count = 0")); |
200 |
|
} |
201 |
|
|
202 |
|
} |