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.30 by jsr166, Fri Jun 3 05:07:14 2011 UTC vs.
Revision 1.44 by jsr166, Mon Aug 12 06:21:11 2019 UTC

# Line 6 | Line 6
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.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);
# Line 69 | 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 <        assert(s.hasQueuedThreads());
79 >        assertTrue(s.hasQueuedThreads());
80          assertTrue(t.isAlive());
81      }
82  
# Line 83 | 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 123 | Line 127 | public class SemaphoreTest extends JSR16
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
# Line 141 | Line 147 | public class SemaphoreTest extends JSR16
147              for (int i = 0; i < permits; i++)
148                  acquire(s);
149          }
150 +        Thread.State parkedState() { return Thread.State.WAITING; }
151      }
152  
153      /**
# Line 186 | 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();
194 <        try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
195 <        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();
207 <        try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
208 <        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 223 | Line 226 | public class SemaphoreTest extends JSR16
226      public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
227      public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
228          final PublicSemaphore s = new PublicSemaphore(0, fair);
229 <        final Semaphore pleaseInterrupt = new Semaphore(0, fair);
229 >        final java.util.concurrent.CyclicBarrier pleaseInterrupt
230 >            = new java.util.concurrent.CyclicBarrier(2);
231          Thread t = newStartedThread(new CheckedRunnable() {
232              public void realRun() {
233                  // Interrupt before acquire
# Line 232 | Line 236 | public class SemaphoreTest extends JSR16
236                      acquirer.acquire(s);
237                      shouldThrow();
238                  } catch (InterruptedException success) {}
239 <
236 <                // Interrupt during acquire
237 <                try {
238 <                    acquirer.acquire(s);
239 <                    shouldThrow();
240 <                } catch (InterruptedException success) {}
239 >                assertFalse(Thread.interrupted());
240  
241                  // Interrupt before acquire(N)
242                  Thread.currentThread().interrupt();
# Line 245 | Line 244 | public class SemaphoreTest extends JSR16
244                      acquirer.acquire(s, 3);
245                      shouldThrow();
246                  } catch (InterruptedException success) {}
247 +                assertFalse(Thread.interrupted());
248  
249 <                pleaseInterrupt.release();
249 >                // Interrupt during acquire
250 >                await(pleaseInterrupt);
251 >                try {
252 >                    acquirer.acquire(s);
253 >                    shouldThrow();
254 >                } catch (InterruptedException success) {}
255 >                assertFalse(Thread.interrupted());
256  
257                  // Interrupt during acquire(N)
258 +                await(pleaseInterrupt);
259                  try {
260                      acquirer.acquire(s, 3);
261                      shouldThrow();
262                  } catch (InterruptedException success) {}
263 +                assertFalse(Thread.interrupted());
264              }});
265  
266 <        waitForQueuedThread(s, t);
267 <        t.interrupt();
268 <        await(pleaseInterrupt);
269 <        waitForQueuedThread(s, t);
270 <        t.interrupt();
266 >        for (int n = 2; n-->0; ) {
267 >            await(pleaseInterrupt);
268 >            assertThreadBlocks(t, acquirer.parkedState());
269 >            t.interrupt();
270 >        }
271 >
272          awaitTermination(t);
273      }
274  
# Line 297 | Line 306 | public class SemaphoreTest extends JSR16
306          waitForQueuedThread(s, t2);
307          t2.interrupt();
308  
309 <        assertThreadStaysAlive(t1);
310 <        assertTrue(t2.isAlive());
309 >        assertThreadBlocks(t1, Thread.State.WAITING);
310 >        assertThreadBlocks(t2, Thread.State.WAITING);
311  
312          s.release(2);
313  
# Line 463 | Line 472 | public class SemaphoreTest extends JSR16
472              clone.release();
473              assertEquals(2, s.availablePermits());
474              assertEquals(1, clone.availablePermits());
475 +            assertFalse(s.hasQueuedThreads());
476 +            assertFalse(clone.hasQueuedThreads());
477 +        } catch (InterruptedException e) { threadUnexpectedException(e); }
478  
479 <            s = new Semaphore(0, fair);
479 >        {
480 >            PublicSemaphore s = new PublicSemaphore(0, fair);
481              Thread t = newStartedThread(new InterruptibleLockRunnable(s));
482 <            waitForQueuedThreads(s);
483 <            clone = serialClone(s);
482 >            // waitForQueuedThreads(s); // suffers from "flicker", so ...
483 >            waitForQueuedThread(s, t);  // ... we use this instead
484 >            PublicSemaphore clone = serialClone(s);
485              assertEquals(fair, s.isFair());
486              assertEquals(fair, clone.isFair());
487              assertEquals(0, s.availablePermits());
# Line 478 | Line 492 | public class SemaphoreTest extends JSR16
492              awaitTermination(t);
493              assertFalse(s.hasQueuedThreads());
494              assertFalse(clone.hasQueuedThreads());
495 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
495 >        }
496      }
497  
498      /**
# Line 586 | Line 600 | public class SemaphoreTest extends JSR16
600                  s.acquire(3);
601              }});
602  
603 <        waitForQueuedThreads(s);
603 >        waitForQueuedThread(s, t1);
604  
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());
# Line 612 | Line 628 | public class SemaphoreTest extends JSR16
628          assertTrue(t2.isAlive());
629          s.release();
630          awaitTermination(t2);
631 <   }
631 >    }
632  
633      /**
634       * toString indicates current number of permits

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines