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

Comparing jsr166/src/test/tck/CountDownLatchTest.java (file contents):
Revision 1.27 by jsr166, Mon Aug 5 14:42:54 2019 UTC vs.
Revision 1.28 by jsr166, Sun Aug 11 22:29:26 2019 UTC

# Line 9 | Line 9
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11   import java.util.concurrent.CountDownLatch;
12 + import java.util.concurrent.ThreadLocalRandom;
13  
14   import junit.framework.Test;
15   import junit.framework.TestSuite;
# Line 72 | Line 73 | public class CountDownLatchTest extends
73          assertEquals(2, l.getCount());
74          l.countDown();
75          assertEquals(1, l.getCount());
76 <        assertThreadBlocks(t, Thread.State.WAITING);
76 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
77          l.countDown();
78          assertEquals(0, l.getCount());
79          awaitTermination(t);
# Line 97 | Line 98 | public class CountDownLatchTest extends
98          assertEquals(2, l.getCount());
99          l.countDown();
100          assertEquals(1, l.getCount());
101 <        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
101 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
102          l.countDown();
103          assertEquals(0, l.getCount());
104          awaitTermination(t);
# Line 129 | Line 130 | public class CountDownLatchTest extends
130              }});
131  
132          await(pleaseInterrupt);
133 <        assertThreadBlocks(t, Thread.State.WAITING);
133 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
134          t.interrupt();
135          awaitTermination(t);
136      }
# Line 138 | Line 139 | public class CountDownLatchTest extends
139       * timed await throws InterruptedException if interrupted before counted down
140       */
141      public void testTimedAwait_Interruptible() {
142 <        final CountDownLatch l = new CountDownLatch(1);
142 >        final int initialCount = ThreadLocalRandom.current().nextInt(1, 3);
143 >        final CountDownLatch l = new CountDownLatch(initialCount);
144          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
145          Thread t = newStartedThread(new CheckedRunnable() {
146              public void realRun() throws InterruptedException {
147 +                long startTime = System.nanoTime();
148 +
149                  Thread.currentThread().interrupt();
150                  try {
151 <                    l.await(LONG_DELAY_MS, MILLISECONDS);
151 >                    l.await(randomTimeout(), randomTimeUnit());
152                      shouldThrow();
153                  } catch (InterruptedException success) {}
154                  assertFalse(Thread.interrupted());
# Line 156 | Line 160 | public class CountDownLatchTest extends
160                  } catch (InterruptedException success) {}
161                  assertFalse(Thread.interrupted());
162  
163 <                assertEquals(1, l.getCount());
163 >                assertEquals(initialCount, l.getCount());
164 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
165              }});
166  
167          await(pleaseInterrupt);
168 <        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
168 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
169          t.interrupt();
170          awaitTermination(t);
171      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines