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.34 by jsr166, Sat Apr 25 04:55:31 2015 UTC vs.
Revision 1.41 by jsr166, Sun May 14 04:18:31 2017 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  
16   import junit.framework.AssertionFailedError;
17   import junit.framework.Test;
# Line 127 | Line 128 | public class SemaphoreTest extends JSR16
128              void acquire(Semaphore s) throws InterruptedException {
129                  assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS));
130              }
131 +            Thread.State parkedState() { return Thread.State.TIMED_WAITING; }
132          },
133          tryAcquireTimedN {
134              void acquire(Semaphore s, int permits) throws InterruptedException {
135                  assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS));
136              }
137 +            Thread.State parkedState() { return Thread.State.TIMED_WAITING; }
138          };
139  
140          // Intentionally meta-circular
# Line 145 | Line 148 | public class SemaphoreTest extends JSR16
148              for (int i = 0; i < permits; i++)
149                  acquire(s);
150          }
151 +        Thread.State parkedState() { return Thread.State.WAITING; }
152      }
153  
154      /**
# Line 190 | Line 194 | public class SemaphoreTest extends JSR16
194      /**
195       * timed tryAcquire times out
196       */
197 <    public void testTryAcquire_timeout()      { testTryAcquire_timeout(false); }
198 <    public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); }
199 <    public void testTryAcquire_timeout(boolean fair) {
200 <        Semaphore s = new Semaphore(0, fair);
197 <        long startTime = System.nanoTime();
197 >    public void testTryAcquire_timeout() {
198 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
199 >        final Semaphore s = new Semaphore(0, fair);
200 >        final long startTime = System.nanoTime();
201          try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
202          catch (InterruptedException e) { threadUnexpectedException(e); }
203          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 203 | Line 206 | public class SemaphoreTest extends JSR16
206      /**
207       * timed tryAcquire(N) times out
208       */
209 <    public void testTryAcquireN_timeout()      { testTryAcquireN_timeout(false); }
210 <    public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); }
211 <    public void testTryAcquireN_timeout(boolean fair) {
212 <        Semaphore s = new Semaphore(2, fair);
210 <        long startTime = System.nanoTime();
209 >    public void testTryAcquireN_timeout() {
210 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
211 >        final Semaphore s = new Semaphore(2, fair);
212 >        final long startTime = System.nanoTime();
213          try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
214          catch (InterruptedException e) { threadUnexpectedException(e); }
215          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 227 | Line 229 | public class SemaphoreTest extends JSR16
229      public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
230      public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
231          final PublicSemaphore s = new PublicSemaphore(0, fair);
232 <        final Semaphore pleaseInterrupt = new Semaphore(0, fair);
232 >        final java.util.concurrent.CyclicBarrier pleaseInterrupt
233 >            = new java.util.concurrent.CyclicBarrier(2);
234          Thread t = newStartedThread(new CheckedRunnable() {
235              public void realRun() {
236                  // Interrupt before acquire
# Line 236 | Line 239 | public class SemaphoreTest extends JSR16
239                      acquirer.acquire(s);
240                      shouldThrow();
241                  } catch (InterruptedException success) {}
242 <
240 <                // Interrupt during acquire
241 <                try {
242 <                    acquirer.acquire(s);
243 <                    shouldThrow();
244 <                } catch (InterruptedException success) {}
242 >                assertFalse(Thread.interrupted());
243  
244                  // Interrupt before acquire(N)
245                  Thread.currentThread().interrupt();
# Line 249 | Line 247 | public class SemaphoreTest extends JSR16
247                      acquirer.acquire(s, 3);
248                      shouldThrow();
249                  } catch (InterruptedException success) {}
250 +                assertFalse(Thread.interrupted());
251  
252 <                pleaseInterrupt.release();
252 >                // Interrupt during acquire
253 >                await(pleaseInterrupt);
254 >                try {
255 >                    acquirer.acquire(s);
256 >                    shouldThrow();
257 >                } catch (InterruptedException success) {}
258 >                assertFalse(Thread.interrupted());
259  
260                  // Interrupt during acquire(N)
261 +                await(pleaseInterrupt);
262                  try {
263                      acquirer.acquire(s, 3);
264                      shouldThrow();
265                  } catch (InterruptedException success) {}
266 +                assertFalse(Thread.interrupted());
267              }});
268  
269 <        waitForQueuedThread(s, t);
270 <        t.interrupt();
271 <        await(pleaseInterrupt);
272 <        waitForQueuedThread(s, t);
273 <        t.interrupt();
269 >        for (int n = 2; n-->0; ) {
270 >            await(pleaseInterrupt);
271 >            assertThreadBlocks(t, acquirer.parkedState());
272 >            t.interrupt();
273 >        }
274 >
275          awaitTermination(t);
276      }
277  
# Line 301 | Line 309 | public class SemaphoreTest extends JSR16
309          waitForQueuedThread(s, t2);
310          t2.interrupt();
311  
312 <        assertThreadStaysAlive(t1);
313 <        assertTrue(t2.isAlive());
312 >        assertThreadBlocks(t1, Thread.State.WAITING);
313 >        assertThreadBlocks(t2, Thread.State.WAITING);
314  
315          s.release(2);
316  
# Line 467 | Line 475 | public class SemaphoreTest extends JSR16
475              clone.release();
476              assertEquals(2, s.availablePermits());
477              assertEquals(1, clone.availablePermits());
478 +            assertFalse(s.hasQueuedThreads());
479 +            assertFalse(clone.hasQueuedThreads());
480 +        } catch (InterruptedException e) { threadUnexpectedException(e); }
481  
482 <            s = new Semaphore(0, fair);
482 >        {
483 >            PublicSemaphore s = new PublicSemaphore(0, fair);
484              Thread t = newStartedThread(new InterruptibleLockRunnable(s));
485 <            waitForQueuedThreads(s);
486 <            clone = serialClone(s);
485 >            // waitForQueuedThreads(s); // suffers from "flicker", so ...
486 >            waitForQueuedThread(s, t);  // ... we use this instead
487 >            PublicSemaphore clone = serialClone(s);
488              assertEquals(fair, s.isFair());
489              assertEquals(fair, clone.isFair());
490              assertEquals(0, s.availablePermits());
# Line 482 | Line 495 | public class SemaphoreTest extends JSR16
495              awaitTermination(t);
496              assertFalse(s.hasQueuedThreads());
497              assertFalse(clone.hasQueuedThreads());
498 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
498 >        }
499      }
500  
501      /**
# Line 590 | Line 603 | public class SemaphoreTest extends JSR16
603                  s.acquire(3);
604              }});
605  
606 <        waitForQueuedThreads(s);
606 >        waitForQueuedThread(s, t1);
607  
608          Thread t2 = newStartedThread(new CheckedRunnable() {
609              public void realRun() throws InterruptedException {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines