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; |
73 |
|
assertEquals(2, l.getCount()); |
74 |
|
l.countDown(); |
75 |
|
assertEquals(1, l.getCount()); |
76 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
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 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
101 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
102 |
|
l.countDown(); |
103 |
|
assertEquals(0, l.getCount()); |
104 |
|
awaitTermination(t); |
130 |
|
}}); |
131 |
|
|
132 |
|
await(pleaseInterrupt); |
133 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
133 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
134 |
|
t.interrupt(); |
135 |
|
awaitTermination(t); |
136 |
|
} |
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 |
+ |
long startTime = System.nanoTime(); |
148 |
+ |
|
149 |
|
Thread.currentThread().interrupt(); |
150 |
|
try { |
151 |
< |
l.await(LONG_DELAY_MS, MILLISECONDS); |
151 |
> |
l.await(randomTimeout(), randomTimeUnit()); |
152 |
|
shouldThrow(); |
153 |
|
} catch (InterruptedException success) {} |
154 |
|
assertFalse(Thread.interrupted()); |
160 |
|
} catch (InterruptedException success) {} |
161 |
|
assertFalse(Thread.interrupted()); |
162 |
|
|
163 |
< |
assertEquals(1, l.getCount()); |
163 |
> |
assertEquals(initialCount, l.getCount()); |
164 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
165 |
|
}}); |
166 |
|
|
167 |
|
await(pleaseInterrupt); |
168 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
168 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
169 |
|
t.interrupt(); |
170 |
|
awaitTermination(t); |
171 |
|
} |