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.68 by jsr166, Tue Aug 13 23:05:18 2019 UTC

# Line 16 | Line 16 | import java.util.HashSet;
16   import java.util.concurrent.locks.AbstractQueuedSynchronizer;
17   import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
18  
19 import junit.framework.AssertionFailedError;
19   import junit.framework.Test;
20   import junit.framework.TestSuite;
21  
# Line 144 | Line 143 | public class AbstractQueuedSynchronizerT
143          long startTime = System.nanoTime();
144          while (!sync.isQueued(t)) {
145              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
146 <                throw new AssertionFailedError("timed out");
146 >                throw new AssertionError("timed out");
147              Thread.yield();
148          }
149          assertTrue(t.isAlive());
# Line 228 | Line 227 | public class AbstractQueuedSynchronizerT
227              assertTrue(c.await(timeoutMillis, MILLISECONDS));
228              break;
229          case awaitNanos:
230 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
231 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
230 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
231 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
232              assertTrue(nanosRemaining > 0);
233              break;
234          case awaitUntil:
# Line 256 | Line 255 | public class AbstractQueuedSynchronizerT
255                  break;
256              case awaitNanos:
257                  startTime = System.nanoTime();
258 <                long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
259 <                long nanosRemaining = c.awaitNanos(nanosTimeout);
258 >                long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
259 >                long nanosRemaining = c.awaitNanos(timeoutNanos);
260                  assertTrue(nanosRemaining <= 0);
261                  assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
262                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
# Line 1261 | Line 1260 | public class AbstractQueuedSynchronizerT
1260      }
1261  
1262      /**
1264     * Disabled demo test for (unfixed as of 2017-11)
1263       * JDK-8191483: AbstractQueuedSynchronizer cancel/cancel race
1264       * ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testCancelCancelRace -Djsr166.runsPerTest=100 tck
1265       */
1266 <    public void XXXXtestCancelCancelRace() throws InterruptedException {
1266 >    public void testCancelCancelRace() throws InterruptedException {
1267          class Sync extends AbstractQueuedSynchronizer {
1268 <            private static final long serialVersionUID = 1L;
1271 <
1272 <            public boolean tryAcquire(int acquires) {
1268 >            protected boolean tryAcquire(int acquires) {
1269                  return !hasQueuedPredecessors() && compareAndSetState(0, 1);
1270              }
1275
1271              protected boolean tryRelease(int releases) {
1272                  return compareAndSetState(1, 0);
1273              }
# Line 1282 | Line 1277 | public class AbstractQueuedSynchronizerT
1277          s.acquire(1);           // acquire to force other threads to enqueue
1278  
1279          // try to trigger double cancel race with two background threads
1280 <        ArrayList<Thread> ts = new ArrayList<>();
1280 >        ArrayList<Thread> threads = new ArrayList<>();
1281          Runnable failedAcquire = () -> {
1282              try {
1283                  s.acquireInterruptibly(1);
1284 <                throw new AssertionError();
1285 <            } catch (InterruptedException expected) {}
1284 >                shouldThrow();
1285 >            } catch (InterruptedException success) {}
1286          };
1287          for (int i = 0; i < 2; i++) {
1288 <            Thread t = new Thread(failedAcquire);
1289 <            t.start();
1290 <            ts.add(t);
1288 >            Thread thread = new Thread(failedAcquire);
1289 >            thread.start();
1290 >            threads.add(thread);
1291          }
1292          Thread.sleep(100);
1293 <        for (Thread t : ts) t.interrupt();
1294 <        for (Thread t : ts) t.join();
1293 >        for (Thread thread : threads) thread.interrupt();
1294 >        for (Thread thread : threads) awaitTermination(thread);
1295  
1296          s.release(1);
1297  
# Line 1311 | Line 1306 | public class AbstractQueuedSynchronizerT
1306                      s.getFirstQueuedThread()));
1307      }
1308  
1309 +    /**
1310 +     * Tests scenario for
1311 +     * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1312 +     */
1313 +    public void testInterruptedFailingAcquire() throws InterruptedException {
1314 +        final RuntimeException ex = new RuntimeException();
1315 +
1316 +        // A synchronizer only offering a choice of failure modes
1317 +        class Sync extends AbstractQueuedSynchronizer {
1318 +            volatile boolean pleaseThrow;
1319 +            @Override protected boolean tryAcquire(int ignored) {
1320 +                if (pleaseThrow) throw ex;
1321 +                return false;
1322 +            }
1323 +            @Override protected int tryAcquireShared(int ignored) {
1324 +                if (pleaseThrow) throw ex;
1325 +                return -1;
1326 +            }
1327 +            @Override protected boolean tryRelease(int ignored) {
1328 +                return true;
1329 +            }
1330 +            @Override protected boolean tryReleaseShared(int ignored) {
1331 +                return true;
1332 +            }
1333 +        }
1334 +
1335 +        final Sync s = new Sync();
1336 +
1337 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1338 +            public void realRun() {
1339 +                try {
1340 +                    if (randomBoolean())
1341 +                        s.acquire(1);
1342 +                    else
1343 +                        s.acquireShared(1);
1344 +                    shouldThrow();
1345 +                } catch (Throwable t) {
1346 +                    assertSame(ex, t);
1347 +                    assertTrue(Thread.interrupted());
1348 +                }
1349 +            }});
1350 +        waitForThreadToEnterWaitState(thread);
1351 +        assertSame(thread, s.getFirstQueuedThread());
1352 +        assertTrue(s.hasQueuedPredecessors());
1353 +        assertTrue(s.hasQueuedThreads());
1354 +        assertEquals(1, s.getQueueLength());
1355 +
1356 +        s.pleaseThrow = true;
1357 +        thread.interrupt();
1358 +        s.release(1);
1359 +        awaitTermination(thread);
1360 +    }
1361 +
1362   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines