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.23 by jsr166, Sat Apr 25 04:55:30 2015 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 22 | Line 23 | public class CountDownLatchTest extends
23      }
24  
25      /**
26 <     * negative constructor argument throws IAE
26 >     * negative constructor argument throws IllegalArgumentException
27       */
28      public void testConstructor() {
29          try {
# Line 72 | Line 73 | public class CountDownLatchTest extends
73          assertEquals(2, l.getCount());
74          l.countDown();
75          assertEquals(1, l.getCount());
76 <        assertThreadStaysAlive(t);
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 <        assertThreadStaysAlive(t);
101 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
102          l.countDown();
103          assertEquals(0, l.getCount());
104          awaitTermination(t);
105      }
106  
107      /**
108 <     * await throws IE if interrupted before counted down
108 >     * await throws InterruptedException if interrupted before counted down
109       */
110      public void testAwait_Interruptible() {
111          final CountDownLatch l = new CountDownLatch(1);
# Line 129 | Line 130 | public class CountDownLatchTest extends
130              }});
131  
132          await(pleaseInterrupt);
133 <        assertThreadStaysAlive(t);
133 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
134          t.interrupt();
135          awaitTermination(t);
136      }
137  
138      /**
139 <     * timed await throws IE if interrupted before counted down
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 <        assertThreadStaysAlive(t);
168 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
169          t.interrupt();
170          awaitTermination(t);
171      }
# Line 173 | Line 178 | public class CountDownLatchTest extends
178          Thread t = newStartedThread(new CheckedRunnable() {
179              public void realRun() throws InterruptedException {
180                  assertEquals(1, l.getCount());
181 +
182 +                long startTime = System.nanoTime();
183                  assertFalse(l.await(timeoutMillis(), MILLISECONDS));
184 +                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
185 +
186                  assertEquals(1, l.getCount());
187              }});
188  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines