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.53 by jsr166, Sun Jan 1 20:34:39 2017 UTC vs.
Revision 1.63 by jsr166, Tue Jan 23 20:44:11 2018 UTC

# Line 9 | Line 9
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10   import static java.util.concurrent.TimeUnit.NANOSECONDS;
11  
12 + import java.util.ArrayList;
13   import java.util.Arrays;
14   import java.util.Collection;
15   import java.util.HashSet;
16 + import java.util.concurrent.ThreadLocalRandom;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
18   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
19  
18 import junit.framework.AssertionFailedError;
20   import junit.framework.Test;
21   import junit.framework.TestSuite;
22  
23 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
24   public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
25      public static void main(String[] args) {
26          main(suite(), args);
# Line 33 | Line 35 | public class AbstractQueuedSynchronizerT
35       * methods/features of AbstractQueuedSynchronizer are tested via
36       * other test classes, including those for ReentrantLock,
37       * ReentrantReadWriteLock, and Semaphore.
38 +     *
39 +     * Unlike the javadoc sample, we don't track owner thread via
40 +     * AbstractOwnableSynchronizer methods.
41       */
42      static class Mutex extends AbstractQueuedSynchronizer {
43          /** An eccentric value for locked synchronizer state. */
# Line 40 | Line 45 | public class AbstractQueuedSynchronizerT
45  
46          static final int UNLOCKED = 0;
47  
48 +        /** Owner thread is untracked, so this is really just isLocked(). */
49          @Override public boolean isHeldExclusively() {
50              int state = getState();
51              assertTrue(state == UNLOCKED || state == LOCKED);
52              return state == LOCKED;
53          }
54  
55 <        @Override public boolean tryAcquire(int acquires) {
55 >        @Override protected boolean tryAcquire(int acquires) {
56              assertEquals(LOCKED, acquires);
57              return compareAndSetState(UNLOCKED, LOCKED);
58          }
59  
60 <        @Override public boolean tryRelease(int releases) {
60 >        @Override protected boolean tryRelease(int releases) {
61              if (getState() != LOCKED) throw new IllegalMonitorStateException();
62              assertEquals(LOCKED, releases);
63              setState(UNLOCKED);
# Line 82 | Line 88 | public class AbstractQueuedSynchronizerT
88              release(LOCKED);
89          }
90  
91 +        /** Faux-Implements Lock.newCondition(). */
92          public ConditionObject newCondition() {
93              return new ConditionObject();
94          }
95      }
96  
97      /**
98 <     * A simple latch class, to test shared mode.
98 >     * A minimal latch class, to test shared mode.
99       */
100      static class BooleanLatch extends AbstractQueuedSynchronizer {
101          public boolean isSignalled() { return getState() != 0; }
# Line 137 | Line 144 | public class AbstractQueuedSynchronizerT
144          long startTime = System.nanoTime();
145          while (!sync.isQueued(t)) {
146              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
147 <                throw new AssertionFailedError("timed out");
147 >                throw new AssertionError("timed out");
148              Thread.yield();
149          }
150          assertTrue(t.isAlive());
# Line 968 | Line 975 | public class AbstractQueuedSynchronizerT
975       */
976      public void testAwaitUninterruptibly() {
977          final Mutex sync = new Mutex();
978 <        final ConditionObject c = sync.newCondition();
978 >        final ConditionObject condition = sync.newCondition();
979          final BooleanLatch pleaseInterrupt = new BooleanLatch();
980          Thread t = newStartedThread(new CheckedRunnable() {
981              public void realRun() {
982                  sync.acquire();
983                  assertTrue(pleaseInterrupt.releaseShared(0));
984 <                c.awaitUninterruptibly();
984 >                condition.awaitUninterruptibly();
985                  assertTrue(Thread.interrupted());
986 <                assertHasWaitersLocked(sync, c, NO_THREADS);
986 >                assertHasWaitersLocked(sync, condition, NO_THREADS);
987                  sync.release();
988              }});
989  
990          pleaseInterrupt.acquireShared(0);
991          sync.acquire();
992 <        assertHasWaitersLocked(sync, c, t);
992 >        assertHasWaitersLocked(sync, condition, t);
993          sync.release();
994          t.interrupt();
995 <        assertHasWaitersUnlocked(sync, c, t);
996 <        assertThreadStaysAlive(t);
995 >        assertHasWaitersUnlocked(sync, condition, t);
996 >        assertThreadBlocks(t, Thread.State.WAITING);
997          sync.acquire();
998 <        assertHasWaitersLocked(sync, c, t);
998 >        assertHasWaitersLocked(sync, condition, t);
999          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1000 <        c.signal();
1001 <        assertHasWaitersLocked(sync, c, NO_THREADS);
1000 >        condition.signal();
1001 >        assertHasWaitersLocked(sync, condition, NO_THREADS);
1002          assertHasExclusiveQueuedThreads(sync, t);
1003          sync.release();
1004          awaitTermination(t);
# Line 1134 | Line 1141 | public class AbstractQueuedSynchronizerT
1141  
1142          waitForQueuedThread(l, t);
1143          assertFalse(l.isSignalled());
1144 <        assertThreadStaysAlive(t);
1144 >        assertThreadBlocks(t, Thread.State.WAITING);
1145          assertHasSharedQueuedThreads(l, t);
1146          assertTrue(l.releaseShared(0));
1147          assertTrue(l.isSignalled());
# Line 1159 | Line 1166 | public class AbstractQueuedSynchronizerT
1166  
1167          waitForQueuedThread(l, t);
1168          assertFalse(l.isSignalled());
1169 <        assertThreadStaysAlive(t);
1169 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1170          assertTrue(l.releaseShared(0));
1171          assertTrue(l.isSignalled());
1172          awaitTermination(t);
# Line 1253 | Line 1260 | public class AbstractQueuedSynchronizerT
1260          sync.release();
1261      }
1262  
1263 +    /**
1264 +     * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race
1265 +     * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck
1266 +     */
1267 +    public void testCancelCancelRace() throws InterruptedException {
1268 +        class Sync extends AbstractQueuedSynchronizer {
1269 +            protected boolean tryAcquire(int acquires) {
1270 +                return !hasQueuedPredecessors() && compareAndSetState(0, 1);
1271 +            }
1272 +            protected boolean tryRelease(int releases) {
1273 +                return compareAndSetState(1, 0);
1274 +            }
1275 +        }
1276 +
1277 +        Sync s = new Sync();
1278 +        s.acquire(1);           // acquire to force other threads to enqueue
1279 +
1280 +        // try to trigger double cancel race with two background threads
1281 +        ArrayList<Thread> threads = new ArrayList<>();
1282 +        Runnable failedAcquire = () -> {
1283 +            try {
1284 +                s.acquireInterruptibly(1);
1285 +                shouldThrow();
1286 +            } catch (InterruptedException expected) {}
1287 +        };
1288 +        for (int i = 0; i < 2; i++) {
1289 +            Thread thread = new Thread(failedAcquire);
1290 +            thread.start();
1291 +            threads.add(thread);
1292 +        }
1293 +        Thread.sleep(100);
1294 +        for (Thread thread : threads) thread.interrupt();
1295 +        for (Thread thread : threads) awaitTermination(thread);
1296 +
1297 +        s.release(1);
1298 +
1299 +        // no one holds lock now, we should be able to acquire
1300 +        if (!s.tryAcquire(1))
1301 +            throw new RuntimeException(
1302 +                String.format(
1303 +                    "Broken: hasQueuedPredecessors=%s hasQueuedThreads=%s queueLength=%d firstQueuedThread=%s",
1304 +                    s.hasQueuedPredecessors(),
1305 +                    s.hasQueuedThreads(),
1306 +                    s.getQueueLength(),
1307 +                    s.getFirstQueuedThread()));
1308 +    }
1309 +
1310 +    /**
1311 +     * Tests scenario for
1312 +     * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1313 +     */
1314 +    public void testInterruptedFailingAcquire() throws InterruptedException {
1315 +        final RuntimeException ex = new RuntimeException();
1316 +
1317 +        // A synchronizer only offering a choice of failure modes
1318 +        class Sync extends AbstractQueuedSynchronizer {
1319 +            boolean pleaseThrow;
1320 +            @Override protected boolean tryAcquire(int ignored) {
1321 +                if (pleaseThrow) throw ex;
1322 +                return false;
1323 +            }
1324 +            @Override protected int tryAcquireShared(int ignored) {
1325 +                if (pleaseThrow) throw ex;
1326 +                return -1;
1327 +            }
1328 +            @Override protected boolean tryRelease(int ignored) {
1329 +                return true;
1330 +            }
1331 +            @Override protected boolean tryReleaseShared(int ignored) {
1332 +                return true;
1333 +            }
1334 +        }
1335 +
1336 +        final Sync s = new Sync();
1337 +
1338 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1339 +            public void realRun() {
1340 +                try {
1341 +                    if (ThreadLocalRandom.current().nextBoolean())
1342 +                        s.acquire(1);
1343 +                    else
1344 +                        s.acquireShared(1);
1345 +                    shouldThrow();
1346 +                } catch (Throwable t) {
1347 +                    assertSame(ex, t);
1348 +                    assertTrue(Thread.interrupted());
1349 +                }
1350 +            }});
1351 +        waitForThreadToEnterWaitState(thread);
1352 +        assertSame(thread, s.getFirstQueuedThread());
1353 +        assertTrue(s.hasQueuedPredecessors());
1354 +        assertTrue(s.hasQueuedThreads());
1355 +        assertEquals(1, s.getQueueLength());
1356 +
1357 +        s.pleaseThrow = true;
1358 +        thread.interrupt();
1359 +        s.release(1);
1360 +        awaitTermination(thread);
1361 +    }
1362 +
1363   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines