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.69 by jsr166, Wed Aug 14 23:06:11 2019 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.atomic.AtomicBoolean;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
18   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
19  
# Line 1312 | Line 1313 | public class AbstractQueuedSynchronizerT
1313       * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck
1314       */
1315      public void testInterruptedFailingAcquire() throws Throwable {
1316 <        final RuntimeException ex = new RuntimeException();
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              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 1334 | Line 1346 | public class AbstractQueuedSynchronizerT
1346          }
1347  
1348          final Sync s = new Sync();
1349 <        final Action[] uninterruptibleAcquireMethods = {
1349 >        final boolean acquireInterruptibly = randomBoolean();
1350 >        final Action[] uninterruptibleAcquireActions = {
1351              () -> s.acquire(1),
1352              () -> s.acquireShared(1),
1340            // TODO: test interruptible acquire methods
1353          };
1354 <        final Action[] releaseMethods = {
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 acquireMethod
1366 <            = chooseRandomly(uninterruptibleAcquireMethods);
1367 <        final Action releaseMethod
1368 <            = chooseRandomly(releaseMethods);
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          //
# Line 1359 | Line 1379 | public class AbstractQueuedSynchronizerT
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 <                    acquireMethod.run();
1384 >                    acquireAction.run();
1385                      shouldThrow();
1386 <                } catch (Throwable t) {
1387 <                    assertSame(ex, t);
1386 >                } catch (InterruptedException possible) {
1387 >                    assertTrue(acquireInterruptibly);
1388 >                    assertFalse(Thread.interrupted());
1389 >                } catch (PleaseThrow possible) {
1390                      awaitInterrupted();
1391                  }
1392              }});
# Line 1373 | Line 1395 | public class AbstractQueuedSynchronizerT
1395              if (s.getFirstQueuedThread() == thread
1396                  && s.hasQueuedPredecessors()
1397                  && s.hasQueuedThreads()
1398 <                && s.getQueueLength() == 1)
1398 >                && s.getQueueLength() == 1
1399 >                && s.hasContended())
1400                  break;
1401              if (startTime == 0L)
1402                  startTime = System.nanoTime();
# Line 1388 | Line 1411 | public class AbstractQueuedSynchronizerT
1411          // release and interrupt, in random order
1412          if (randomBoolean()) {
1413              thread.interrupt();
1414 <            releaseMethod.run();
1414 >            releaseAction.run();
1415          } else {
1416 <            releaseMethod.run();
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