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 |
> |
|
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); |
76 |
|
throw new AssertionFailedError("timed out"); |
77 |
|
Thread.yield(); |
78 |
|
} |
79 |
< |
assert(s.hasQueuedThreads()); |
79 |
> |
assertTrue(s.hasQueuedThreads()); |
80 |
|
assertTrue(t.isAlive()); |
81 |
|
} |
82 |
|
|
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 |
|
/** |
230 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
231 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
232 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
233 |
< |
final Semaphore pleaseInterrupt = new Semaphore(0, fair); |
233 |
> |
final java.util.concurrent.CyclicBarrier pleaseInterrupt |
234 |
> |
= new java.util.concurrent.CyclicBarrier(2); |
235 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
236 |
|
public void realRun() { |
237 |
|
// Interrupt before acquire |
240 |
|
acquirer.acquire(s); |
241 |
|
shouldThrow(); |
242 |
|
} catch (InterruptedException success) {} |
243 |
< |
|
236 |
< |
// Interrupt during acquire |
237 |
< |
try { |
238 |
< |
acquirer.acquire(s); |
239 |
< |
shouldThrow(); |
240 |
< |
} catch (InterruptedException success) {} |
243 |
> |
assertFalse(Thread.interrupted()); |
244 |
|
|
245 |
|
// Interrupt before acquire(N) |
246 |
|
Thread.currentThread().interrupt(); |
248 |
|
acquirer.acquire(s, 3); |
249 |
|
shouldThrow(); |
250 |
|
} catch (InterruptedException success) {} |
251 |
+ |
assertFalse(Thread.interrupted()); |
252 |
|
|
253 |
< |
pleaseInterrupt.release(); |
253 |
> |
// Interrupt during acquire |
254 |
> |
await(pleaseInterrupt); |
255 |
> |
try { |
256 |
> |
acquirer.acquire(s); |
257 |
> |
shouldThrow(); |
258 |
> |
} catch (InterruptedException success) {} |
259 |
> |
assertFalse(Thread.interrupted()); |
260 |
|
|
261 |
|
// Interrupt during acquire(N) |
262 |
+ |
await(pleaseInterrupt); |
263 |
|
try { |
264 |
|
acquirer.acquire(s, 3); |
265 |
|
shouldThrow(); |
266 |
|
} catch (InterruptedException success) {} |
267 |
+ |
assertFalse(Thread.interrupted()); |
268 |
|
}}); |
269 |
|
|
270 |
< |
waitForQueuedThread(s, t); |
271 |
< |
t.interrupt(); |
272 |
< |
await(pleaseInterrupt); |
273 |
< |
waitForQueuedThread(s, t); |
274 |
< |
t.interrupt(); |
270 |
> |
for (int n = 2; n-->0; ) { |
271 |
> |
await(pleaseInterrupt); |
272 |
> |
assertThreadBlocks(t, acquirer.parkedState()); |
273 |
> |
t.interrupt(); |
274 |
> |
} |
275 |
> |
|
276 |
|
awaitTermination(t); |
277 |
|
} |
278 |
|
|
310 |
|
waitForQueuedThread(s, t2); |
311 |
|
t2.interrupt(); |
312 |
|
|
313 |
< |
assertThreadStaysAlive(t1); |
314 |
< |
assertTrue(t2.isAlive()); |
313 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
314 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
315 |
|
|
316 |
|
s.release(2); |
317 |
|
|
476 |
|
clone.release(); |
477 |
|
assertEquals(2, s.availablePermits()); |
478 |
|
assertEquals(1, clone.availablePermits()); |
479 |
+ |
assertFalse(s.hasQueuedThreads()); |
480 |
+ |
assertFalse(clone.hasQueuedThreads()); |
481 |
+ |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
482 |
|
|
483 |
< |
s = new Semaphore(0, fair); |
483 |
> |
{ |
484 |
> |
PublicSemaphore s = new PublicSemaphore(0, fair); |
485 |
|
Thread t = newStartedThread(new InterruptibleLockRunnable(s)); |
486 |
< |
waitForQueuedThreads(s); |
487 |
< |
clone = serialClone(s); |
486 |
> |
// waitForQueuedThreads(s); // suffers from "flicker", so ... |
487 |
> |
waitForQueuedThread(s, t); // ... we use this instead |
488 |
> |
PublicSemaphore clone = serialClone(s); |
489 |
|
assertEquals(fair, s.isFair()); |
490 |
|
assertEquals(fair, clone.isFair()); |
491 |
|
assertEquals(0, s.availablePermits()); |
496 |
|
awaitTermination(t); |
497 |
|
assertFalse(s.hasQueuedThreads()); |
498 |
|
assertFalse(clone.hasQueuedThreads()); |
499 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
499 |
> |
} |
500 |
|
} |
501 |
|
|
502 |
|
/** |
604 |
|
s.acquire(3); |
605 |
|
}}); |
606 |
|
|
607 |
< |
waitForQueuedThreads(s); |
607 |
> |
waitForQueuedThread(s, t1); |
608 |
|
|
609 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
610 |
|
public void realRun() throws InterruptedException { |
630 |
|
assertTrue(t2.isAlive()); |
631 |
|
s.release(); |
632 |
|
awaitTermination(t2); |
633 |
< |
} |
633 |
> |
} |
634 |
|
|
635 |
|
/** |
636 |
|
* toString indicates current number of permits |