6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.CountDownLatch; |
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 { |
73 |
|
assertEquals(2, l.getCount()); |
74 |
|
l.countDown(); |
75 |
|
assertEquals(1, l.getCount()); |
76 |
< |
assertThreadStaysAlive(t); |
76 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
77 |
|
l.countDown(); |
78 |
|
assertEquals(0, l.getCount()); |
79 |
|
awaitTermination(t); |
98 |
|
assertEquals(2, l.getCount()); |
99 |
|
l.countDown(); |
100 |
|
assertEquals(1, l.getCount()); |
101 |
< |
assertThreadStaysAlive(t); |
101 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
102 |
|
l.countDown(); |
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_Interruptible() { |
111 |
|
final CountDownLatch l = new CountDownLatch(1); |
130 |
|
}}); |
131 |
|
|
132 |
|
await(pleaseInterrupt); |
133 |
< |
assertThreadStaysAlive(t); |
133 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
134 |
|
t.interrupt(); |
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_Interruptible() { |
142 |
< |
final CountDownLatch l = new CountDownLatch(1); |
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 |
|
Thread.currentThread().interrupt(); |
148 |
|
try { |
149 |
< |
l.await(LONG_DELAY_MS, MILLISECONDS); |
149 |
> |
l.await(randomTimeout(), randomTimeUnit()); |
150 |
|
shouldThrow(); |
151 |
|
} catch (InterruptedException success) {} |
152 |
|
assertFalse(Thread.interrupted()); |
153 |
|
|
154 |
|
pleaseInterrupt.countDown(); |
155 |
|
try { |
156 |
< |
l.await(LONG_DELAY_MS, MILLISECONDS); |
156 |
> |
l.await(LONGER_DELAY_MS, MILLISECONDS); |
157 |
|
shouldThrow(); |
158 |
|
} catch (InterruptedException success) {} |
159 |
|
assertFalse(Thread.interrupted()); |
160 |
|
|
161 |
< |
assertEquals(1, l.getCount()); |
161 |
> |
assertEquals(initialCount, l.getCount()); |
162 |
|
}}); |
163 |
|
|
164 |
|
await(pleaseInterrupt); |
165 |
< |
assertThreadStaysAlive(t); |
165 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
166 |
|
t.interrupt(); |
167 |
|
awaitTermination(t); |
168 |
|
} |
175 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
176 |
|
public void realRun() throws InterruptedException { |
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 |
|
|