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.59 by jsr166, Mon Nov 27 03:25:42 2017 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  
19 import junit.framework.AssertionFailedError;
20   import junit.framework.Test;
21   import junit.framework.TestSuite;
22  
# Line 144 | 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 228 | 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 256 | 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 1261 | Line 1261 | public class AbstractQueuedSynchronizerT
1261      }
1262  
1263      /**
1264     * Disabled demo test for (unfixed as of 2017-11)
1264       * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race
1265       * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck
1266       */
1267 <    public void XXXXtestCancelCancelRace() throws InterruptedException {
1267 >    public void testCancelCancelRace() throws InterruptedException {
1268          class Sync extends AbstractQueuedSynchronizer {
1269 <            private static final long serialVersionUID = 1L;
1271 <
1272 <            public boolean tryAcquire(int acquires) {
1269 >            protected boolean tryAcquire(int acquires) {
1270                  return !hasQueuedPredecessors() && compareAndSetState(0, 1);
1271              }
1275
1272              protected boolean tryRelease(int releases) {
1273                  return compareAndSetState(1, 0);
1274              }
# Line 1282 | Line 1278 | public class AbstractQueuedSynchronizerT
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> ts = new ArrayList<>();
1281 >        ArrayList<Thread> threads = new ArrayList<>();
1282          Runnable failedAcquire = () -> {
1283              try {
1284                  s.acquireInterruptibly(1);
1285 <                throw new AssertionError();
1286 <            } catch (InterruptedException expected) {}
1285 >                shouldThrow();
1286 >            } catch (InterruptedException success) {}
1287          };
1288          for (int i = 0; i < 2; i++) {
1289 <            Thread t = new Thread(failedAcquire);
1290 <            t.start();
1291 <            ts.add(t);
1289 >            Thread thread = new Thread(failedAcquire);
1290 >            thread.start();
1291 >            threads.add(thread);
1292          }
1293          Thread.sleep(100);
1294 <        for (Thread t : ts) t.interrupt();
1295 <        for (Thread t : ts) t.join();
1294 >        for (Thread thread : threads) thread.interrupt();
1295 >        for (Thread thread : threads) awaitTermination(thread);
1296  
1297          s.release(1);
1298  
# Line 1311 | Line 1307 | public class AbstractQueuedSynchronizerT
1307                      s.getFirstQueuedThread()));
1308      }
1309  
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 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 +            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 +                maybeThrow();
1334 +                return false;
1335 +            }
1336 +            @Override protected int tryAcquireShared(int ignored) {
1337 +                maybeThrow();
1338 +                return -1;
1339 +            }
1340 +            @Override protected boolean tryRelease(int ignored) {
1341 +                return true;
1342 +            }
1343 +            @Override protected boolean tryReleaseShared(int ignored) {
1344 +                return true;
1345 +            }
1346 +        }
1347 +
1348 +        final Sync s = new Sync();
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() throws Throwable {
1383 +                try {
1384 +                    acquireAction.run();
1385 +                    shouldThrow();
1386 +                } catch (InterruptedException possible) {
1387 +                    assertTrue(acquireInterruptibly);
1388 +                    assertFalse(Thread.interrupted());
1389 +                } catch (PleaseThrow possible) {
1390 +                    awaitInterrupted();
1391 +                }
1392 +            }});
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 +        // 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