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 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.concurrent.CountDownLatch; |
13 |
> |
import java.util.concurrent.Semaphore; |
14 |
> |
import java.util.concurrent.ThreadLocalRandom; |
15 |
> |
|
16 |
> |
import junit.framework.AssertionFailedError; |
17 |
> |
import junit.framework.Test; |
18 |
> |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class SemaphoreTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
main(suite(), args); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
|
return new TestSuite(SemaphoreTest.class); |
77 |
|
throw new AssertionFailedError("timed out"); |
78 |
|
Thread.yield(); |
79 |
|
} |
80 |
< |
assert(s.hasQueuedThreads()); |
80 |
> |
assertTrue(s.hasQueuedThreads()); |
81 |
|
assertTrue(t.isAlive()); |
82 |
|
} |
83 |
|
|
128 |
|
void acquire(Semaphore s) throws InterruptedException { |
129 |
|
assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS)); |
130 |
|
} |
131 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
132 |
|
}, |
133 |
|
tryAcquireTimedN { |
134 |
|
void acquire(Semaphore s, int permits) throws InterruptedException { |
135 |
|
assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS)); |
136 |
|
} |
137 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
138 |
|
}; |
139 |
|
|
140 |
|
// Intentionally meta-circular |
148 |
|
for (int i = 0; i < permits; i++) |
149 |
|
acquire(s); |
150 |
|
} |
151 |
+ |
Thread.State parkedState() { return Thread.State.WAITING; } |
152 |
|
} |
153 |
|
|
154 |
|
/** |
194 |
|
/** |
195 |
|
* timed tryAcquire times out |
196 |
|
*/ |
197 |
< |
public void testTryAcquire_timeout() { testTryAcquire_timeout(false); } |
198 |
< |
public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); } |
199 |
< |
public void testTryAcquire_timeout(boolean fair) { |
200 |
< |
Semaphore s = new Semaphore(0, fair); |
193 |
< |
long startTime = System.nanoTime(); |
197 |
> |
public void testTryAcquire_timeout() { |
198 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
199 |
> |
final Semaphore s = new Semaphore(0, fair); |
200 |
> |
final long startTime = System.nanoTime(); |
201 |
|
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
202 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
203 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
206 |
|
/** |
207 |
|
* timed tryAcquire(N) times out |
208 |
|
*/ |
209 |
< |
public void testTryAcquireN_timeout() { testTryAcquireN_timeout(false); } |
210 |
< |
public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); } |
211 |
< |
public void testTryAcquireN_timeout(boolean fair) { |
212 |
< |
Semaphore s = new Semaphore(2, fair); |
206 |
< |
long startTime = System.nanoTime(); |
209 |
> |
public void testTryAcquireN_timeout() { |
210 |
> |
final boolean fair = ThreadLocalRandom.current().nextBoolean(); |
211 |
> |
final Semaphore s = new Semaphore(2, fair); |
212 |
> |
final long startTime = System.nanoTime(); |
213 |
|
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
214 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
215 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
229 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
230 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
231 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
232 |
< |
final Semaphore anotherInterruptPlease = new Semaphore(0, fair); |
232 |
> |
final java.util.concurrent.CyclicBarrier pleaseInterrupt |
233 |
> |
= new java.util.concurrent.CyclicBarrier(2); |
234 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
235 |
|
public void realRun() { |
236 |
|
// Interrupt before acquire |
239 |
|
acquirer.acquire(s); |
240 |
|
shouldThrow(); |
241 |
|
} catch (InterruptedException success) {} |
242 |
< |
|
236 |
< |
// Interrupt during acquire |
237 |
< |
try { |
238 |
< |
acquirer.acquire(s); |
239 |
< |
shouldThrow(); |
240 |
< |
} catch (InterruptedException success) {} |
242 |
> |
assertFalse(Thread.interrupted()); |
243 |
|
|
244 |
|
// Interrupt before acquire(N) |
245 |
|
Thread.currentThread().interrupt(); |
247 |
|
acquirer.acquire(s, 3); |
248 |
|
shouldThrow(); |
249 |
|
} catch (InterruptedException success) {} |
250 |
+ |
assertFalse(Thread.interrupted()); |
251 |
|
|
252 |
< |
anotherInterruptPlease.release(); |
252 |
> |
// Interrupt during acquire |
253 |
> |
await(pleaseInterrupt); |
254 |
> |
try { |
255 |
> |
acquirer.acquire(s); |
256 |
> |
shouldThrow(); |
257 |
> |
} catch (InterruptedException success) {} |
258 |
> |
assertFalse(Thread.interrupted()); |
259 |
|
|
260 |
|
// Interrupt during acquire(N) |
261 |
+ |
await(pleaseInterrupt); |
262 |
|
try { |
263 |
|
acquirer.acquire(s, 3); |
264 |
|
shouldThrow(); |
265 |
|
} catch (InterruptedException success) {} |
266 |
+ |
assertFalse(Thread.interrupted()); |
267 |
|
}}); |
268 |
|
|
269 |
< |
waitForQueuedThread(s, t); |
270 |
< |
t.interrupt(); |
271 |
< |
try { |
272 |
< |
assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
273 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
274 |
< |
waitForQueuedThread(s, t); |
264 |
< |
t.interrupt(); |
269 |
> |
for (int n = 2; n-->0; ) { |
270 |
> |
await(pleaseInterrupt); |
271 |
> |
assertThreadBlocks(t, acquirer.parkedState()); |
272 |
> |
t.interrupt(); |
273 |
> |
} |
274 |
> |
|
275 |
|
awaitTermination(t); |
276 |
|
} |
277 |
|
|
285 |
|
public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true, AcquireMethod.acquireUninterruptiblyN); } |
286 |
|
public void testUninterruptible(boolean fair, final AcquireMethod acquirer) { |
287 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
288 |
< |
final Semaphore anotherInterruptPlease = new Semaphore(0, fair); |
289 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
288 |
> |
final Semaphore pleaseInterrupt = new Semaphore(-1, fair); |
289 |
> |
|
290 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
291 |
|
public void realRun() throws InterruptedException { |
292 |
|
// Interrupt before acquire |
293 |
+ |
pleaseInterrupt.release(); |
294 |
|
Thread.currentThread().interrupt(); |
295 |
|
acquirer.acquire(s); |
296 |
|
assertTrue(Thread.interrupted()); |
297 |
+ |
}}); |
298 |
|
|
299 |
< |
anotherInterruptPlease.release(); |
300 |
< |
|
299 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
|
// Interrupt during acquire |
302 |
+ |
pleaseInterrupt.release(); |
303 |
|
acquirer.acquire(s); |
304 |
|
assertTrue(Thread.interrupted()); |
305 |
|
}}); |
306 |
|
|
307 |
< |
waitForQueuedThread(s, t); |
308 |
< |
assertThreadStaysAlive(t); |
309 |
< |
s.release(); |
307 |
> |
await(pleaseInterrupt); |
308 |
> |
waitForQueuedThread(s, t1); |
309 |
> |
waitForQueuedThread(s, t2); |
310 |
> |
t2.interrupt(); |
311 |
|
|
312 |
< |
try { |
313 |
< |
assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
299 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
300 |
< |
waitForQueuedThread(s, t); |
301 |
< |
t.interrupt(); |
302 |
< |
assertThreadStaysAlive(t); |
303 |
< |
s.release(); |
312 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
313 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
314 |
|
|
315 |
< |
awaitTermination(t); |
315 |
> |
s.release(2); |
316 |
> |
|
317 |
> |
awaitTermination(t1); |
318 |
> |
awaitTermination(t2); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
475 |
|
clone.release(); |
476 |
|
assertEquals(2, s.availablePermits()); |
477 |
|
assertEquals(1, clone.availablePermits()); |
478 |
+ |
assertFalse(s.hasQueuedThreads()); |
479 |
+ |
assertFalse(clone.hasQueuedThreads()); |
480 |
+ |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
481 |
|
|
482 |
< |
s = new Semaphore(0, fair); |
482 |
> |
{ |
483 |
> |
PublicSemaphore s = new PublicSemaphore(0, fair); |
484 |
|
Thread t = newStartedThread(new InterruptibleLockRunnable(s)); |
485 |
< |
waitForQueuedThreads(s); |
486 |
< |
clone = serialClone(s); |
485 |
> |
// waitForQueuedThreads(s); // suffers from "flicker", so ... |
486 |
> |
waitForQueuedThread(s, t); // ... we use this instead |
487 |
> |
PublicSemaphore clone = serialClone(s); |
488 |
|
assertEquals(fair, s.isFair()); |
489 |
|
assertEquals(fair, clone.isFair()); |
490 |
|
assertEquals(0, s.availablePermits()); |
495 |
|
awaitTermination(t); |
496 |
|
assertFalse(s.hasQueuedThreads()); |
497 |
|
assertFalse(clone.hasQueuedThreads()); |
498 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
498 |
> |
} |
499 |
|
} |
500 |
|
|
501 |
|
/** |
603 |
|
s.acquire(3); |
604 |
|
}}); |
605 |
|
|
606 |
< |
waitForQueuedThreads(s); |
606 |
> |
waitForQueuedThread(s, t1); |
607 |
|
|
608 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
609 |
|
public void realRun() throws InterruptedException { |
610 |
|
// Will fail, even though 1 permit is available |
611 |
< |
assertFalse(s.tryAcquire(0L, MILLISECONDS)); |
612 |
< |
assertFalse(s.tryAcquire(1, 0L, MILLISECONDS)); |
611 |
> |
assertFalse( |
612 |
> |
s.tryAcquire(randomExpiredTimeout(), randomTimeUnit())); |
613 |
> |
assertFalse( |
614 |
> |
s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit())); |
615 |
|
|
616 |
|
// untimed tryAcquire will barge and succeed |
617 |
|
assertTrue(s.tryAcquire()); |
631 |
|
assertTrue(t2.isAlive()); |
632 |
|
s.release(); |
633 |
|
awaitTermination(t2); |
634 |
< |
} |
634 |
> |
} |
635 |
|
|
636 |
|
/** |
637 |
|
* toString indicates current number of permits |