12 |
|
import java.util.concurrent.CountDownLatch; |
13 |
|
import java.util.concurrent.Semaphore; |
14 |
|
|
15 |
– |
import junit.framework.AssertionFailedError; |
15 |
|
import junit.framework.Test; |
16 |
|
import junit.framework.TestSuite; |
17 |
|
|
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 |
|
assertTrue(s.hasQueuedThreads()); |
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 |
|
} |
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(); |
201 |
< |
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } |
202 |
< |
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(); |
214 |
< |
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } |
215 |
< |
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 |
|
|
267 |
|
assertThreadBlocks(t, acquirer.parkedState()); |
268 |
|
t.interrupt(); |
269 |
|
} |
270 |
< |
|
270 |
> |
|
271 |
|
awaitTermination(t); |
272 |
|
} |
273 |
|
|
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()); |