11 |
|
import java.util.Collection; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.Semaphore; |
14 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
15 |
|
|
15 |
– |
import junit.framework.AssertionFailedError; |
16 |
|
import junit.framework.Test; |
17 |
|
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class SemaphoreTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run(suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
|
return new TestSuite(SemaphoreTest.class); |
73 |
|
long startTime = System.nanoTime(); |
74 |
|
while (!s.hasQueuedThread(t)) { |
75 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
76 |
< |
throw new AssertionFailedError("timed out"); |
76 |
> |
throw new AssertionError("timed out"); |
77 |
|
Thread.yield(); |
78 |
|
} |
79 |
|
assertTrue(s.hasQueuedThreads()); |
87 |
|
long startTime = System.nanoTime(); |
88 |
|
while (!s.hasQueuedThreads()) { |
89 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
90 |
< |
throw new AssertionFailedError("timed out"); |
90 |
> |
throw new AssertionError("timed out"); |
91 |
|
Thread.yield(); |
92 |
|
} |
93 |
|
} |
127 |
|
void acquire(Semaphore s) throws InterruptedException { |
128 |
|
assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS)); |
129 |
|
} |
130 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
131 |
|
}, |
132 |
|
tryAcquireTimedN { |
133 |
|
void acquire(Semaphore s, int permits) throws InterruptedException { |
134 |
|
assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS)); |
135 |
|
} |
136 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
137 |
|
}; |
138 |
|
|
139 |
|
// Intentionally meta-circular |
147 |
|
for (int i = 0; i < permits; i++) |
148 |
|
acquire(s); |
149 |
|
} |
150 |
+ |
Thread.State parkedState() { return Thread.State.WAITING; } |
151 |
|
} |
152 |
|
|
153 |
|
/** |
193 |
|
/** |
194 |
|
* timed tryAcquire times out |
195 |
|
*/ |
196 |
< |
public void testTryAcquire_timeout() { testTryAcquire_timeout(false); } |
197 |
< |
public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); } |
198 |
< |
public void testTryAcquire_timeout(boolean fair) { |
199 |
< |
Semaphore s = new Semaphore(0, fair); |
197 |
< |
long startTime = System.nanoTime(); |
196 |
> |
public void testTryAcquire_timeout() { |
197 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
198 |
> |
final Semaphore s = new Semaphore(0, fair); |
199 |
> |
final long startTime = System.nanoTime(); |
200 |
|
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
201 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
202 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
205 |
|
/** |
206 |
|
* timed tryAcquire(N) times out |
207 |
|
*/ |
208 |
< |
public void testTryAcquireN_timeout() { testTryAcquireN_timeout(false); } |
209 |
< |
public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); } |
210 |
< |
public void testTryAcquireN_timeout(boolean fair) { |
211 |
< |
Semaphore s = new Semaphore(2, fair); |
210 |
< |
long startTime = System.nanoTime(); |
208 |
> |
public void testTryAcquireN_timeout() { |
209 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
210 |
> |
final Semaphore s = new Semaphore(2, fair); |
211 |
> |
final long startTime = System.nanoTime(); |
212 |
|
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
213 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
214 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
228 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
229 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
230 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
231 |
< |
final Semaphore pleaseInterrupt = new Semaphore(0, fair); |
231 |
> |
final java.util.concurrent.CyclicBarrier pleaseInterrupt |
232 |
> |
= new java.util.concurrent.CyclicBarrier(2); |
233 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
234 |
|
public void realRun() { |
235 |
|
// Interrupt before acquire |
238 |
|
acquirer.acquire(s); |
239 |
|
shouldThrow(); |
240 |
|
} catch (InterruptedException success) {} |
241 |
< |
|
240 |
< |
// Interrupt during acquire |
241 |
< |
try { |
242 |
< |
acquirer.acquire(s); |
243 |
< |
shouldThrow(); |
244 |
< |
} catch (InterruptedException success) {} |
241 |
> |
assertFalse(Thread.interrupted()); |
242 |
|
|
243 |
|
// Interrupt before acquire(N) |
244 |
|
Thread.currentThread().interrupt(); |
246 |
|
acquirer.acquire(s, 3); |
247 |
|
shouldThrow(); |
248 |
|
} catch (InterruptedException success) {} |
249 |
+ |
assertFalse(Thread.interrupted()); |
250 |
|
|
251 |
< |
pleaseInterrupt.release(); |
251 |
> |
// Interrupt during acquire |
252 |
> |
await(pleaseInterrupt); |
253 |
> |
try { |
254 |
> |
acquirer.acquire(s); |
255 |
> |
shouldThrow(); |
256 |
> |
} catch (InterruptedException success) {} |
257 |
> |
assertFalse(Thread.interrupted()); |
258 |
|
|
259 |
|
// Interrupt during acquire(N) |
260 |
+ |
await(pleaseInterrupt); |
261 |
|
try { |
262 |
|
acquirer.acquire(s, 3); |
263 |
|
shouldThrow(); |
264 |
|
} catch (InterruptedException success) {} |
265 |
+ |
assertFalse(Thread.interrupted()); |
266 |
|
}}); |
267 |
|
|
268 |
< |
waitForQueuedThread(s, t); |
269 |
< |
t.interrupt(); |
270 |
< |
await(pleaseInterrupt); |
271 |
< |
waitForQueuedThread(s, t); |
272 |
< |
t.interrupt(); |
268 |
> |
for (int n = 2; n-->0; ) { |
269 |
> |
await(pleaseInterrupt); |
270 |
> |
assertThreadBlocks(t, acquirer.parkedState()); |
271 |
> |
t.interrupt(); |
272 |
> |
} |
273 |
> |
|
274 |
|
awaitTermination(t); |
275 |
|
} |
276 |
|
|
308 |
|
waitForQueuedThread(s, t2); |
309 |
|
t2.interrupt(); |
310 |
|
|
311 |
< |
assertThreadStaysAlive(t1); |
312 |
< |
assertTrue(t2.isAlive()); |
311 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
312 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
313 |
|
|
314 |
|
s.release(2); |
315 |
|
|
474 |
|
clone.release(); |
475 |
|
assertEquals(2, s.availablePermits()); |
476 |
|
assertEquals(1, clone.availablePermits()); |
477 |
+ |
assertFalse(s.hasQueuedThreads()); |
478 |
+ |
assertFalse(clone.hasQueuedThreads()); |
479 |
+ |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
480 |
|
|
481 |
< |
s = new Semaphore(0, fair); |
481 |
> |
{ |
482 |
> |
PublicSemaphore s = new PublicSemaphore(0, fair); |
483 |
|
Thread t = newStartedThread(new InterruptibleLockRunnable(s)); |
484 |
< |
waitForQueuedThreads(s); |
485 |
< |
clone = serialClone(s); |
484 |
> |
// waitForQueuedThreads(s); // suffers from "flicker", so ... |
485 |
> |
waitForQueuedThread(s, t); // ... we use this instead |
486 |
> |
PublicSemaphore clone = serialClone(s); |
487 |
|
assertEquals(fair, s.isFair()); |
488 |
|
assertEquals(fair, clone.isFair()); |
489 |
|
assertEquals(0, s.availablePermits()); |
494 |
|
awaitTermination(t); |
495 |
|
assertFalse(s.hasQueuedThreads()); |
496 |
|
assertFalse(clone.hasQueuedThreads()); |
497 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
497 |
> |
} |
498 |
|
} |
499 |
|
|
500 |
|
/** |
602 |
|
s.acquire(3); |
603 |
|
}}); |
604 |
|
|
605 |
< |
waitForQueuedThreads(s); |
605 |
> |
waitForQueuedThread(s, t1); |
606 |
|
|
607 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
608 |
|
public void realRun() throws InterruptedException { |
609 |
|
// Will fail, even though 1 permit is available |
610 |
< |
assertFalse(s.tryAcquire(0L, MILLISECONDS)); |
611 |
< |
assertFalse(s.tryAcquire(1, 0L, MILLISECONDS)); |
610 |
> |
assertFalse( |
611 |
> |
s.tryAcquire(randomExpiredTimeout(), randomTimeUnit())); |
612 |
> |
assertFalse( |
613 |
> |
s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit())); |
614 |
|
|
615 |
|
// untimed tryAcquire will barge and succeed |
616 |
|
assertTrue(s.tryAcquire()); |