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.29 by jsr166, Tue May 31 16:16:24 2011 UTC vs.
Revision 1.39 by jsr166, Sun May 14 03:31:53 2017 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.AssertionFailedError;
17 > import junit.framework.Test;
18 > import junit.framework.TestSuite;
19  
20   public class SemaphoreTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run(suite());
22 >        main(suite(), args);
23      }
24      public static Test suite() {
25          return new TestSuite(SemaphoreTest.class);
# Line 72 | Line 77 | public class SemaphoreTest extends JSR16
77                  throw new AssertionFailedError("timed out");
78              Thread.yield();
79          }
80 <        assert(s.hasQueuedThreads());
80 >        assertTrue(s.hasQueuedThreads());
81          assertTrue(t.isAlive());
82      }
83  
# Line 123 | 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 141 | 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 186 | 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);
201 <        long startTime = System.nanoTime();
197 >    public void testTryAcquire_timeout() {
198 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
199 >        final boolean fair = rnd.nextBoolean();
200 >        final Semaphore s = new Semaphore(0, fair);
201 >        final long startTime = System.nanoTime();
202          try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
203          catch (InterruptedException e) { threadUnexpectedException(e); }
204          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 199 | Line 207 | public class SemaphoreTest extends JSR16
207      /**
208       * timed tryAcquire(N) times out
209       */
210 <    public void testTryAcquireN_timeout()      { testTryAcquireN_timeout(false); }
211 <    public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); }
212 <    public void testTryAcquireN_timeout(boolean fair) {
213 <        Semaphore s = new Semaphore(2, fair);
214 <        long startTime = System.nanoTime();
210 >    public void testTryAcquireN_timeout() {
211 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
212 >        final boolean fair = rnd.nextBoolean();
213 >        final Semaphore s = new Semaphore(2, fair);
214 >        final long startTime = System.nanoTime();
215          try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
216          catch (InterruptedException e) { threadUnexpectedException(e); }
217          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 223 | Line 231 | public class SemaphoreTest extends JSR16
231      public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
232      public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
233          final PublicSemaphore s = new PublicSemaphore(0, fair);
234 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
234 >        final java.util.concurrent.CyclicBarrier pleaseInterrupt
235 >            = new java.util.concurrent.CyclicBarrier(2);
236          Thread t = newStartedThread(new CheckedRunnable() {
237              public void realRun() {
238                  // Interrupt before acquire
# Line 232 | Line 241 | public class SemaphoreTest extends JSR16
241                      acquirer.acquire(s);
242                      shouldThrow();
243                  } catch (InterruptedException success) {}
244 <
236 <                // Interrupt during acquire
237 <                try {
238 <                    acquirer.acquire(s);
239 <                    shouldThrow();
240 <                } catch (InterruptedException success) {}
244 >                assertFalse(Thread.interrupted());
245  
246                  // Interrupt before acquire(N)
247                  Thread.currentThread().interrupt();
# Line 245 | Line 249 | public class SemaphoreTest extends JSR16
249                      acquirer.acquire(s, 3);
250                      shouldThrow();
251                  } catch (InterruptedException success) {}
252 +                assertFalse(Thread.interrupted());
253  
254 <                anotherInterruptPlease.release();
254 >                // Interrupt during acquire
255 >                await(pleaseInterrupt);
256 >                try {
257 >                    acquirer.acquire(s);
258 >                    shouldThrow();
259 >                } catch (InterruptedException success) {}
260 >                assertFalse(Thread.interrupted());
261  
262                  // Interrupt during acquire(N)
263 +                await(pleaseInterrupt);
264                  try {
265                      acquirer.acquire(s, 3);
266                      shouldThrow();
267                  } catch (InterruptedException success) {}
268 +                assertFalse(Thread.interrupted());
269              }});
270  
271 <        waitForQueuedThread(s, t);
272 <        t.interrupt();
273 <        try {
274 <            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
275 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
276 <        waitForQueuedThread(s, t);
264 <        t.interrupt();
271 >        for (int n = 2; n-->0; ) {
272 >            await(pleaseInterrupt);
273 >            assertThreadBlocks(t, acquirer.parkedState());
274 >            t.interrupt();
275 >        }
276 >        
277          awaitTermination(t);
278      }
279  
# Line 275 | Line 287 | public class SemaphoreTest extends JSR16
287      public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true,  AcquireMethod.acquireUninterruptiblyN); }
288      public void testUninterruptible(boolean fair, final AcquireMethod acquirer) {
289          final PublicSemaphore s = new PublicSemaphore(0, fair);
290 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
291 <        Thread t = newStartedThread(new CheckedRunnable() {
290 >        final Semaphore pleaseInterrupt = new Semaphore(-1, fair);
291 >
292 >        Thread t1 = newStartedThread(new CheckedRunnable() {
293              public void realRun() throws InterruptedException {
294                  // Interrupt before acquire
295 +                pleaseInterrupt.release();
296                  Thread.currentThread().interrupt();
297                  acquirer.acquire(s);
298                  assertTrue(Thread.interrupted());
299 +            }});
300  
301 <                anotherInterruptPlease.release();
302 <
301 >        Thread t2 = newStartedThread(new CheckedRunnable() {
302 >            public void realRun() throws InterruptedException {
303                  // Interrupt during acquire
304 +                pleaseInterrupt.release();
305                  acquirer.acquire(s);
306                  assertTrue(Thread.interrupted());
307              }});
308  
309 <        waitForQueuedThread(s, t);
310 <        assertThreadStaysAlive(t);
311 <        s.release();
309 >        await(pleaseInterrupt);
310 >        waitForQueuedThread(s, t1);
311 >        waitForQueuedThread(s, t2);
312 >        t2.interrupt();
313  
314 <        try {
315 <            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
299 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
300 <        waitForQueuedThread(s, t);
301 <        t.interrupt();
302 <        assertThreadStaysAlive(t);
303 <        s.release();
314 >        assertThreadBlocks(t1, Thread.State.WAITING);
315 >        assertThreadBlocks(t2, Thread.State.WAITING);
316  
317 <        awaitTermination(t);
317 >        s.release(2);
318 >
319 >        awaitTermination(t1);
320 >        awaitTermination(t2);
321      }
322  
323      /**
# Line 462 | Line 477 | public class SemaphoreTest extends JSR16
477              clone.release();
478              assertEquals(2, s.availablePermits());
479              assertEquals(1, clone.availablePermits());
480 +            assertFalse(s.hasQueuedThreads());
481 +            assertFalse(clone.hasQueuedThreads());
482 +        } catch (InterruptedException e) { threadUnexpectedException(e); }
483  
484 <            s = new Semaphore(0, fair);
484 >        {
485 >            PublicSemaphore s = new PublicSemaphore(0, fair);
486              Thread t = newStartedThread(new InterruptibleLockRunnable(s));
487 <            waitForQueuedThreads(s);
488 <            clone = serialClone(s);
487 >            // waitForQueuedThreads(s); // suffers from "flicker", so ...
488 >            waitForQueuedThread(s, t);  // ... we use this instead
489 >            PublicSemaphore clone = serialClone(s);
490              assertEquals(fair, s.isFair());
491              assertEquals(fair, clone.isFair());
492              assertEquals(0, s.availablePermits());
# Line 477 | Line 497 | public class SemaphoreTest extends JSR16
497              awaitTermination(t);
498              assertFalse(s.hasQueuedThreads());
499              assertFalse(clone.hasQueuedThreads());
500 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
500 >        }
501      }
502  
503      /**
# Line 585 | Line 605 | public class SemaphoreTest extends JSR16
605                  s.acquire(3);
606              }});
607  
608 <        waitForQueuedThreads(s);
608 >        waitForQueuedThread(s, t1);
609  
610          Thread t2 = newStartedThread(new CheckedRunnable() {
611              public void realRun() throws InterruptedException {
# Line 611 | Line 631 | public class SemaphoreTest extends JSR16
631          assertTrue(t2.isAlive());
632          s.release();
633          awaitTermination(t2);
634 <   }
634 >    }
635  
636      /**
637       * toString indicates current number of permits

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines