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.50 by jsr166, Sat Apr 25 04:55:30 2015 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());
24 >        main(suite(), args);
25      }
26      public static Test suite() {
27          return new TestSuite(AbstractQueuedSynchronizerTest.class);
# 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 221 | Line 228 | public class AbstractQueuedSynchronizerT
228          case awaitUntil:
229              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
230              break;
231 +        default:
232 +            throw new AssertionError();
233          }
234      }
235  
# Line 1190 | Line 1199 | public class AbstractQueuedSynchronizerT
1199       */
1200      public void testTryAcquireSharedNanos_Timeout() {
1201          final BooleanLatch l = new BooleanLatch();
1202 +        final BooleanLatch observedQueued = new BooleanLatch();
1203          Thread t = newStartedThread(new CheckedRunnable() {
1204              public void realRun() throws InterruptedException {
1205                  assertFalse(l.isSignalled());
1206 <                long startTime = System.nanoTime();
1207 <                long nanos = MILLISECONDS.toNanos(timeoutMillis());
1208 <                assertFalse(l.tryAcquireSharedNanos(0, nanos));
1209 <                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1206 >                for (long millis = timeoutMillis();
1207 >                     !observedQueued.isSignalled();
1208 >                     millis *= 2) {
1209 >                    long nanos = MILLISECONDS.toNanos(millis);
1210 >                    long startTime = System.nanoTime();
1211 >                    assertFalse(l.tryAcquireSharedNanos(0, nanos));
1212 >                    assertTrue(millisElapsedSince(startTime) >= millis);
1213 >                }
1214                  assertFalse(l.isSignalled());
1215              }});
1216  
1217          waitForQueuedThread(l, t);
1218 +        observedQueued.releaseShared(0);
1219          assertFalse(l.isSignalled());
1220          awaitTermination(t);
1221          assertFalse(l.isSignalled());
1222      }
1223  
1224 +    /**
1225 +     * awaitNanos/timed await with 0 wait times out immediately
1226 +     */
1227 +    public void testAwait_Zero() throws InterruptedException {
1228 +        final Mutex sync = new Mutex();
1229 +        final ConditionObject c = sync.newCondition();
1230 +        sync.acquire();
1231 +        assertTrue(c.awaitNanos(0L) <= 0);
1232 +        assertFalse(c.await(0L, NANOSECONDS));
1233 +        sync.release();
1234 +    }
1235 +
1236 +    /**
1237 +     * awaitNanos/timed await with maximum negative wait times does not underflow
1238 +     */
1239 +    public void testAwait_NegativeInfinity() throws InterruptedException {
1240 +        final Mutex sync = new Mutex();
1241 +        final ConditionObject c = sync.newCondition();
1242 +        sync.acquire();
1243 +        assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1244 +        assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1245 +        sync.release();
1246 +    }
1247 +
1248   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines