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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines