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.62 by dl, Sun Jan 7 23:05:44 2018 UTC vs.
Revision 1.73 by jsr166, Fri Aug 16 02:32:26 2019 UTC

# Line 13 | Line 13 | 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;
16 > import java.util.concurrent.atomic.AtomicBoolean;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
18   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
19  
20 import junit.framework.AssertionFailedError;
20   import junit.framework.Test;
21   import junit.framework.TestSuite;
22  
# Line 145 | 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 229 | Line 228 | public class AbstractQueuedSynchronizerT
228              assertTrue(c.await(timeoutMillis, MILLISECONDS));
229              break;
230          case awaitNanos:
231 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
232 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
231 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
232 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
233              assertTrue(nanosRemaining > 0);
234              break;
235          case awaitUntil:
# Line 257 | Line 256 | public class AbstractQueuedSynchronizerT
256                  break;
257              case awaitNanos:
258                  startTime = System.nanoTime();
259 <                long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
260 <                long nanosRemaining = c.awaitNanos(nanosTimeout);
259 >                long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
260 >                long nanosRemaining = c.awaitNanos(timeoutNanos);
261                  assertTrue(nanosRemaining <= 0);
262                  assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
263                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
# Line 1284 | Line 1283 | public class AbstractQueuedSynchronizerT
1283              try {
1284                  s.acquireInterruptibly(1);
1285                  shouldThrow();
1286 <            } catch (InterruptedException expected) {}
1286 >            } catch (InterruptedException success) {}
1287          };
1288          for (int i = 0; i < 2; i++) {
1289              Thread thread = new Thread(failedAcquire);
# Line 1311 | Line 1310 | public class AbstractQueuedSynchronizerT
1310      /**
1311       * Tests scenario for
1312       * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1313 +     * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck
1314       */
1315 <    public void testInterruptedFailingAcquire() throws InterruptedException {
1316 <        final RuntimeException ex = new RuntimeException();
1315 >    public void testInterruptedFailingAcquire() throws Throwable {
1316 >        class PleaseThrow extends RuntimeException {}
1317 >        final PleaseThrow ex = new PleaseThrow();
1318 >        final AtomicBoolean thrown = new AtomicBoolean();
1319  
1320          // A synchronizer only offering a choice of failure modes
1321          class Sync extends AbstractQueuedSynchronizer {
1322 <            boolean pleaseThrow;
1322 >            volatile boolean pleaseThrow;
1323 >            void maybeThrow() {
1324 >                if (pleaseThrow) {
1325 >                    // assert: tryAcquire methods can throw at most once
1326 >                    if (! thrown.compareAndSet(false, true))
1327 >                        throw new AssertionError();
1328 >                    throw ex;
1329 >                }
1330 >            }
1331 >
1332              @Override protected boolean tryAcquire(int ignored) {
1333 <                if (pleaseThrow) throw ex;
1333 >                maybeThrow();
1334                  return false;
1335              }
1336              @Override protected int tryAcquireShared(int ignored) {
1337 <                if (pleaseThrow) throw ex;
1337 >                maybeThrow();
1338                  return -1;
1339              }
1340              @Override protected boolean tryRelease(int ignored) {
# Line 1335 | Line 1346 | public class AbstractQueuedSynchronizerT
1346          }
1347  
1348          final Sync s = new Sync();
1349 <
1349 >        final boolean acquireInterruptibly = randomBoolean();
1350 >        final Action[] uninterruptibleAcquireActions = {
1351 >            () -> s.acquire(1),
1352 >            () -> s.acquireShared(1),
1353 >        };
1354 >        final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1355 >        final Action[] interruptibleAcquireActions = {
1356 >            () -> s.acquireInterruptibly(1),
1357 >            () -> s.acquireSharedInterruptibly(1),
1358 >            () -> s.tryAcquireNanos(1, nanosTimeout),
1359 >            () -> s.tryAcquireSharedNanos(1, nanosTimeout),
1360 >        };
1361 >        final Action[] releaseActions = {
1362 >            () -> s.release(1),
1363 >            () -> s.releaseShared(1),
1364 >        };
1365 >        final Action acquireAction = acquireInterruptibly
1366 >            ? chooseRandomly(interruptibleAcquireActions)
1367 >            : chooseRandomly(uninterruptibleAcquireActions);
1368 >        final Action releaseAction
1369 >            = chooseRandomly(releaseActions);
1370 >
1371 >        // From os_posix.cpp:
1372 >        //
1373 >        // NOTE that since there is no "lock" around the interrupt and
1374 >        // is_interrupted operations, there is the possibility that the
1375 >        // interrupted flag (in osThread) will be "false" but that the
1376 >        // low-level events will be in the signaled state. This is
1377 >        // intentional. The effect of this is that Object.wait() and
1378 >        // LockSupport.park() will appear to have a spurious wakeup, which
1379 >        // is allowed and not harmful, and the possibility is so rare that
1380 >        // it is not worth the added complexity to add yet another lock.
1381          final Thread thread = newStartedThread(new CheckedRunnable() {
1382 <            public void realRun() {
1382 >            public void realRun() throws Throwable {
1383                  try {
1384 <                    if (ThreadLocalRandom.current().nextBoolean())
1343 <                        s.acquire(1);
1344 <                    else
1345 <                        s.acquireShared(1);
1384 >                    acquireAction.run();
1385                      shouldThrow();
1386 <                } catch (Throwable t) {
1387 <                    assertSame(ex, t);
1388 <                    assertTrue(Thread.interrupted());
1386 >                } catch (InterruptedException possible) {
1387 >                    assertTrue(acquireInterruptibly);
1388 >                    assertFalse(Thread.interrupted());
1389 >                } catch (PleaseThrow possible) {
1390 >                    awaitInterrupted();
1391                  }
1392              }});
1393 <        waitForThreadToEnterWaitState(thread);
1394 <        assertSame(thread, s.getFirstQueuedThread());
1395 <        assertTrue(s.hasQueuedPredecessors());
1396 <        assertTrue(s.hasQueuedThreads());
1397 <        assertEquals(1, s.getQueueLength());
1393 >        for (long startTime = 0L;; ) {
1394 >            waitForThreadToEnterWaitState(thread);
1395 >            if (s.getFirstQueuedThread() == thread
1396 >                && s.hasQueuedPredecessors()
1397 >                && s.hasQueuedThreads()
1398 >                && s.getQueueLength() == 1
1399 >                && s.hasContended())
1400 >                break;
1401 >            if (startTime == 0L)
1402 >                startTime = System.nanoTime();
1403 >            else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1404 >                fail("timed out waiting for AQS state: "
1405 >                     + "thread state=" + thread.getState()
1406 >                     + ", queued threads=" + s.getQueuedThreads());
1407 >            Thread.yield();
1408 >        }
1409  
1410          s.pleaseThrow = true;
1411 <        thread.interrupt();
1412 <        s.release(1);
1411 >        // release and interrupt, in random order
1412 >        if (randomBoolean()) {
1413 >            thread.interrupt();
1414 >            releaseAction.run();
1415 >        } else {
1416 >            releaseAction.run();
1417 >            thread.interrupt();
1418 >        }
1419          awaitTermination(thread);
1420 +
1421 +        if (! acquireInterruptibly)
1422 +            assertTrue(thrown.get());
1423 +
1424 +        assertNull(s.getFirstQueuedThread());
1425 +        assertFalse(s.hasQueuedPredecessors());
1426 +        assertFalse(s.hasQueuedThreads());
1427 +        assertEquals(0, s.getQueueLength());
1428 +        assertTrue(s.getQueuedThreads().isEmpty());
1429 +        assertTrue(s.hasContended());
1430      }
1431  
1432   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines