6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
> |
import java.util.Collection; |
12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.Semaphore; |
14 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
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); |
201 |
< |
long startTime = System.nanoTime(); |
197 |
> |
public void testTryAcquire_timeout() { |
198 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
199 |
> |
final boolean fair = rnd.nextBoolean(); |
200 |
> |
final Semaphore s = new Semaphore(0, fair); |
201 |
> |
final long startTime = System.nanoTime(); |
202 |
|
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
203 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
204 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
207 |
|
/** |
208 |
|
* timed tryAcquire(N) times out |
209 |
|
*/ |
210 |
< |
public void testTryAcquireN_timeout() { testTryAcquireN_timeout(false); } |
211 |
< |
public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); } |
212 |
< |
public void testTryAcquireN_timeout(boolean fair) { |
213 |
< |
Semaphore s = new Semaphore(2, fair); |
214 |
< |
long startTime = System.nanoTime(); |
210 |
> |
public void testTryAcquireN_timeout() { |
211 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
212 |
> |
final boolean fair = rnd.nextBoolean(); |
213 |
> |
final Semaphore s = new Semaphore(2, fair); |
214 |
> |
final long startTime = System.nanoTime(); |
215 |
|
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
216 |
|
catch (InterruptedException e) { threadUnexpectedException(e); } |
217 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
231 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
232 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
233 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
234 |
< |
final Semaphore pleaseInterrupt = new Semaphore(0, fair); |
234 |
> |
final java.util.concurrent.CyclicBarrier pleaseInterrupt |
235 |
> |
= new java.util.concurrent.CyclicBarrier(2); |
236 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
237 |
|
public void realRun() { |
238 |
|
// Interrupt before acquire |
241 |
|
acquirer.acquire(s); |
242 |
|
shouldThrow(); |
243 |
|
} catch (InterruptedException success) {} |
244 |
< |
|
236 |
< |
// Interrupt during acquire |
237 |
< |
try { |
238 |
< |
acquirer.acquire(s); |
239 |
< |
shouldThrow(); |
240 |
< |
} catch (InterruptedException success) {} |
244 |
> |
assertFalse(Thread.interrupted()); |
245 |
|
|
246 |
|
// Interrupt before acquire(N) |
247 |
|
Thread.currentThread().interrupt(); |
249 |
|
acquirer.acquire(s, 3); |
250 |
|
shouldThrow(); |
251 |
|
} catch (InterruptedException success) {} |
252 |
+ |
assertFalse(Thread.interrupted()); |
253 |
|
|
254 |
< |
pleaseInterrupt.release(); |
254 |
> |
// Interrupt during acquire |
255 |
> |
await(pleaseInterrupt); |
256 |
> |
try { |
257 |
> |
acquirer.acquire(s); |
258 |
> |
shouldThrow(); |
259 |
> |
} catch (InterruptedException success) {} |
260 |
> |
assertFalse(Thread.interrupted()); |
261 |
|
|
262 |
|
// Interrupt during acquire(N) |
263 |
+ |
await(pleaseInterrupt); |
264 |
|
try { |
265 |
|
acquirer.acquire(s, 3); |
266 |
|
shouldThrow(); |
267 |
|
} catch (InterruptedException success) {} |
268 |
+ |
assertFalse(Thread.interrupted()); |
269 |
|
}}); |
270 |
|
|
271 |
< |
waitForQueuedThread(s, t); |
272 |
< |
t.interrupt(); |
273 |
< |
await(pleaseInterrupt); |
274 |
< |
waitForQueuedThread(s, t); |
275 |
< |
t.interrupt(); |
271 |
> |
for (int n = 2; n-->0; ) { |
272 |
> |
await(pleaseInterrupt); |
273 |
> |
assertThreadBlocks(t, acquirer.parkedState()); |
274 |
> |
t.interrupt(); |
275 |
> |
} |
276 |
> |
|
277 |
|
awaitTermination(t); |
278 |
|
} |
279 |
|
|
311 |
|
waitForQueuedThread(s, t2); |
312 |
|
t2.interrupt(); |
313 |
|
|
314 |
< |
assertThreadStaysAlive(t1); |
315 |
< |
assertTrue(t2.isAlive()); |
314 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
315 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
316 |
|
|
317 |
|
s.release(2); |
318 |
|
|
477 |
|
clone.release(); |
478 |
|
assertEquals(2, s.availablePermits()); |
479 |
|
assertEquals(1, clone.availablePermits()); |
480 |
+ |
assertFalse(s.hasQueuedThreads()); |
481 |
+ |
assertFalse(clone.hasQueuedThreads()); |
482 |
+ |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
483 |
|
|
484 |
< |
s = new Semaphore(0, fair); |
484 |
> |
{ |
485 |
> |
PublicSemaphore s = new PublicSemaphore(0, fair); |
486 |
|
Thread t = newStartedThread(new InterruptibleLockRunnable(s)); |
487 |
< |
waitForQueuedThreads(s); |
488 |
< |
clone = serialClone(s); |
487 |
> |
// waitForQueuedThreads(s); // suffers from "flicker", so ... |
488 |
> |
waitForQueuedThread(s, t); // ... we use this instead |
489 |
> |
PublicSemaphore clone = serialClone(s); |
490 |
|
assertEquals(fair, s.isFair()); |
491 |
|
assertEquals(fair, clone.isFair()); |
492 |
|
assertEquals(0, s.availablePermits()); |
497 |
|
awaitTermination(t); |
498 |
|
assertFalse(s.hasQueuedThreads()); |
499 |
|
assertFalse(clone.hasQueuedThreads()); |
500 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
500 |
> |
} |
501 |
|
} |
502 |
|
|
503 |
|
/** |
605 |
|
s.acquire(3); |
606 |
|
}}); |
607 |
|
|
608 |
< |
waitForQueuedThreads(s); |
608 |
> |
waitForQueuedThread(s, t1); |
609 |
|
|
610 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
611 |
|
public void realRun() throws InterruptedException { |
631 |
|
assertTrue(t2.isAlive()); |
632 |
|
s.release(); |
633 |
|
awaitTermination(t2); |
634 |
< |
} |
634 |
> |
} |
635 |
|
|
636 |
|
/** |
637 |
|
* toString indicates current number of permits |