--- jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2017/11/27 23:06:53 1.60 +++ jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java 2019/08/15 16:01:30 1.71 @@ -13,11 +13,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; -import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; @@ -145,7 +143,7 @@ public class AbstractQueuedSynchronizerT long startTime = System.nanoTime(); while (!sync.isQueued(t)) { if (millisElapsedSince(startTime) > LONG_DELAY_MS) - throw new AssertionFailedError("timed out"); + throw new AssertionError("timed out"); Thread.yield(); } assertTrue(t.isAlive()); @@ -229,8 +227,8 @@ public class AbstractQueuedSynchronizerT assertTrue(c.await(timeoutMillis, MILLISECONDS)); break; case awaitNanos: - long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); - long nanosRemaining = c.awaitNanos(nanosTimeout); + long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(timeoutNanos); assertTrue(nanosRemaining > 0); break; case awaitUntil: @@ -257,8 +255,8 @@ public class AbstractQueuedSynchronizerT break; case awaitNanos: startTime = System.nanoTime(); - long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); - long nanosRemaining = c.awaitNanos(nanosTimeout); + long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); + long nanosRemaining = c.awaitNanos(timeoutNanos); assertTrue(nanosRemaining <= 0); assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); @@ -1262,11 +1260,10 @@ public class AbstractQueuedSynchronizerT } /** - * Disabled demo test for (unfixed as of 2017-11) * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck */ - public void XXXXtestCancelCancelRace() throws InterruptedException { + public void testCancelCancelRace() throws InterruptedException { class Sync extends AbstractQueuedSynchronizer { protected boolean tryAcquire(int acquires) { return !hasQueuedPredecessors() && compareAndSetState(0, 1); @@ -1285,7 +1282,7 @@ public class AbstractQueuedSynchronizerT try { s.acquireInterruptibly(1); shouldThrow(); - } catch (InterruptedException expected) {} + } catch (InterruptedException success) {} }; for (int i = 0; i < 2; i++) { Thread thread = new Thread(failedAcquire); @@ -1312,13 +1309,15 @@ public class AbstractQueuedSynchronizerT /** * Tests scenario for * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw + * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck */ - public void testInterruptedFailingAcquire() throws InterruptedException { - final RuntimeException ex = new RuntimeException(); + public void testInterruptedFailingAcquire() throws Throwable { + class PleaseThrow extends RuntimeException {} + final PleaseThrow ex = new PleaseThrow(); // A synchronizer only offering a choice of failure modes class Sync extends AbstractQueuedSynchronizer { - boolean pleaseThrow; + volatile boolean pleaseThrow; @Override protected boolean tryAcquire(int ignored) { if (pleaseThrow) throw ex; return false; @@ -1336,30 +1335,82 @@ public class AbstractQueuedSynchronizerT } final Sync s = new Sync(); - + final boolean acquireInterruptibly = randomBoolean(); + final Action[] uninterruptibleAcquireActions = { + () -> s.acquire(1), + () -> s.acquireShared(1), + }; + final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); + final Action[] interruptibleAcquireActions = { + () -> s.acquireInterruptibly(1), + () -> s.acquireSharedInterruptibly(1), + () -> s.tryAcquireNanos(1, nanosTimeout), + () -> s.tryAcquireSharedNanos(1, nanosTimeout), + }; + final Action[] releaseActions = { + () -> s.release(1), + () -> s.releaseShared(1), + }; + final Action acquireAction = acquireInterruptibly + ? chooseRandomly(interruptibleAcquireActions) + : chooseRandomly(uninterruptibleAcquireActions); + final Action releaseAction + = chooseRandomly(releaseActions); + + // From os_posix.cpp: + // + // NOTE that since there is no "lock" around the interrupt and + // is_interrupted operations, there is the possibility that the + // interrupted flag (in osThread) will be "false" but that the + // low-level events will be in the signaled state. This is + // intentional. The effect of this is that Object.wait() and + // LockSupport.park() will appear to have a spurious wakeup, which + // is allowed and not harmful, and the possibility is so rare that + // it is not worth the added complexity to add yet another lock. final Thread thread = newStartedThread(new CheckedRunnable() { - public void realRun() { + public void realRun() throws Throwable { try { - if (ThreadLocalRandom.current().nextBoolean()) - s.acquire(1); - else - s.acquireShared(1); + acquireAction.run(); shouldThrow(); - } catch (Throwable t) { - assertSame(ex, t); - assertTrue(Thread.interrupted()); + } catch (InterruptedException possible) { + assertTrue(acquireInterruptibly); + assertFalse(Thread.interrupted()); + } catch (PleaseThrow possible) { + awaitInterrupted(); } }}); - waitForThreadToEnterWaitState(thread); - assertSame(thread, s.getFirstQueuedThread()); - assertTrue(s.hasQueuedPredecessors()); - assertTrue(s.hasQueuedThreads()); - assertEquals(1, s.getQueueLength()); + for (long startTime = 0L;; ) { + waitForThreadToEnterWaitState(thread); + if (s.getFirstQueuedThread() == thread + && s.hasQueuedPredecessors() + && s.hasQueuedThreads() + && s.getQueueLength() == 1) + break; + if (startTime == 0L) + startTime = System.nanoTime(); + else if (millisElapsedSince(startTime) > LONG_DELAY_MS) + fail("timed out waiting for AQS state: " + + "thread state=" + thread.getState() + + ", queued threads=" + s.getQueuedThreads()); + Thread.yield(); + } s.pleaseThrow = true; - thread.interrupt(); - s.release(1); + // release and interrupt, in random order + if (randomBoolean()) { + thread.interrupt(); + releaseAction.run(); + } else { + releaseAction.run(); + thread.interrupt(); + } awaitTermination(thread); + + assertNull(s.getFirstQueuedThread()); + assertFalse(s.hasQueuedPredecessors()); + assertFalse(s.hasQueuedThreads()); + assertEquals(0, s.getQueueLength()); + assertTrue(s.getQueuedThreads().isEmpty()); } }