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.38 by jsr166, Sun May 14 00:38:16 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 >
15 > import junit.framework.AssertionFailedError;
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 72 | Line 76 | public class SemaphoreTest extends JSR16
76                  throw new AssertionFailedError("timed out");
77              Thread.yield();
78          }
79 <        assert(s.hasQueuedThreads());
79 >        assertTrue(s.hasQueuedThreads());
80          assertTrue(t.isAlive());
81      }
82  
# 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 223 | Line 230 | public class SemaphoreTest extends JSR16
230      public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
231      public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
232          final PublicSemaphore s = new PublicSemaphore(0, fair);
233 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
233 >        final java.util.concurrent.CyclicBarrier pleaseInterrupt
234 >            = new java.util.concurrent.CyclicBarrier(2);
235          Thread t = newStartedThread(new CheckedRunnable() {
236              public void realRun() {
237                  // Interrupt before acquire
# Line 232 | Line 240 | public class SemaphoreTest extends JSR16
240                      acquirer.acquire(s);
241                      shouldThrow();
242                  } catch (InterruptedException success) {}
243 <
236 <                // Interrupt during acquire
237 <                try {
238 <                    acquirer.acquire(s);
239 <                    shouldThrow();
240 <                } catch (InterruptedException success) {}
243 >                assertFalse(Thread.interrupted());
244  
245                  // Interrupt before acquire(N)
246                  Thread.currentThread().interrupt();
# Line 245 | Line 248 | public class SemaphoreTest extends JSR16
248                      acquirer.acquire(s, 3);
249                      shouldThrow();
250                  } catch (InterruptedException success) {}
251 +                assertFalse(Thread.interrupted());
252  
253 <                anotherInterruptPlease.release();
253 >                // Interrupt during acquire
254 >                await(pleaseInterrupt);
255 >                try {
256 >                    acquirer.acquire(s);
257 >                    shouldThrow();
258 >                } catch (InterruptedException success) {}
259 >                assertFalse(Thread.interrupted());
260  
261                  // Interrupt during acquire(N)
262 +                await(pleaseInterrupt);
263                  try {
264                      acquirer.acquire(s, 3);
265                      shouldThrow();
266                  } catch (InterruptedException success) {}
267 +                assertFalse(Thread.interrupted());
268              }});
269  
270 <        waitForQueuedThread(s, t);
271 <        t.interrupt();
272 <        try {
273 <            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
274 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
275 <        waitForQueuedThread(s, t);
264 <        t.interrupt();
270 >        for (int n = 2; n-->0; ) {
271 >            await(pleaseInterrupt);
272 >            assertThreadBlocks(t, acquirer.parkedState());
273 >            t.interrupt();
274 >        }
275 >        
276          awaitTermination(t);
277      }
278  
# Line 275 | Line 286 | public class SemaphoreTest extends JSR16
286      public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true,  AcquireMethod.acquireUninterruptiblyN); }
287      public void testUninterruptible(boolean fair, final AcquireMethod acquirer) {
288          final PublicSemaphore s = new PublicSemaphore(0, fair);
289 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
290 <        Thread t = newStartedThread(new CheckedRunnable() {
289 >        final Semaphore pleaseInterrupt = new Semaphore(-1, fair);
290 >
291 >        Thread t1 = newStartedThread(new CheckedRunnable() {
292              public void realRun() throws InterruptedException {
293                  // Interrupt before acquire
294 +                pleaseInterrupt.release();
295                  Thread.currentThread().interrupt();
296                  acquirer.acquire(s);
297                  assertTrue(Thread.interrupted());
298 +            }});
299  
300 <                anotherInterruptPlease.release();
301 <
300 >        Thread t2 = newStartedThread(new CheckedRunnable() {
301 >            public void realRun() throws InterruptedException {
302                  // Interrupt during acquire
303 +                pleaseInterrupt.release();
304                  acquirer.acquire(s);
305                  assertTrue(Thread.interrupted());
306              }});
307  
308 <        waitForQueuedThread(s, t);
309 <        assertThreadStaysAlive(t);
310 <        s.release();
308 >        await(pleaseInterrupt);
309 >        waitForQueuedThread(s, t1);
310 >        waitForQueuedThread(s, t2);
311 >        t2.interrupt();
312  
313 <        try {
314 <            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();
313 >        assertThreadBlocks(t1, Thread.State.WAITING);
314 >        assertThreadBlocks(t2, Thread.State.WAITING);
315  
316 <        awaitTermination(t);
316 >        s.release(2);
317 >
318 >        awaitTermination(t1);
319 >        awaitTermination(t2);
320      }
321  
322      /**
# Line 462 | Line 476 | public class SemaphoreTest extends JSR16
476              clone.release();
477              assertEquals(2, s.availablePermits());
478              assertEquals(1, clone.availablePermits());
479 +            assertFalse(s.hasQueuedThreads());
480 +            assertFalse(clone.hasQueuedThreads());
481 +        } catch (InterruptedException e) { threadUnexpectedException(e); }
482  
483 <            s = new Semaphore(0, fair);
483 >        {
484 >            PublicSemaphore s = new PublicSemaphore(0, fair);
485              Thread t = newStartedThread(new InterruptibleLockRunnable(s));
486 <            waitForQueuedThreads(s);
487 <            clone = serialClone(s);
486 >            // waitForQueuedThreads(s); // suffers from "flicker", so ...
487 >            waitForQueuedThread(s, t);  // ... we use this instead
488 >            PublicSemaphore clone = serialClone(s);
489              assertEquals(fair, s.isFair());
490              assertEquals(fair, clone.isFair());
491              assertEquals(0, s.availablePermits());
# Line 477 | Line 496 | public class SemaphoreTest extends JSR16
496              awaitTermination(t);
497              assertFalse(s.hasQueuedThreads());
498              assertFalse(clone.hasQueuedThreads());
499 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
499 >        }
500      }
501  
502      /**
# Line 585 | Line 604 | public class SemaphoreTest extends JSR16
604                  s.acquire(3);
605              }});
606  
607 <        waitForQueuedThreads(s);
607 >        waitForQueuedThread(s, t1);
608  
609          Thread t2 = newStartedThread(new CheckedRunnable() {
610              public void realRun() throws InterruptedException {
# Line 611 | Line 630 | public class SemaphoreTest extends JSR16
630          assertTrue(t2.isAlive());
631          s.release();
632          awaitTermination(t2);
633 <   }
633 >    }
634  
635      /**
636       * toString indicates current number of permits

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines