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.28 by jsr166, Tue May 24 23:43:14 2011 UTC vs.
Revision 1.45 by jsr166, Tue Aug 13 00:46:27 2019 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
10 >
11 > import java.util.Collection;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.Semaphore;
14 >
15 > import junit.framework.Test;
16 > import junit.framework.TestSuite;
17  
18   public class SemaphoreTest extends JSR166TestCase {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run(suite());
20 >        main(suite(), args);
21      }
22      public static Test suite() {
23          return new TestSuite(SemaphoreTest.class);
# Line 69 | Line 72 | public class SemaphoreTest extends JSR16
72          long startTime = System.nanoTime();
73          while (!s.hasQueuedThread(t)) {
74              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
75 <                throw new AssertionFailedError("timed out");
75 >                throw new AssertionError("timed out");
76              Thread.yield();
77          }
78 <        assert(s.hasQueuedThreads());
78 >        assertTrue(s.hasQueuedThreads());
79          assertTrue(t.isAlive());
80      }
81  
# Line 83 | Line 86 | public class SemaphoreTest extends JSR16
86          long startTime = System.nanoTime();
87          while (!s.hasQueuedThreads()) {
88              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
89 <                throw new AssertionFailedError("timed out");
89 >                throw new AssertionError("timed out");
90              Thread.yield();
91          }
92      }
# Line 123 | Line 126 | public class SemaphoreTest extends JSR16
126              void acquire(Semaphore s) throws InterruptedException {
127                  assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS));
128              }
129 +            Thread.State parkedState() { return Thread.State.TIMED_WAITING; }
130          },
131          tryAcquireTimedN {
132              void acquire(Semaphore s, int permits) throws InterruptedException {
133                  assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS));
134              }
135 +            Thread.State parkedState() { return Thread.State.TIMED_WAITING; }
136          };
137  
138          // Intentionally meta-circular
# Line 141 | Line 146 | public class SemaphoreTest extends JSR16
146              for (int i = 0; i < permits; i++)
147                  acquire(s);
148          }
149 +        Thread.State parkedState() { return Thread.State.WAITING; }
150      }
151  
152      /**
# Line 186 | Line 192 | public class SemaphoreTest extends JSR16
192      /**
193       * timed tryAcquire times out
194       */
195 <    public void testTryAcquire_timeout()      { testTryAcquire_timeout(false); }
196 <    public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); }
197 <    public void testTryAcquire_timeout(boolean fair) {
198 <        Semaphore s = new Semaphore(0, fair);
199 <        long startTime = System.nanoTime();
194 <        try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
195 <        catch (InterruptedException e) { threadUnexpectedException(e); }
195 >    public void testTryAcquire_timeout() throws InterruptedException {
196 >        final boolean fair = randomBoolean();
197 >        final Semaphore s = new Semaphore(0, fair);
198 >        final long startTime = System.nanoTime();
199 >        assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS));
200          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
201      }
202  
203      /**
204       * timed tryAcquire(N) times out
205       */
206 <    public void testTryAcquireN_timeout()      { testTryAcquireN_timeout(false); }
207 <    public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); }
208 <    public void testTryAcquireN_timeout(boolean fair) {
209 <        Semaphore s = new Semaphore(2, fair);
210 <        long startTime = System.nanoTime();
207 <        try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
208 <        catch (InterruptedException e) { threadUnexpectedException(e); }
206 >    public void testTryAcquireN_timeout() throws InterruptedException {
207 >        final boolean fair = randomBoolean();
208 >        final Semaphore s = new Semaphore(2, fair);
209 >        final long startTime = System.nanoTime();
210 >        assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS));
211          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
212      }
213  
# Line 223 | Line 225 | public class SemaphoreTest extends JSR16
225      public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
226      public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
227          final PublicSemaphore s = new PublicSemaphore(0, fair);
228 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
228 >        final java.util.concurrent.CyclicBarrier pleaseInterrupt
229 >            = new java.util.concurrent.CyclicBarrier(2);
230          Thread t = newStartedThread(new CheckedRunnable() {
231              public void realRun() {
232                  // Interrupt before acquire
# Line 232 | Line 235 | public class SemaphoreTest extends JSR16
235                      acquirer.acquire(s);
236                      shouldThrow();
237                  } catch (InterruptedException success) {}
238 <
236 <                // Interrupt during acquire
237 <                try {
238 <                    acquirer.acquire(s);
239 <                    shouldThrow();
240 <                } catch (InterruptedException success) {}
238 >                assertFalse(Thread.interrupted());
239  
240                  // Interrupt before acquire(N)
241                  Thread.currentThread().interrupt();
# Line 245 | Line 243 | public class SemaphoreTest extends JSR16
243                      acquirer.acquire(s, 3);
244                      shouldThrow();
245                  } catch (InterruptedException success) {}
246 +                assertFalse(Thread.interrupted());
247  
248 <                anotherInterruptPlease.release();
248 >                // Interrupt during acquire
249 >                await(pleaseInterrupt);
250 >                try {
251 >                    acquirer.acquire(s);
252 >                    shouldThrow();
253 >                } catch (InterruptedException success) {}
254 >                assertFalse(Thread.interrupted());
255  
256                  // Interrupt during acquire(N)
257 +                await(pleaseInterrupt);
258                  try {
259                      acquirer.acquire(s, 3);
260                      shouldThrow();
261                  } catch (InterruptedException success) {}
262 +                assertFalse(Thread.interrupted());
263              }});
264  
265 <        waitForQueuedThread(s, t);
266 <        t.interrupt();
267 <        try {
268 <            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
269 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
270 <        waitForQueuedThread(s, t);
264 <        t.interrupt();
265 >        for (int n = 2; n-->0; ) {
266 >            await(pleaseInterrupt);
267 >            assertThreadBlocks(t, acquirer.parkedState());
268 >            t.interrupt();
269 >        }
270 >
271          awaitTermination(t);
272      }
273  
# Line 275 | Line 281 | public class SemaphoreTest extends JSR16
281      public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true,  AcquireMethod.acquireUninterruptiblyN); }
282      public void testUninterruptible(boolean fair, final AcquireMethod acquirer) {
283          final PublicSemaphore s = new PublicSemaphore(0, fair);
284 <        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
285 <        Thread t = newStartedThread(new CheckedRunnable() {
284 >        final Semaphore pleaseInterrupt = new Semaphore(-1, fair);
285 >
286 >        Thread t1 = newStartedThread(new CheckedRunnable() {
287              public void realRun() throws InterruptedException {
288                  // Interrupt before acquire
289 +                pleaseInterrupt.release();
290                  Thread.currentThread().interrupt();
291                  acquirer.acquire(s);
292                  assertTrue(Thread.interrupted());
293 +            }});
294  
295 <                anotherInterruptPlease.release();
296 <
295 >        Thread t2 = newStartedThread(new CheckedRunnable() {
296 >            public void realRun() throws InterruptedException {
297                  // Interrupt during acquire
298 +                pleaseInterrupt.release();
299                  acquirer.acquire(s);
300                  assertTrue(Thread.interrupted());
301              }});
302  
303 <        waitForQueuedThread(s, t);
304 <        assertThreadStaysAlive(t);
305 <        s.release();
303 >        await(pleaseInterrupt);
304 >        waitForQueuedThread(s, t1);
305 >        waitForQueuedThread(s, t2);
306 >        t2.interrupt();
307  
308 <        try {
309 <            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();
308 >        assertThreadBlocks(t1, Thread.State.WAITING);
309 >        assertThreadBlocks(t2, Thread.State.WAITING);
310  
311 <        awaitTermination(t);
311 >        s.release(2);
312 >
313 >        awaitTermination(t1);
314 >        awaitTermination(t2);
315      }
316  
317      /**
# Line 462 | Line 471 | public class SemaphoreTest extends JSR16
471              clone.release();
472              assertEquals(2, s.availablePermits());
473              assertEquals(1, clone.availablePermits());
474 +            assertFalse(s.hasQueuedThreads());
475 +            assertFalse(clone.hasQueuedThreads());
476 +        } catch (InterruptedException e) { threadUnexpectedException(e); }
477  
478 <            s = new Semaphore(0, fair);
478 >        {
479 >            PublicSemaphore s = new PublicSemaphore(0, fair);
480              Thread t = newStartedThread(new InterruptibleLockRunnable(s));
481 <            waitForQueuedThreads(s);
482 <            clone = serialClone(s);
481 >            // waitForQueuedThreads(s); // suffers from "flicker", so ...
482 >            waitForQueuedThread(s, t);  // ... we use this instead
483 >            PublicSemaphore clone = serialClone(s);
484              assertEquals(fair, s.isFair());
485              assertEquals(fair, clone.isFair());
486              assertEquals(0, s.availablePermits());
# Line 477 | Line 491 | public class SemaphoreTest extends JSR16
491              awaitTermination(t);
492              assertFalse(s.hasQueuedThreads());
493              assertFalse(clone.hasQueuedThreads());
494 <        } catch (InterruptedException e) { threadUnexpectedException(e); }
494 >        }
495      }
496  
497      /**
# Line 585 | Line 599 | public class SemaphoreTest extends JSR16
599                  s.acquire(3);
600              }});
601  
602 <        waitForQueuedThreads(s);
602 >        waitForQueuedThread(s, t1);
603  
604          Thread t2 = newStartedThread(new CheckedRunnable() {
605              public void realRun() throws InterruptedException {
606                  // Will fail, even though 1 permit is available
607 <                assertFalse(s.tryAcquire(0L, MILLISECONDS));
608 <                assertFalse(s.tryAcquire(1, 0L, MILLISECONDS));
607 >                assertFalse(
608 >                    s.tryAcquire(randomExpiredTimeout(), randomTimeUnit()));
609 >                assertFalse(
610 >                    s.tryAcquire(1, randomExpiredTimeout(), randomTimeUnit()));
611  
612                  // untimed tryAcquire will barge and succeed
613                  assertTrue(s.tryAcquire());
# Line 611 | Line 627 | public class SemaphoreTest extends JSR16
627          assertTrue(t2.isAlive());
628          s.release();
629          awaitTermination(t2);
630 <   }
630 >    }
631  
632      /**
633       * toString indicates current number of permits

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines