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.31 by jsr166, Wed Dec 31 20:34:16 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.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   public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
23      public static void main(String[] args) {
24          junit.textui.TestRunner.run(suite());
# Line 117 | Line 124 | public class AbstractQueuedLongSynchroni
124      }
125  
126      /** A constant to clarify calls to checking methods below. */
127 <    final static Thread[] NO_THREADS = new Thread[0];
127 >    static final Thread[] NO_THREADS = new Thread[0];
128  
129      /**
130       * Spin-waits until sync.isQueued(t) becomes true.
# Line 195 | Line 202 | public class AbstractQueuedLongSynchroni
202                       new HashSet<Thread>(Arrays.asList(threads)));
203      }
204  
205 <    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil };
205 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
206  
207      /**
208       * Awaits condition using the specified AwaitMethod.
# Line 218 | Line 225 | public class AbstractQueuedLongSynchroni
225          case awaitUntil:
226              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
227              break;
228 +        default:
229 +            throw new AssertionError();
230          }
231      }
232  
# Line 944 | Line 953 | public class AbstractQueuedLongSynchroni
953      }
954  
955      /**
956 <     * awaitUninterruptibly doesn't abort on interrupt
956 >     * awaitUninterruptibly is uninterruptible
957       */
958      public void testAwaitUninterruptibly() {
959          final Mutex sync = new Mutex();
960          final ConditionObject c = sync.newCondition();
961 <        final BooleanLatch acquired = new BooleanLatch();
961 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
962          Thread t = newStartedThread(new CheckedRunnable() {
963              public void realRun() {
964                  sync.acquire();
965 <                assertTrue(acquired.releaseShared(0));
965 >                assertTrue(pleaseInterrupt.releaseShared(0));
966                  c.awaitUninterruptibly();
967                  assertTrue(Thread.interrupted());
968                  assertHasWaitersLocked(sync, c, NO_THREADS);
969                  sync.release();
970              }});
971  
972 <        acquired.acquireShared(0);
972 >        pleaseInterrupt.acquireShared(0);
973          sync.acquire();
974          assertHasWaitersLocked(sync, c, t);
975          sync.release();
# Line 987 | Line 996 | public class AbstractQueuedLongSynchroni
996      public void testInterruptible(final AwaitMethod awaitMethod) {
997          final Mutex sync = new Mutex();
998          final ConditionObject c = sync.newCondition();
999 <        final BooleanLatch acquired = new BooleanLatch();
999 >        final BooleanLatch pleaseInterrupt = new BooleanLatch();
1000          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1001              public void realRun() throws InterruptedException {
1002                  sync.acquire();
1003 <                assertTrue(acquired.releaseShared(0));
1003 >                assertTrue(pleaseInterrupt.releaseShared(0));
1004                  await(c, awaitMethod);
1005              }});
1006  
1007 <        acquired.acquireShared(0);
1007 >        pleaseInterrupt.acquireShared(0);
1008          t.interrupt();
1009          awaitTermination(t);
1010      }
# Line 1187 | Line 1196 | public class AbstractQueuedLongSynchroni
1196       */
1197      public void testTryAcquireSharedNanos_Timeout() {
1198          final BooleanLatch l = new BooleanLatch();
1199 +        final BooleanLatch observedQueued = new BooleanLatch();
1200 +        final long timeoutMillis = timeoutMillis();
1201          Thread t = newStartedThread(new CheckedRunnable() {
1202              public void realRun() throws InterruptedException {
1203                  assertFalse(l.isSignalled());
1204 <                long startTime = System.nanoTime();
1205 <                long nanos = MILLISECONDS.toNanos(timeoutMillis());
1206 <                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1207 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1204 >                for (long millis = timeoutMillis();
1205 >                     !observedQueued.isSignalled();
1206 >                     millis *= 2) {
1207 >                    long nanos = MILLISECONDS.toNanos(millis);
1208 >                    long startTime = System.nanoTime();
1209 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1210 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1211 >                }
1212                  assertFalse(l.isSignalled());
1213              }});
1214  
1215          waitForQueuedThread(l, t);
1216 +        observedQueued.releaseShared(0);
1217          assertFalse(l.isSignalled());
1218          awaitTermination(t);
1219          assertFalse(l.isSignalled());
1220      }
1221  
1222 +    /**
1223 +     * awaitNanos/timed await with 0 wait times out immediately
1224 +     */
1225 +    public void testAwait_Zero() throws InterruptedException {
1226 +        final Mutex sync = new Mutex();
1227 +        final ConditionObject c = sync.newCondition();
1228 +        sync.acquire();
1229 +        assertTrue(c.awaitNanos(0L) <= 0);
1230 +        assertFalse(c.await(0L, NANOSECONDS));
1231 +        sync.release();
1232 +    }
1233 +
1234 +    /**
1235 +     * awaitNanos/timed await with maximum negative wait times does not underflow
1236 +     */
1237 +    public void testAwait_NegativeInfinity() throws InterruptedException {
1238 +        final Mutex sync = new Mutex();
1239 +        final ConditionObject c = sync.newCondition();
1240 +        sync.acquire();
1241 +        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1242 +        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1243 +        sync.release();
1244 +    }
1245 +
1246   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines