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.Test; |
16 |
> |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class SemaphoreTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run(suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
|
return new TestSuite(SemaphoreTest.class); |
72 |
|
long startTime = System.nanoTime(); |
73 |
|
while (!s.hasQueuedThread(t)) { |
74 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
75 |
< |
throw new AssertionFailedError("timed out"); |
75 |
> |
throw new AssertionError("timed out"); |
76 |
|
Thread.yield(); |
77 |
|
} |
78 |
< |
assert(s.hasQueuedThreads()); |
78 |
> |
assertTrue(s.hasQueuedThreads()); |
79 |
|
assertTrue(t.isAlive()); |
80 |
|
} |
81 |
|
|
86 |
|
long startTime = System.nanoTime(); |
87 |
|
while (!s.hasQueuedThreads()) { |
88 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
89 |
< |
throw new AssertionFailedError("timed out"); |
89 |
> |
throw new AssertionError("timed out"); |
90 |
|
Thread.yield(); |
91 |
|
} |
92 |
|
} |
126 |
|
void acquire(Semaphore s) throws InterruptedException { |
127 |
|
assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS)); |
128 |
|
} |
129 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
130 |
|
}, |
131 |
|
tryAcquireTimedN { |
132 |
|
void acquire(Semaphore s, int permits) throws InterruptedException { |
133 |
|
assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS)); |
134 |
|
} |
135 |
+ |
Thread.State parkedState() { return Thread.State.TIMED_WAITING; } |
136 |
|
}; |
137 |
|
|
138 |
|
// Intentionally meta-circular |
146 |
|
for (int i = 0; i < permits; i++) |
147 |
|
acquire(s); |
148 |
|
} |
149 |
+ |
Thread.State parkedState() { return Thread.State.WAITING; } |
150 |
|
} |
151 |
|
|
152 |
|
/** |
192 |
|
/** |
193 |
|
* timed tryAcquire times out |
194 |
|
*/ |
195 |
< |
public void testTryAcquire_timeout() { testTryAcquire_timeout(false); } |
196 |
< |
public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); } |
197 |
< |
public void testTryAcquire_timeout(boolean fair) { |
198 |
< |
Semaphore s = new Semaphore(0, fair); |
199 |
< |
long startTime = System.nanoTime(); |
194 |
< |
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
195 |
< |
catch (InterruptedException e) { threadUnexpectedException(e); } |
195 |
> |
public void testTryAcquire_timeout() throws InterruptedException { |
196 |
> |
final boolean fair = randomBoolean(); |
197 |
> |
final Semaphore s = new Semaphore(0, fair); |
198 |
> |
final long startTime = System.nanoTime(); |
199 |
> |
assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); |
200 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* timed tryAcquire(N) times out |
205 |
|
*/ |
206 |
< |
public void testTryAcquireN_timeout() { testTryAcquireN_timeout(false); } |
207 |
< |
public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); } |
208 |
< |
public void testTryAcquireN_timeout(boolean fair) { |
209 |
< |
Semaphore s = new Semaphore(2, fair); |
210 |
< |
long startTime = System.nanoTime(); |
207 |
< |
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
208 |
< |
catch (InterruptedException e) { threadUnexpectedException(e); } |
206 |
> |
public void testTryAcquireN_timeout() throws InterruptedException { |
207 |
> |
final boolean fair = randomBoolean(); |
208 |
> |
final Semaphore s = new Semaphore(2, fair); |
209 |
> |
final long startTime = System.nanoTime(); |
210 |
> |
assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); |
211 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
212 |
|
} |
213 |
|
|
225 |
|
public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true, AcquireMethod.tryAcquireTimedN); } |
226 |
|
public void testInterruptible(boolean fair, final AcquireMethod acquirer) { |
227 |
|
final PublicSemaphore s = new PublicSemaphore(0, fair); |
228 |
< |
final Semaphore pleaseInterrupt = new Semaphore(0, fair); |
228 |
> |
final java.util.concurrent.CyclicBarrier pleaseInterrupt |
229 |
> |
= new java.util.concurrent.CyclicBarrier(2); |
230 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
231 |
|
public void realRun() { |
232 |
|
// Interrupt before acquire |
235 |
|
acquirer.acquire(s); |
236 |
|
shouldThrow(); |
237 |
|
} catch (InterruptedException success) {} |
238 |
< |
|
236 |
< |
// Interrupt during acquire |
237 |
< |
try { |
238 |
< |
acquirer.acquire(s); |
239 |
< |
shouldThrow(); |
240 |
< |
} catch (InterruptedException success) {} |
238 |
> |
assertFalse(Thread.interrupted()); |
239 |
|
|
240 |
|
// Interrupt before acquire(N) |
241 |
|
Thread.currentThread().interrupt(); |
243 |
|
acquirer.acquire(s, 3); |
244 |
|
shouldThrow(); |
245 |
|
} catch (InterruptedException success) {} |
246 |
+ |
assertFalse(Thread.interrupted()); |
247 |
|
|
248 |
< |
pleaseInterrupt.release(); |
248 |
> |
// Interrupt during acquire |
249 |
> |
await(pleaseInterrupt); |
250 |
> |
try { |
251 |
> |
acquirer.acquire(s); |
252 |
> |
shouldThrow(); |
253 |
> |
} catch (InterruptedException success) {} |
254 |
> |
assertFalse(Thread.interrupted()); |
255 |
|
|
256 |
|
// Interrupt during acquire(N) |
257 |
+ |
await(pleaseInterrupt); |
258 |
|
try { |
259 |
|
acquirer.acquire(s, 3); |
260 |
|
shouldThrow(); |
261 |
|
} catch (InterruptedException success) {} |
262 |
+ |
assertFalse(Thread.interrupted()); |
263 |
|
}}); |
264 |
|
|
265 |
< |
waitForQueuedThread(s, t); |
266 |
< |
t.interrupt(); |
267 |
< |
await(pleaseInterrupt); |
268 |
< |
waitForQueuedThread(s, t); |
269 |
< |
t.interrupt(); |
265 |
> |
for (int n = 2; n-->0; ) { |
266 |
> |
await(pleaseInterrupt); |
267 |
> |
assertThreadBlocks(t, acquirer.parkedState()); |
268 |
> |
t.interrupt(); |
269 |
> |
} |
270 |
> |
|
271 |
|
awaitTermination(t); |
272 |
|
} |
273 |
|
|
305 |
|
waitForQueuedThread(s, t2); |
306 |
|
t2.interrupt(); |
307 |
|
|
308 |
< |
assertThreadStaysAlive(t1); |
309 |
< |
assertTrue(t2.isAlive()); |
308 |
> |
assertThreadBlocks(t1, Thread.State.WAITING); |
309 |
> |
assertThreadBlocks(t2, Thread.State.WAITING); |
310 |
|
|
311 |
|
s.release(2); |
312 |
|
|
471 |
|
clone.release(); |
472 |
|
assertEquals(2, s.availablePermits()); |
473 |
|
assertEquals(1, clone.availablePermits()); |
474 |
+ |
assertFalse(s.hasQueuedThreads()); |
475 |
+ |
assertFalse(clone.hasQueuedThreads()); |
476 |
+ |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
477 |
|
|
478 |
< |
s = new Semaphore(0, fair); |
478 |
> |
{ |
479 |
> |
PublicSemaphore s = new PublicSemaphore(0, fair); |
480 |
|
Thread t = newStartedThread(new InterruptibleLockRunnable(s)); |
481 |
< |
waitForQueuedThreads(s); |
482 |
< |
clone = serialClone(s); |
481 |
> |
// waitForQueuedThreads(s); // suffers from "flicker", so ... |
482 |
> |
waitForQueuedThread(s, t); // ... we use this instead |
483 |
> |
PublicSemaphore clone = serialClone(s); |
484 |
|
assertEquals(fair, s.isFair()); |
485 |
|
assertEquals(fair, clone.isFair()); |
486 |
|
assertEquals(0, s.availablePermits()); |
491 |
|
awaitTermination(t); |
492 |
|
assertFalse(s.hasQueuedThreads()); |
493 |
|
assertFalse(clone.hasQueuedThreads()); |
494 |
< |
} catch (InterruptedException e) { threadUnexpectedException(e); } |
494 |
> |
} |
495 |
|
} |
496 |
|
|
497 |
|
/** |
599 |
|
s.acquire(3); |
600 |
|
}}); |
601 |
|
|
602 |
< |
waitForQueuedThreads(s); |
602 |
> |
waitForQueuedThread(s, t1); |
603 |
|
|
604 |
|
Thread t2 = newStartedThread(new CheckedRunnable() { |
605 |
|
public void realRun() throws InterruptedException { |
606 |
|
// Will fail, even though 1 permit is available |
607 |
< |
assertFalse(s.tryAcquire(0L, MILLISECONDS)); |
608 |
< |
assertFalse(s.tryAcquire(1, 0L, MILLISECONDS)); |
607 |
> |
assertFalse( |
608 |
> |
s.tryAcquire(randomExpiredTimeout(), randomTimeUnit())); |
609 |
> |
assertFalse( |
610 |
> |
s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit())); |
611 |
|
|
612 |
|
// untimed tryAcquire will barge and succeed |
613 |
|
assertTrue(s.tryAcquire()); |
627 |
|
assertTrue(t2.isAlive()); |
628 |
|
s.release(); |
629 |
|
awaitTermination(t2); |
630 |
< |
} |
630 |
> |
} |
631 |
|
|
632 |
|
/** |
633 |
|
* toString indicates current number of permits |