34 */ |
34 */ |
35 |
35 |
36 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
36 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
37 |
37 |
38 import java.util.concurrent.CountDownLatch; |
38 import java.util.concurrent.CountDownLatch; |
|
39 import java.util.concurrent.ThreadLocalRandom; |
39 |
40 |
40 import junit.framework.Test; |
41 import junit.framework.Test; |
41 import junit.framework.TestSuite; |
42 import junit.framework.TestSuite; |
42 |
43 |
43 public class CountDownLatchTest extends JSR166TestCase { |
44 public class CountDownLatchTest extends JSR166TestCase { |
97 |
98 |
98 await(pleaseCountDown); |
99 await(pleaseCountDown); |
99 assertEquals(2, l.getCount()); |
100 assertEquals(2, l.getCount()); |
100 l.countDown(); |
101 l.countDown(); |
101 assertEquals(1, l.getCount()); |
102 assertEquals(1, l.getCount()); |
102 assertThreadBlocks(t, Thread.State.WAITING); |
103 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
103 l.countDown(); |
104 l.countDown(); |
104 assertEquals(0, l.getCount()); |
105 assertEquals(0, l.getCount()); |
105 awaitTermination(t); |
106 awaitTermination(t); |
106 } |
107 } |
107 |
108 |
122 |
123 |
123 await(pleaseCountDown); |
124 await(pleaseCountDown); |
124 assertEquals(2, l.getCount()); |
125 assertEquals(2, l.getCount()); |
125 l.countDown(); |
126 l.countDown(); |
126 assertEquals(1, l.getCount()); |
127 assertEquals(1, l.getCount()); |
127 assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
128 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
128 l.countDown(); |
129 l.countDown(); |
129 assertEquals(0, l.getCount()); |
130 assertEquals(0, l.getCount()); |
130 awaitTermination(t); |
131 awaitTermination(t); |
131 } |
132 } |
132 |
133 |
154 |
155 |
155 assertEquals(1, l.getCount()); |
156 assertEquals(1, l.getCount()); |
156 }}); |
157 }}); |
157 |
158 |
158 await(pleaseInterrupt); |
159 await(pleaseInterrupt); |
159 assertThreadBlocks(t, Thread.State.WAITING); |
160 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
160 t.interrupt(); |
161 t.interrupt(); |
161 awaitTermination(t); |
162 awaitTermination(t); |
162 } |
163 } |
163 |
164 |
164 /** |
165 /** |
165 * timed await throws InterruptedException if interrupted before counted down |
166 * timed await throws InterruptedException if interrupted before counted down |
166 */ |
167 */ |
167 public void testTimedAwait_Interruptible() { |
168 public void testTimedAwait_Interruptible() { |
168 final CountDownLatch l = new CountDownLatch(1); |
169 final int initialCount = ThreadLocalRandom.current().nextInt(1, 3); |
|
170 final CountDownLatch l = new CountDownLatch(initialCount); |
169 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
171 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
170 Thread t = newStartedThread(new CheckedRunnable() { |
172 Thread t = newStartedThread(new CheckedRunnable() { |
171 public void realRun() throws InterruptedException { |
173 public void realRun() throws InterruptedException { |
172 Thread.currentThread().interrupt(); |
174 Thread.currentThread().interrupt(); |
173 try { |
175 try { |
174 l.await(LONG_DELAY_MS, MILLISECONDS); |
176 l.await(randomTimeout(), randomTimeUnit()); |
175 shouldThrow(); |
177 shouldThrow(); |
176 } catch (InterruptedException success) {} |
178 } catch (InterruptedException success) {} |
177 assertFalse(Thread.interrupted()); |
179 assertFalse(Thread.interrupted()); |
178 |
180 |
179 pleaseInterrupt.countDown(); |
181 pleaseInterrupt.countDown(); |
180 try { |
182 try { |
181 l.await(LONG_DELAY_MS, MILLISECONDS); |
183 l.await(LONGER_DELAY_MS, MILLISECONDS); |
182 shouldThrow(); |
184 shouldThrow(); |
183 } catch (InterruptedException success) {} |
185 } catch (InterruptedException success) {} |
184 assertFalse(Thread.interrupted()); |
186 assertFalse(Thread.interrupted()); |
185 |
187 |
186 assertEquals(1, l.getCount()); |
188 assertEquals(initialCount, l.getCount()); |
187 }}); |
189 }}); |
188 |
190 |
189 await(pleaseInterrupt); |
191 await(pleaseInterrupt); |
190 assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
192 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
191 t.interrupt(); |
193 t.interrupt(); |
192 awaitTermination(t); |
194 awaitTermination(t); |
193 } |
195 } |
194 |
196 |
195 /** |
197 /** |
198 public void testAwaitTimeout() throws InterruptedException { |
200 public void testAwaitTimeout() throws InterruptedException { |
199 final CountDownLatch l = new CountDownLatch(1); |
201 final CountDownLatch l = new CountDownLatch(1); |
200 Thread t = newStartedThread(new CheckedRunnable() { |
202 Thread t = newStartedThread(new CheckedRunnable() { |
201 public void realRun() throws InterruptedException { |
203 public void realRun() throws InterruptedException { |
202 assertEquals(1, l.getCount()); |
204 assertEquals(1, l.getCount()); |
|
205 |
|
206 long startTime = System.nanoTime(); |
203 assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
207 assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
|
208 assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
209 |
204 assertEquals(1, l.getCount()); |
210 assertEquals(1, l.getCount()); |
205 }}); |
211 }}); |
206 |
212 |
207 awaitTermination(t); |
213 awaitTermination(t); |
208 assertEquals(1, l.getCount()); |
214 assertEquals(1, l.getCount()); |