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.62 by dl, Sun Jan 7 23:05:44 2018 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;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
18   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
19  
# Line 1261 | Line 1262 | public class AbstractQueuedSynchronizerT
1262      }
1263  
1264      /**
1264     * Disabled demo test for (unfixed as of 2017-11)
1265       * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race
1266       * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck
1267       */
1268 <    public void XXXXtestCancelCancelRace() throws InterruptedException {
1268 >    public void testCancelCancelRace() throws InterruptedException {
1269          class Sync extends AbstractQueuedSynchronizer {
1270 <            private static final long serialVersionUID = 1L;
1271 <
1272 <            public boolean tryAcquire(int acquires) {
1270 >            protected boolean tryAcquire(int acquires) {
1271                  return !hasQueuedPredecessors() && compareAndSetState(0, 1);
1272              }
1275
1273              protected boolean tryRelease(int releases) {
1274                  return compareAndSetState(1, 0);
1275              }
# Line 1282 | Line 1279 | public class AbstractQueuedSynchronizerT
1279          s.acquire(1);           // acquire to force other threads to enqueue
1280  
1281          // try to trigger double cancel race with two background threads
1282 <        ArrayList<Thread> ts = new ArrayList<>();
1282 >        ArrayList<Thread> threads = new ArrayList<>();
1283          Runnable failedAcquire = () -> {
1284              try {
1285                  s.acquireInterruptibly(1);
1286 <                throw new AssertionError();
1286 >                shouldThrow();
1287              } catch (InterruptedException expected) {}
1288          };
1289          for (int i = 0; i < 2; i++) {
1290 <            Thread t = new Thread(failedAcquire);
1291 <            t.start();
1292 <            ts.add(t);
1290 >            Thread thread = new Thread(failedAcquire);
1291 >            thread.start();
1292 >            threads.add(thread);
1293          }
1294          Thread.sleep(100);
1295 <        for (Thread t : ts) t.interrupt();
1296 <        for (Thread t : ts) t.join();
1295 >        for (Thread thread : threads) thread.interrupt();
1296 >        for (Thread thread : threads) awaitTermination(thread);
1297  
1298          s.release(1);
1299  
# Line 1311 | Line 1308 | public class AbstractQueuedSynchronizerT
1308                      s.getFirstQueuedThread()));
1309      }
1310  
1311 +    /**
1312 +     * Tests scenario for
1313 +     * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1314 +     */
1315 +    public void testInterruptedFailingAcquire() throws InterruptedException {
1316 +        final RuntimeException ex = new RuntimeException();
1317 +
1318 +        // A synchronizer only offering a choice of failure modes
1319 +        class Sync extends AbstractQueuedSynchronizer {
1320 +            boolean pleaseThrow;
1321 +            @Override protected boolean tryAcquire(int ignored) {
1322 +                if (pleaseThrow) throw ex;
1323 +                return false;
1324 +            }
1325 +            @Override protected int tryAcquireShared(int ignored) {
1326 +                if (pleaseThrow) throw ex;
1327 +                return -1;
1328 +            }
1329 +            @Override protected boolean tryRelease(int ignored) {
1330 +                return true;
1331 +            }
1332 +            @Override protected boolean tryReleaseShared(int ignored) {
1333 +                return true;
1334 +            }
1335 +        }
1336 +
1337 +        final Sync s = new Sync();
1338 +
1339 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1340 +            public void realRun() {
1341 +                try {
1342 +                    if (ThreadLocalRandom.current().nextBoolean())
1343 +                        s.acquire(1);
1344 +                    else
1345 +                        s.acquireShared(1);
1346 +                    shouldThrow();
1347 +                } catch (Throwable t) {
1348 +                    assertSame(ex, t);
1349 +                    assertTrue(Thread.interrupted());
1350 +                }
1351 +            }});
1352 +        waitForThreadToEnterWaitState(thread);
1353 +        assertSame(thread, s.getFirstQueuedThread());
1354 +        assertTrue(s.hasQueuedPredecessors());
1355 +        assertTrue(s.hasQueuedThreads());
1356 +        assertEquals(1, s.getQueueLength());
1357 +
1358 +        s.pleaseThrow = true;
1359 +        thread.interrupt();
1360 +        s.release(1);
1361 +        awaitTermination(thread);
1362 +    }
1363 +
1364   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines