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.41 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.47 by jsr166, Wed Dec 31 19:05:42 2014 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   public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
23      public static void main(String[] args) {
24          junit.textui.TestRunner.run(suite());
# Line 121 | Line 128 | public class AbstractQueuedSynchronizerT
128      }
129  
130      /** A constant to clarify calls to checking methods below. */
131 <    final static Thread[] NO_THREADS = new Thread[0];
131 >    static final Thread[] NO_THREADS = new Thread[0];
132  
133      /**
134       * Spin-waits until sync.isQueued(t) becomes true.
# Line 198 | Line 205 | public class AbstractQueuedSynchronizerT
205                       new HashSet<Thread>(Arrays.asList(threads)));
206      }
207  
208 <    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
208 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
209  
210      /**
211       * Awaits condition using the specified AwaitMethod.
# Line 947 | Line 954 | public class AbstractQueuedSynchronizerT
954      }
955  
956      /**
957 <     * awaitUninterruptibly doesn't abort on interrupt
957 >     * awaitUninterruptibly is uninterruptible
958       */
959      public void testAwaitUninterruptibly() {
960          final Mutex sync = new Mutex();
961          final ConditionObject c = sync.newCondition();
962 <        final BooleanLatch acquired = new BooleanLatch();
962 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
963          Thread t = newStartedThread(new CheckedRunnable() {
964              public void realRun() {
965                  sync.acquire();
966 <                assertTrue(acquired.releaseShared(0));
966 >                assertTrue(pleaseInterrupt.releaseShared(0));
967                  c.awaitUninterruptibly();
968                  assertTrue(Thread.interrupted());
969                  assertHasWaitersLocked(sync, c, NO_THREADS);
970                  sync.release();
971              }});
972  
973 <        acquired.acquireShared(0);
973 >        pleaseInterrupt.acquireShared(0);
974          sync.acquire();
975          assertHasWaitersLocked(sync, c, t);
976          sync.release();
# Line 990 | Line 997 | public class AbstractQueuedSynchronizerT
997      public void testInterruptible(final AwaitMethod awaitMethod) {
998          final Mutex sync = new Mutex();
999          final ConditionObject c = sync.newCondition();
1000 <        final BooleanLatch acquired = new BooleanLatch();
1000 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
1001          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1002              public void realRun() throws InterruptedException {
1003                  sync.acquire();
1004 <                assertTrue(acquired.releaseShared(0));
1004 >                assertTrue(pleaseInterrupt.releaseShared(0));
1005                  await(c, awaitMethod);
1006              }});
1007  
1008 <        acquired.acquireShared(0);
1008 >        pleaseInterrupt.acquireShared(0);
1009          t.interrupt();
1010          awaitTermination(t);
1011      }
# Line 1190 | Line 1197 | public class AbstractQueuedSynchronizerT
1197       */
1198      public void testTryAcquireSharedNanos_Timeout() {
1199          final BooleanLatch l = new BooleanLatch();
1200 +        final BooleanLatch observedQueued = new BooleanLatch();
1201 +        final long timeoutMillis = timeoutMillis();
1202          Thread t = newStartedThread(new CheckedRunnable() {
1203              public void realRun() throws InterruptedException {
1204                  assertFalse(l.isSignalled());
1205 <                long startTime = System.nanoTime();
1206 <                long nanos = MILLISECONDS.toNanos(timeoutMillis());
1207 <                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1208 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1205 >                for (long millis = timeoutMillis();
1206 >                     !observedQueued.isSignalled();
1207 >                     millis *= 2) {
1208 >                    long nanos = MILLISECONDS.toNanos(millis);
1209 >                    long startTime = System.nanoTime();
1210 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1211 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1212 >                }
1213                  assertFalse(l.isSignalled());
1214              }});
1215  
1216          waitForQueuedThread(l, t);
1217 +        observedQueued.releaseShared(0);
1218          assertFalse(l.isSignalled());
1219          awaitTermination(t);
1220          assertFalse(l.isSignalled());
1221      }
1222  
1223 +    /**
1224 +     * awaitNanos/timed await with 0 wait times out immediately
1225 +     */
1226 +    public void testAwait_Zero() throws InterruptedException {
1227 +        final Mutex sync = new Mutex();
1228 +        final ConditionObject c = sync.newCondition();
1229 +        sync.acquire();
1230 +        assertTrue(c.awaitNanos(0L) <= 0);
1231 +        assertFalse(c.await(0L, NANOSECONDS));
1232 +        sync.release();
1233 +    }
1234 +
1235 +    /**
1236 +     * awaitNanos/timed await with maximum negative wait times does not underflow
1237 +     */
1238 +    public void testAwait_NegativeInfinity() throws InterruptedException {
1239 +        final Mutex sync = new Mutex();
1240 +        final ConditionObject c = sync.newCondition();
1241 +        sync.acquire();
1242 +        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1243 +        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1244 +        sync.release();
1245 +    }
1246 +
1247   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines