ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SemaphoreTest.java (file contents):
Revision 1.38 by jsr166, Sun May 14 00:38:16 2017 UTC vs.
Revision 1.44 by jsr166, Mon Aug 12 06:21:11 2019 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import java.util.Collection;
12   import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.Semaphore;
14 + import java.util.concurrent.ThreadLocalRandom;
15  
15 import junit.framework.AssertionFailedError;
16   import junit.framework.Test;
17   import junit.framework.TestSuite;
18  
# Line 73 | Line 73 | public class SemaphoreTest extends JSR16
73          long startTime = System.nanoTime();
74          while (!s.hasQueuedThread(t)) {
75              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
76 <                throw new AssertionFailedError("timed out");
76 >                throw new AssertionError("timed out");
77              Thread.yield();
78          }
79          assertTrue(s.hasQueuedThreads());
# Line 87 | Line 87 | public class SemaphoreTest extends JSR16
87          long startTime = System.nanoTime();
88          while (!s.hasQueuedThreads()) {
89              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
90 <                throw new AssertionFailedError("timed out");
90 >                throw new AssertionError("timed out");
91              Thread.yield();
92          }
93      }
# Line 193 | Line 193 | public class SemaphoreTest extends JSR16
193      /**
194       * timed tryAcquire times out
195       */
196 <    public void testTryAcquire_timeout()      { testTryAcquire_timeout(false); }
197 <    public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); }
198 <    public void testTryAcquire_timeout(boolean fair) {
199 <        Semaphore s = new Semaphore(0, fair);
200 <        long startTime = System.nanoTime();
201 <        try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
202 <        catch (InterruptedException e) { threadUnexpectedException(e); }
196 >    public void testTryAcquire_timeout() throws InterruptedException {
197 >        final boolean fair = randomBoolean();
198 >        final Semaphore s = new Semaphore(0, fair);
199 >        final long startTime = System.nanoTime();
200 >        assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS));
201          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
202      }
203  
204      /**
205       * timed tryAcquire(N) times out
206       */
207 <    public void testTryAcquireN_timeout()      { testTryAcquireN_timeout(false); }
208 <    public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); }
209 <    public void testTryAcquireN_timeout(boolean fair) {
210 <        Semaphore s = new Semaphore(2, fair);
211 <        long startTime = System.nanoTime();
214 <        try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
215 <        catch (InterruptedException e) { threadUnexpectedException(e); }
207 >    public void testTryAcquireN_timeout() throws InterruptedException {
208 >        final boolean fair = randomBoolean();
209 >        final Semaphore s = new Semaphore(2, fair);
210 >        final long startTime = System.nanoTime();
211 >        assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS));
212          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
213      }
214  
# Line 272 | Line 268 | public class SemaphoreTest extends JSR16
268              assertThreadBlocks(t, acquirer.parkedState());
269              t.interrupt();
270          }
271 <        
271 >
272          awaitTermination(t);
273      }
274  
# Line 609 | Line 605 | public class SemaphoreTest extends JSR16
605          Thread t2 = newStartedThread(new CheckedRunnable() {
606              public void realRun() throws InterruptedException {
607                  // Will fail, even though 1 permit is available
608 <                assertFalse(s.tryAcquire(0L, MILLISECONDS));
609 <                assertFalse(s.tryAcquire(1, 0L, MILLISECONDS));
608 >                assertFalse(
609 >                    s.tryAcquire(randomExpiredTimeout(), randomTimeUnit()));
610 >                assertFalse(
611 >                    s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit()));
612  
613                  // untimed tryAcquire will barge and succeed
614                  assertTrue(s.tryAcquire());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines