ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java (file contents):
Revision 1.42 by jsr166, Fri Sep 29 19:34:37 2017 UTC vs.
Revision 1.51 by jsr166, Thu Aug 15 16:01:30 2019 UTC

# Line 15 | Line 15 | import java.util.HashSet;
15   import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
16   import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject;
17  
18 import junit.framework.AssertionFailedError;
18   import junit.framework.Test;
19   import junit.framework.TestSuite;
20  
# Line 140 | Line 139 | public class AbstractQueuedLongSynchroni
139          long startTime = System.nanoTime();
140          while (!sync.isQueued(t)) {
141              if (millisElapsedSince(startTime) > LONG_DELAY_MS)
142 <                throw new AssertionFailedError("timed out");
142 >                throw new AssertionError("timed out");
143              Thread.yield();
144          }
145          assertTrue(t.isAlive());
# Line 224 | Line 223 | public class AbstractQueuedLongSynchroni
223              assertTrue(c.await(timeoutMillis, MILLISECONDS));
224              break;
225          case awaitNanos:
226 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
227 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
226 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
227 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
228              assertTrue(nanosRemaining > 0);
229              break;
230          case awaitUntil:
# Line 252 | Line 251 | public class AbstractQueuedLongSynchroni
251                  break;
252              case awaitNanos:
253                  startTime = System.nanoTime();
254 <                long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
255 <                long nanosRemaining = c.awaitNanos(nanosTimeout);
254 >                long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
255 >                long nanosRemaining = c.awaitNanos(timeoutNanos);
256                  assertTrue(nanosRemaining <= 0);
257                  assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
258                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
# Line 1256 | Line 1255 | public class AbstractQueuedLongSynchroni
1255          sync.release();
1256      }
1257  
1258 +    /**
1259 +     * Tests scenario for
1260 +     * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
1261 +     * ant -Djsr166.tckTestClass=AbstractQueuedLongSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck
1262 +     */
1263 +    public void testInterruptedFailingAcquire() throws Throwable {
1264 +        class PleaseThrow extends RuntimeException {}
1265 +        final PleaseThrow ex = new PleaseThrow();
1266 +
1267 +        // A synchronizer only offering a choice of failure modes
1268 +        class Sync extends AbstractQueuedLongSynchronizer {
1269 +            volatile boolean pleaseThrow;
1270 +            @Override protected boolean tryAcquire(long ignored) {
1271 +                if (pleaseThrow) throw ex;
1272 +                return false;
1273 +            }
1274 +            @Override protected long tryAcquireShared(long ignored) {
1275 +                if (pleaseThrow) throw ex;
1276 +                return -1;
1277 +            }
1278 +            @Override protected boolean tryRelease(long ignored) {
1279 +                return true;
1280 +            }
1281 +            @Override protected boolean tryReleaseShared(long ignored) {
1282 +                return true;
1283 +            }
1284 +        }
1285 +
1286 +        final Sync s = new Sync();
1287 +        final boolean acquireInterruptibly = randomBoolean();
1288 +        final Action[] uninterruptibleAcquireActions = {
1289 +            () -> s.acquire(1),
1290 +            () -> s.acquireShared(1),
1291 +        };
1292 +        final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1293 +        final Action[] interruptibleAcquireActions = {
1294 +            () -> s.acquireInterruptibly(1),
1295 +            () -> s.acquireSharedInterruptibly(1),
1296 +            () -> s.tryAcquireNanos(1, nanosTimeout),
1297 +            () -> s.tryAcquireSharedNanos(1, nanosTimeout),
1298 +        };
1299 +        final Action[] releaseActions = {
1300 +            () -> s.release(1),
1301 +            () -> s.releaseShared(1),
1302 +        };
1303 +        final Action acquireAction = acquireInterruptibly
1304 +            ? chooseRandomly(interruptibleAcquireActions)
1305 +            : chooseRandomly(uninterruptibleAcquireActions);
1306 +        final Action releaseAction
1307 +            = chooseRandomly(releaseActions);
1308 +
1309 +        // From os_posix.cpp:
1310 +        //
1311 +        // NOTE that since there is no "lock" around the interrupt and
1312 +        // is_interrupted operations, there is the possibility that the
1313 +        // interrupted flag (in osThread) will be "false" but that the
1314 +        // low-level events will be in the signaled state. This is
1315 +        // intentional. The effect of this is that Object.wait() and
1316 +        // LockSupport.park() will appear to have a spurious wakeup, which
1317 +        // is allowed and not harmful, and the possibility is so rare that
1318 +        // it is not worth the added complexity to add yet another lock.
1319 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1320 +            public void realRun() throws Throwable {
1321 +                try {
1322 +                    acquireAction.run();
1323 +                    shouldThrow();
1324 +                } catch (InterruptedException possible) {
1325 +                    assertTrue(acquireInterruptibly);
1326 +                    assertFalse(Thread.interrupted());
1327 +                } catch (PleaseThrow possible) {
1328 +                    awaitInterrupted();
1329 +                }
1330 +            }});
1331 +        for (long startTime = 0L;; ) {
1332 +            waitForThreadToEnterWaitState(thread);
1333 +            if (s.getFirstQueuedThread() == thread
1334 +                && s.hasQueuedPredecessors()
1335 +                && s.hasQueuedThreads()
1336 +                && s.getQueueLength() == 1)
1337 +                break;
1338 +            if (startTime == 0L)
1339 +                startTime = System.nanoTime();
1340 +            else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1341 +                fail("timed out waiting for AQS state: "
1342 +                     + "thread state=" + thread.getState()
1343 +                     + ", queued threads=" + s.getQueuedThreads());
1344 +            Thread.yield();
1345 +        }
1346 +
1347 +        s.pleaseThrow = true;
1348 +        // release and interrupt, in random order
1349 +        if (randomBoolean()) {
1350 +            thread.interrupt();
1351 +            releaseAction.run();
1352 +        } else {
1353 +            releaseAction.run();
1354 +            thread.interrupt();
1355 +        }
1356 +        awaitTermination(thread);
1357 +
1358 +        assertNull(s.getFirstQueuedThread());
1359 +        assertFalse(s.hasQueuedPredecessors());
1360 +        assertFalse(s.hasQueuedThreads());
1361 +        assertEquals(0, s.getQueueLength());
1362 +        assertTrue(s.getQueuedThreads().isEmpty());
1363 +    }
1364 +
1365   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines