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.47 by jsr166, Wed Dec 31 19:05:42 2014 UTC vs.
Revision 1.58 by jsr166, Fri Sep 29 19:34:37 2017 UTC

# Line 19 | Line 19 | import junit.framework.AssertionFailedEr
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 33 | Line 34 | public class AbstractQueuedSynchronizerT
34       * methods/features of AbstractQueuedSynchronizer are tested via
35       * other test classes, including those for ReentrantLock,
36       * ReentrantReadWriteLock, and Semaphore.
37 +     *
38 +     * Unlike the javadoc sample, we don't track owner thread via
39 +     * AbstractOwnableSynchronizer methods.
40       */
41      static class Mutex extends AbstractQueuedSynchronizer {
42          /** An eccentric value for locked synchronizer state. */
# Line 40 | Line 44 | public class AbstractQueuedSynchronizerT
44  
45          static final int UNLOCKED = 0;
46  
47 +        /** Owner thread is untracked, so this is really just isLocked(). */
48          @Override public boolean isHeldExclusively() {
49              int state = getState();
50              assertTrue(state == UNLOCKED || state == LOCKED);
51              return state == LOCKED;
52          }
53  
54 <        @Override public boolean tryAcquire(int acquires) {
54 >        @Override protected boolean tryAcquire(int acquires) {
55              assertEquals(LOCKED, acquires);
56              return compareAndSetState(UNLOCKED, LOCKED);
57          }
58  
59 <        @Override public boolean tryRelease(int releases) {
59 >        @Override protected boolean tryRelease(int releases) {
60              if (getState() != LOCKED) throw new IllegalMonitorStateException();
61              assertEquals(LOCKED, releases);
62              setState(UNLOCKED);
# Line 82 | Line 87 | public class AbstractQueuedSynchronizerT
87              release(LOCKED);
88          }
89  
90 +        /** Faux-Implements Lock.newCondition(). */
91          public ConditionObject newCondition() {
92              return new ConditionObject();
93          }
94      }
95  
96      /**
97 <     * A simple latch class, to test shared mode.
97 >     * A minimal latch class, to test shared mode.
98       */
99      static class BooleanLatch extends AbstractQueuedSynchronizer {
100          public boolean isSignalled() { return getState() != 0; }
# Line 228 | Line 234 | public class AbstractQueuedSynchronizerT
234          case awaitUntil:
235              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
236              break;
237 +        default:
238 +            throw new AssertionError();
239          }
240      }
241  
# Line 236 | Line 244 | public class AbstractQueuedSynchronizerT
244       * default timeout duration).
245       */
246      void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
247 <        long timeoutMillis = timeoutMillis();
248 <        long startTime = System.nanoTime();
247 >        final long timeoutMillis = timeoutMillis();
248 >        final long startTime;
249          try {
250              switch (awaitMethod) {
251              case awaitTimed:
252 +                startTime = System.nanoTime();
253                  assertFalse(c.await(timeoutMillis, MILLISECONDS));
254 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
255                  break;
256              case awaitNanos:
257 +                startTime = System.nanoTime();
258                  long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
259                  long nanosRemaining = c.awaitNanos(nanosTimeout);
260                  assertTrue(nanosRemaining <= 0);
261 +                assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
262 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
263                  break;
264              case awaitUntil:
265 +                // We shouldn't assume that nanoTime and currentTimeMillis
266 +                // use the same time source, so don't use nanoTime here.
267 +                java.util.Date delayedDate = delayedDate(timeoutMillis);
268                  assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
269 +                assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
270                  break;
271              default:
272                  throw new UnsupportedOperationException();
273              }
274          } catch (InterruptedException ie) { threadUnexpectedException(ie); }
258        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
275      }
276  
277      /**
# Line 958 | Line 974 | public class AbstractQueuedSynchronizerT
974       */
975      public void testAwaitUninterruptibly() {
976          final Mutex sync = new Mutex();
977 <        final ConditionObject c = sync.newCondition();
977 >        final ConditionObject condition = sync.newCondition();
978          final BooleanLatch pleaseInterrupt = new BooleanLatch();
979          Thread t = newStartedThread(new CheckedRunnable() {
980              public void realRun() {
981                  sync.acquire();
982                  assertTrue(pleaseInterrupt.releaseShared(0));
983 <                c.awaitUninterruptibly();
983 >                condition.awaitUninterruptibly();
984                  assertTrue(Thread.interrupted());
985 <                assertHasWaitersLocked(sync, c, NO_THREADS);
985 >                assertHasWaitersLocked(sync, condition, NO_THREADS);
986                  sync.release();
987              }});
988  
989          pleaseInterrupt.acquireShared(0);
990          sync.acquire();
991 <        assertHasWaitersLocked(sync, c, t);
991 >        assertHasWaitersLocked(sync, condition, t);
992          sync.release();
993          t.interrupt();
994 <        assertHasWaitersUnlocked(sync, c, t);
995 <        assertThreadStaysAlive(t);
994 >        assertHasWaitersUnlocked(sync, condition, t);
995 >        assertThreadBlocks(t, Thread.State.WAITING);
996          sync.acquire();
997 <        assertHasWaitersLocked(sync, c, t);
997 >        assertHasWaitersLocked(sync, condition, t);
998          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
999 <        c.signal();
1000 <        assertHasWaitersLocked(sync, c, NO_THREADS);
999 >        condition.signal();
1000 >        assertHasWaitersLocked(sync, condition, NO_THREADS);
1001          assertHasExclusiveQueuedThreads(sync, t);
1002          sync.release();
1003          awaitTermination(t);
# Line 1124 | Line 1140 | public class AbstractQueuedSynchronizerT
1140  
1141          waitForQueuedThread(l, t);
1142          assertFalse(l.isSignalled());
1143 <        assertThreadStaysAlive(t);
1143 >        assertThreadBlocks(t, Thread.State.WAITING);
1144          assertHasSharedQueuedThreads(l, t);
1145          assertTrue(l.releaseShared(0));
1146          assertTrue(l.isSignalled());
# Line 1149 | Line 1165 | public class AbstractQueuedSynchronizerT
1165  
1166          waitForQueuedThread(l, t);
1167          assertFalse(l.isSignalled());
1168 <        assertThreadStaysAlive(t);
1168 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1169          assertTrue(l.releaseShared(0));
1170          assertTrue(l.isSignalled());
1171          awaitTermination(t);
# Line 1198 | Line 1214 | public class AbstractQueuedSynchronizerT
1214      public void testTryAcquireSharedNanos_Timeout() {
1215          final BooleanLatch l = new BooleanLatch();
1216          final BooleanLatch observedQueued = new BooleanLatch();
1201        final long timeoutMillis = timeoutMillis();
1217          Thread t = newStartedThread(new CheckedRunnable() {
1218              public void realRun() throws InterruptedException {
1219                  assertFalse(l.isSignalled());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines