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.37 by jsr166, Sun Jan 1 20:34:39 2017 UTC vs.
Revision 1.48 by jsr166, Tue Aug 13 23:05:18 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  
21 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
22   public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
23      public static void main(String[] args) {
24          main(suite(), args);
# Line 30 | Line 30 | public class AbstractQueuedLongSynchroni
30      /**
31       * A simple mutex class, adapted from the class javadoc.  Exclusive
32       * acquire tests exercise this as a sample user extension.
33 +     *
34 +     * Unlike the javadoc sample, we don't track owner thread via
35 +     * AbstractOwnableSynchronizer methods.
36       */
37      static class Mutex extends AbstractQueuedLongSynchronizer {
38          /** An eccentric value > 32 bits for locked synchronizer state. */
# Line 37 | Line 40 | public class AbstractQueuedLongSynchroni
40  
41          static final long UNLOCKED = 0;
42  
43 <        public boolean isHeldExclusively() {
43 >        /** Owner thread is untracked, so this is really just isLocked(). */
44 >        @Override public boolean isHeldExclusively() {
45              long state = getState();
46              assertTrue(state == UNLOCKED || state == LOCKED);
47              return state == LOCKED;
48          }
49  
50 <        public boolean tryAcquire(long acquires) {
50 >        @Override protected boolean tryAcquire(long acquires) {
51              assertEquals(LOCKED, acquires);
52              return compareAndSetState(UNLOCKED, LOCKED);
53          }
54  
55 <        public boolean tryRelease(long releases) {
55 >        @Override protected boolean tryRelease(long releases) {
56              if (getState() != LOCKED) throw new IllegalMonitorStateException();
57              setState(UNLOCKED);
58              return true;
# Line 78 | Line 82 | public class AbstractQueuedLongSynchroni
82              release(LOCKED);
83          }
84  
85 +        /** Faux-Implements Lock.newCondition(). */
86          public ConditionObject newCondition() {
87              return new ConditionObject();
88          }
89      }
90  
91      /**
92 <     * A simple latch class, to test shared mode.
92 >     * A minimal latch class, to test shared mode.
93       */
94      static class BooleanLatch extends AbstractQueuedLongSynchronizer {
95          public boolean isSignalled() { return getState() != 0; }
# Line 134 | 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 218 | 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 246 | 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 965 | Line 970 | public class AbstractQueuedLongSynchroni
970       */
971      public void testAwaitUninterruptibly() {
972          final Mutex sync = new Mutex();
973 <        final ConditionObject c = sync.newCondition();
973 >        final ConditionObject condition = sync.newCondition();
974          final BooleanLatch pleaseInterrupt = new BooleanLatch();
975          Thread t = newStartedThread(new CheckedRunnable() {
976              public void realRun() {
977                  sync.acquire();
978                  assertTrue(pleaseInterrupt.releaseShared(0));
979 <                c.awaitUninterruptibly();
979 >                condition.awaitUninterruptibly();
980                  assertTrue(Thread.interrupted());
981 <                assertHasWaitersLocked(sync, c, NO_THREADS);
981 >                assertHasWaitersLocked(sync, condition, NO_THREADS);
982                  sync.release();
983              }});
984  
985          pleaseInterrupt.acquireShared(0);
986          sync.acquire();
987 <        assertHasWaitersLocked(sync, c, t);
987 >        assertHasWaitersLocked(sync, condition, t);
988          sync.release();
989          t.interrupt();
990 <        assertHasWaitersUnlocked(sync, c, t);
991 <        assertThreadStaysAlive(t);
990 >        assertHasWaitersUnlocked(sync, condition, t);
991 >        assertThreadBlocks(t, Thread.State.WAITING);
992          sync.acquire();
993 <        assertHasWaitersLocked(sync, c, t);
993 >        assertHasWaitersLocked(sync, condition, t);
994          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
995 <        c.signal();
996 <        assertHasWaitersLocked(sync, c, NO_THREADS);
995 >        condition.signal();
996 >        assertHasWaitersLocked(sync, condition, NO_THREADS);
997          assertHasExclusiveQueuedThreads(sync, t);
998          sync.release();
999          awaitTermination(t);
# Line 1131 | Line 1136 | public class AbstractQueuedLongSynchroni
1136  
1137          waitForQueuedThread(l, t);
1138          assertFalse(l.isSignalled());
1139 <        assertThreadStaysAlive(t);
1139 >        assertThreadBlocks(t, Thread.State.WAITING);
1140          assertHasSharedQueuedThreads(l, t);
1141          assertTrue(l.releaseShared(0));
1142          assertTrue(l.isSignalled());
# Line 1156 | Line 1161 | public class AbstractQueuedLongSynchroni
1161  
1162          waitForQueuedThread(l, t);
1163          assertFalse(l.isSignalled());
1164 <        assertThreadStaysAlive(t);
1164 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1165          assertTrue(l.releaseShared(0));
1166          assertTrue(l.isSignalled());
1167          awaitTermination(t);
# Line 1250 | 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 +     */
1262 +    public void testInterruptedFailingAcquire() throws InterruptedException {
1263 +        final RuntimeException ex = new RuntimeException();
1264 +
1265 +        // A synchronizer only offering a choice of failure modes
1266 +        class Sync extends AbstractQueuedLongSynchronizer {
1267 +            volatile boolean pleaseThrow;
1268 +            @Override protected boolean tryAcquire(long ignored) {
1269 +                if (pleaseThrow) throw ex;
1270 +                return false;
1271 +            }
1272 +            @Override protected long tryAcquireShared(long ignored) {
1273 +                if (pleaseThrow) throw ex;
1274 +                return -1;
1275 +            }
1276 +            @Override protected boolean tryRelease(long ignored) {
1277 +                return true;
1278 +            }
1279 +            @Override protected boolean tryReleaseShared(long ignored) {
1280 +                return true;
1281 +            }
1282 +        }
1283 +
1284 +        final Sync s = new Sync();
1285 +
1286 +        final Thread thread = newStartedThread(new CheckedRunnable() {
1287 +            public void realRun() {
1288 +                try {
1289 +                    if (randomBoolean())
1290 +                        s.acquire(1);
1291 +                    else
1292 +                        s.acquireShared(1);
1293 +                    shouldThrow();
1294 +                } catch (Throwable t) {
1295 +                    assertSame(ex, t);
1296 +                    assertTrue(Thread.interrupted());
1297 +                }
1298 +            }});
1299 +        waitForThreadToEnterWaitState(thread);
1300 +        assertSame(thread, s.getFirstQueuedThread());
1301 +        assertTrue(s.hasQueuedPredecessors());
1302 +        assertTrue(s.hasQueuedThreads());
1303 +        assertEquals(1, s.getQueueLength());
1304 +
1305 +        s.pleaseThrow = true;
1306 +        thread.interrupt();
1307 +        s.release(1);
1308 +        awaitTermination(thread);
1309 +    }
1310 +
1311   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines