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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.42 by jsr166, Fri Jun 3 05:07:14 2011 UTC vs.
Revision 1.57 by jsr166, Mon Jul 17 21:01:30 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.AbstractQueuedSynchronizer;
16   import java.util.concurrent.locks.AbstractQueuedSynchronizer.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 AbstractQueuedSynchronizerTest 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(AbstractQueuedSynchronizerTest.class);
# Line 121 | Line 129 | public class AbstractQueuedSynchronizerT
129      }
130  
131      /** A constant to clarify calls to checking methods below. */
132 <    final static Thread[] NO_THREADS = new Thread[0];
132 >    static final Thread[] NO_THREADS = new Thread[0];
133  
134      /**
135       * Spin-waits until sync.isQueued(t) becomes true.
# Line 198 | Line 206 | public class AbstractQueuedSynchronizerT
206                       new HashSet<Thread>(Arrays.asList(threads)));
207      }
208  
209 <    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
209 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
210  
211      /**
212       * Awaits condition using the specified AwaitMethod.
# Line 221 | Line 229 | public class AbstractQueuedSynchronizerT
229          case awaitUntil:
230              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
231              break;
232 +        default:
233 +            throw new AssertionError();
234          }
235      }
236  
# Line 229 | Line 239 | public class AbstractQueuedSynchronizerT
239       * default timeout duration).
240       */
241      void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
242 <        long timeoutMillis = timeoutMillis();
243 <        long startTime = System.nanoTime();
242 >        final long timeoutMillis = timeoutMillis();
243 >        final long startTime;
244          try {
245              switch (awaitMethod) {
246              case awaitTimed:
247 +                startTime = System.nanoTime();
248                  assertFalse(c.await(timeoutMillis, MILLISECONDS));
249 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
250                  break;
251              case awaitNanos:
252 +                startTime = System.nanoTime();
253                  long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
254                  long nanosRemaining = c.awaitNanos(nanosTimeout);
255                  assertTrue(nanosRemaining <= 0);
256 +                assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
257 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
258                  break;
259              case awaitUntil:
260 +                // We shouldn't assume that nanoTime and currentTimeMillis
261 +                // use the same time source, so don't use nanoTime here.
262 +                java.util.Date delayedDate = delayedDate(timeoutMillis);
263                  assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
264 +                assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
265                  break;
266              default:
267                  throw new UnsupportedOperationException();
268              }
269          } catch (InterruptedException ie) { threadUnexpectedException(ie); }
251        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
270      }
271  
272      /**
# Line 951 | Line 969 | public class AbstractQueuedSynchronizerT
969       */
970      public void testAwaitUninterruptibly() {
971          final Mutex sync = new Mutex();
972 <        final ConditionObject c = sync.newCondition();
972 >        final ConditionObject condition = sync.newCondition();
973          final BooleanLatch pleaseInterrupt = new BooleanLatch();
974          Thread t = newStartedThread(new CheckedRunnable() {
975              public void realRun() {
976                  sync.acquire();
977                  assertTrue(pleaseInterrupt.releaseShared(0));
978 <                c.awaitUninterruptibly();
978 >                condition.awaitUninterruptibly();
979                  assertTrue(Thread.interrupted());
980 <                assertHasWaitersLocked(sync, c, NO_THREADS);
980 >                assertHasWaitersLocked(sync, condition, NO_THREADS);
981                  sync.release();
982              }});
983  
984          pleaseInterrupt.acquireShared(0);
985          sync.acquire();
986 <        assertHasWaitersLocked(sync, c, t);
986 >        assertHasWaitersLocked(sync, condition, t);
987          sync.release();
988          t.interrupt();
989 <        assertHasWaitersUnlocked(sync, c, t);
990 <        assertThreadStaysAlive(t);
989 >        assertHasWaitersUnlocked(sync, condition, t);
990 >        assertThreadBlocks(t, Thread.State.WAITING);
991          sync.acquire();
992 <        assertHasWaitersLocked(sync, c, t);
992 >        assertHasWaitersLocked(sync, condition, t);
993          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
994 <        c.signal();
995 <        assertHasWaitersLocked(sync, c, NO_THREADS);
994 >        condition.signal();
995 >        assertHasWaitersLocked(sync, condition, NO_THREADS);
996          assertHasExclusiveQueuedThreads(sync, t);
997          sync.release();
998          awaitTermination(t);
# Line 1117 | Line 1135 | public class AbstractQueuedSynchronizerT
1135  
1136          waitForQueuedThread(l, t);
1137          assertFalse(l.isSignalled());
1138 <        assertThreadStaysAlive(t);
1138 >        assertThreadBlocks(t, Thread.State.WAITING);
1139          assertHasSharedQueuedThreads(l, t);
1140          assertTrue(l.releaseShared(0));
1141          assertTrue(l.isSignalled());
# Line 1142 | Line 1160 | public class AbstractQueuedSynchronizerT
1160  
1161          waitForQueuedThread(l, t);
1162          assertFalse(l.isSignalled());
1163 <        assertThreadStaysAlive(t);
1163 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1164          assertTrue(l.releaseShared(0));
1165          assertTrue(l.isSignalled());
1166          awaitTermination(t);
# Line 1190 | Line 1208 | public class AbstractQueuedSynchronizerT
1208       */
1209      public void testTryAcquireSharedNanos_Timeout() {
1210          final BooleanLatch l = new BooleanLatch();
1211 +        final BooleanLatch observedQueued = new BooleanLatch();
1212          Thread t = newStartedThread(new CheckedRunnable() {
1213              public void realRun() throws InterruptedException {
1214                  assertFalse(l.isSignalled());
1215 <                long startTime = System.nanoTime();
1216 <                long nanos = MILLISECONDS.toNanos(timeoutMillis());
1217 <                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1218 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1215 >                for (long millis = timeoutMillis();
1216 >                     !observedQueued.isSignalled();
1217 >                     millis *= 2) {
1218 >                    long nanos = MILLISECONDS.toNanos(millis);
1219 >                    long startTime = System.nanoTime();
1220 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1221 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1222 >                }
1223                  assertFalse(l.isSignalled());
1224              }});
1225  
1226          waitForQueuedThread(l, t);
1227 +        observedQueued.releaseShared(0);
1228          assertFalse(l.isSignalled());
1229          awaitTermination(t);
1230          assertFalse(l.isSignalled());
1231      }
1232  
1233 +    /**
1234 +     * awaitNanos/timed await with 0 wait times out immediately
1235 +     */
1236 +    public void testAwait_Zero() throws InterruptedException {
1237 +        final Mutex sync = new Mutex();
1238 +        final ConditionObject c = sync.newCondition();
1239 +        sync.acquire();
1240 +        assertTrue(c.awaitNanos(0L) <= 0);
1241 +        assertFalse(c.await(0L, NANOSECONDS));
1242 +        sync.release();
1243 +    }
1244 +
1245 +    /**
1246 +     * awaitNanos/timed await with maximum negative wait times does not underflow
1247 +     */
1248 +    public void testAwait_NegativeInfinity() throws InterruptedException {
1249 +        final Mutex sync = new Mutex();
1250 +        final ConditionObject c = sync.newCondition();
1251 +        sync.acquire();
1252 +        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1253 +        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1254 +        sync.release();
1255 +    }
1256 +
1257   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines