ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java (file contents):
Revision 1.24 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.42 by jsr166, Fri Sep 29 19:34:37 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 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 +
12 + import java.util.Arrays;
13 + import java.util.Collection;
14 + import java.util.HashSet;
15   import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
16   import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject;
17  
18 + import junit.framework.AssertionFailedError;
19 + import junit.framework.Test;
20 + import junit.framework.TestSuite;
21 +
22 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
23   public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(AbstractQueuedLongSynchronizerTest.class);
# Line 23 | Line 31 | public class AbstractQueuedLongSynchroni
31      /**
32       * A simple mutex class, adapted from the class javadoc.  Exclusive
33       * acquire tests exercise this as a sample user extension.
34 +     *
35 +     * Unlike the javadoc sample, we don't track owner thread via
36 +     * AbstractOwnableSynchronizer methods.
37       */
38      static class Mutex extends AbstractQueuedLongSynchronizer {
39          /** An eccentric value > 32 bits for locked synchronizer state. */
# Line 30 | Line 41 | public class AbstractQueuedLongSynchroni
41  
42          static final long UNLOCKED = 0;
43  
44 <        public boolean isHeldExclusively() {
44 >        /** Owner thread is untracked, so this is really just isLocked(). */
45 >        @Override public boolean isHeldExclusively() {
46              long state = getState();
47              assertTrue(state == UNLOCKED || state == LOCKED);
48              return state == LOCKED;
49          }
50  
51 <        public boolean tryAcquire(long acquires) {
51 >        @Override protected boolean tryAcquire(long acquires) {
52              assertEquals(LOCKED, acquires);
53              return compareAndSetState(UNLOCKED, LOCKED);
54          }
55  
56 <        public boolean tryRelease(long releases) {
56 >        @Override protected boolean tryRelease(long releases) {
57              if (getState() != LOCKED) throw new IllegalMonitorStateException();
58              setState(UNLOCKED);
59              return true;
# Line 71 | Line 83 | public class AbstractQueuedLongSynchroni
83              release(LOCKED);
84          }
85  
86 +        /** Faux-Implements Lock.newCondition(). */
87          public ConditionObject newCondition() {
88              return new ConditionObject();
89          }
90      }
91  
92      /**
93 <     * A simple latch class, to test shared mode.
93 >     * A minimal latch class, to test shared mode.
94       */
95      static class BooleanLatch extends AbstractQueuedLongSynchronizer {
96          public boolean isSignalled() { return getState() != 0; }
# Line 87 | Line 100 | public class AbstractQueuedLongSynchroni
100          }
101  
102          public boolean tryReleaseShared(long ignore) {
103 <            setState(1 << 62);
103 >            setState(1L << 62);
104              return true;
105          }
106      }
# Line 117 | Line 130 | public class AbstractQueuedLongSynchroni
130      }
131  
132      /** A constant to clarify calls to checking methods below. */
133 <    final static Thread[] NO_THREADS = new Thread[0];
133 >    static final Thread[] NO_THREADS = new Thread[0];
134  
135      /**
136       * Spin-waits until sync.isQueued(t) becomes true.
# Line 195 | Line 208 | public class AbstractQueuedLongSynchroni
208                       new HashSet<Thread>(Arrays.asList(threads)));
209      }
210  
211 <    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
211 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
212  
213      /**
214       * Awaits condition using the specified AwaitMethod.
# Line 218 | Line 231 | public class AbstractQueuedLongSynchroni
231          case awaitUntil:
232              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
233              break;
234 +        default:
235 +            throw new AssertionError();
236          }
237      }
238  
# Line 226 | Line 241 | public class AbstractQueuedLongSynchroni
241       * default timeout duration).
242       */
243      void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
244 <        long timeoutMillis = timeoutMillis();
245 <        long startTime = System.nanoTime();
244 >        final long timeoutMillis = timeoutMillis();
245 >        final long startTime;
246          try {
247              switch (awaitMethod) {
248              case awaitTimed:
249 +                startTime = System.nanoTime();
250                  assertFalse(c.await(timeoutMillis, MILLISECONDS));
251 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
252                  break;
253              case awaitNanos:
254 +                startTime = System.nanoTime();
255                  long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
256                  long nanosRemaining = c.awaitNanos(nanosTimeout);
257                  assertTrue(nanosRemaining <= 0);
258 +                assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
259 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
260                  break;
261              case awaitUntil:
262 +                // We shouldn't assume that nanoTime and currentTimeMillis
263 +                // use the same time source, so don't use nanoTime here.
264 +                java.util.Date delayedDate = delayedDate(timeoutMillis);
265                  assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
266 +                assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
267                  break;
268              default:
269                  throw new UnsupportedOperationException();
270              }
271          } catch (InterruptedException ie) { threadUnexpectedException(ie); }
248        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
272      }
273  
274      /**
# Line 944 | Line 967 | public class AbstractQueuedLongSynchroni
967      }
968  
969      /**
970 <     * awaitUninterruptibly doesn't abort on interrupt
970 >     * awaitUninterruptibly is uninterruptible
971       */
972      public void testAwaitUninterruptibly() {
973          final Mutex sync = new Mutex();
974 <        final ConditionObject c = sync.newCondition();
975 <        final BooleanLatch acquired = new BooleanLatch();
974 >        final ConditionObject condition = sync.newCondition();
975 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
976          Thread t = newStartedThread(new CheckedRunnable() {
977              public void realRun() {
978                  sync.acquire();
979 <                assertTrue(acquired.releaseShared(0));
980 <                c.awaitUninterruptibly();
979 >                assertTrue(pleaseInterrupt.releaseShared(0));
980 >                condition.awaitUninterruptibly();
981                  assertTrue(Thread.interrupted());
982 <                assertHasWaitersLocked(sync, c, NO_THREADS);
982 >                assertHasWaitersLocked(sync, condition, NO_THREADS);
983                  sync.release();
984              }});
985  
986 <        acquired.acquireShared(0);
986 >        pleaseInterrupt.acquireShared(0);
987          sync.acquire();
988 <        assertHasWaitersLocked(sync, c, t);
988 >        assertHasWaitersLocked(sync, condition, t);
989          sync.release();
990          t.interrupt();
991 <        assertHasWaitersUnlocked(sync, c, t);
992 <        assertThreadStaysAlive(t);
991 >        assertHasWaitersUnlocked(sync, condition, t);
992 >        assertThreadBlocks(t, Thread.State.WAITING);
993          sync.acquire();
994 <        assertHasWaitersLocked(sync, c, t);
994 >        assertHasWaitersLocked(sync, condition, t);
995          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
996 <        c.signal();
997 <        assertHasWaitersLocked(sync, c, NO_THREADS);
996 >        condition.signal();
997 >        assertHasWaitersLocked(sync, condition, NO_THREADS);
998          assertHasExclusiveQueuedThreads(sync, t);
999          sync.release();
1000          awaitTermination(t);
# Line 987 | Line 1010 | public class AbstractQueuedLongSynchroni
1010      public void testInterruptible(final AwaitMethod awaitMethod) {
1011          final Mutex sync = new Mutex();
1012          final ConditionObject c = sync.newCondition();
1013 <        final BooleanLatch acquired = new BooleanLatch();
1013 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
1014          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1015              public void realRun() throws InterruptedException {
1016                  sync.acquire();
1017 <                assertTrue(acquired.releaseShared(0));
1017 >                assertTrue(pleaseInterrupt.releaseShared(0));
1018                  await(c, awaitMethod);
1019              }});
1020  
1021 <        acquired.acquireShared(0);
1021 >        pleaseInterrupt.acquireShared(0);
1022          t.interrupt();
1023          awaitTermination(t);
1024      }
# Line 1114 | Line 1137 | public class AbstractQueuedLongSynchroni
1137  
1138          waitForQueuedThread(l, t);
1139          assertFalse(l.isSignalled());
1140 <        assertThreadStaysAlive(t);
1140 >        assertThreadBlocks(t, Thread.State.WAITING);
1141          assertHasSharedQueuedThreads(l, t);
1142          assertTrue(l.releaseShared(0));
1143          assertTrue(l.isSignalled());
# Line 1139 | Line 1162 | public class AbstractQueuedLongSynchroni
1162  
1163          waitForQueuedThread(l, t);
1164          assertFalse(l.isSignalled());
1165 <        assertThreadStaysAlive(t);
1165 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1166          assertTrue(l.releaseShared(0));
1167          assertTrue(l.isSignalled());
1168          awaitTermination(t);
# Line 1187 | Line 1210 | public class AbstractQueuedLongSynchroni
1210       */
1211      public void testTryAcquireSharedNanos_Timeout() {
1212          final BooleanLatch l = new BooleanLatch();
1213 +        final BooleanLatch observedQueued = new BooleanLatch();
1214          Thread t = newStartedThread(new CheckedRunnable() {
1215              public void realRun() throws InterruptedException {
1216                  assertFalse(l.isSignalled());
1217 <                long startTime = System.nanoTime();
1218 <                long nanos = MILLISECONDS.toNanos(timeoutMillis());
1219 <                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1220 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1217 >                for (long millis = timeoutMillis();
1218 >                     !observedQueued.isSignalled();
1219 >                     millis *= 2) {
1220 >                    long nanos = MILLISECONDS.toNanos(millis);
1221 >                    long startTime = System.nanoTime();
1222 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1223 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1224 >                }
1225                  assertFalse(l.isSignalled());
1226              }});
1227  
1228          waitForQueuedThread(l, t);
1229 +        observedQueued.releaseShared(0);
1230          assertFalse(l.isSignalled());
1231          awaitTermination(t);
1232          assertFalse(l.isSignalled());
1233      }
1234  
1235 +    /**
1236 +     * awaitNanos/timed await with 0 wait times out immediately
1237 +     */
1238 +    public void testAwait_Zero() throws InterruptedException {
1239 +        final Mutex sync = new Mutex();
1240 +        final ConditionObject c = sync.newCondition();
1241 +        sync.acquire();
1242 +        assertTrue(c.awaitNanos(0L) <= 0);
1243 +        assertFalse(c.await(0L, NANOSECONDS));
1244 +        sync.release();
1245 +    }
1246 +
1247 +    /**
1248 +     * awaitNanos/timed await with maximum negative wait times does not underflow
1249 +     */
1250 +    public void testAwait_NegativeInfinity() throws InterruptedException {
1251 +        final Mutex sync = new Mutex();
1252 +        final ConditionObject c = sync.newCondition();
1253 +        sync.acquire();
1254 +        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1255 +        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1256 +        sync.release();
1257 +    }
1258 +
1259   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines