1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.concurrent.CountDownLatch; |
12 |
+ |
|
13 |
+ |
import junit.framework.Test; |
14 |
+ |
import junit.framework.TestSuite; |
15 |
+ |
|
16 |
|
public class CountDownLatchTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(CountDownLatchTest.class); |
56 |
|
/** |
57 |
|
* await returns after countDown to zero, but not before |
58 |
|
*/ |
59 |
< |
public void testAwait() throws InterruptedException { |
59 |
> |
public void testAwait() { |
60 |
|
final CountDownLatch l = new CountDownLatch(2); |
61 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
62 |
|
|
63 |
< |
Thread t = new Thread(new CheckedRunnable() { |
63 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
64 |
|
public void realRun() throws InterruptedException { |
65 |
< |
threadAssertTrue(l.getCount() > 0); |
65 |
> |
assertEquals(2, l.getCount()); |
66 |
> |
pleaseCountDown.countDown(); |
67 |
|
l.await(); |
68 |
< |
threadAssertTrue(l.getCount() == 0); |
68 |
> |
assertEquals(0, l.getCount()); |
69 |
|
}}); |
70 |
|
|
71 |
< |
t.start(); |
72 |
< |
assertEquals(l.getCount(), 2); |
69 |
< |
Thread.sleep(SHORT_DELAY_MS); |
71 |
> |
await(pleaseCountDown); |
72 |
> |
assertEquals(2, l.getCount()); |
73 |
|
l.countDown(); |
74 |
< |
assertEquals(l.getCount(), 1); |
74 |
> |
assertEquals(1, l.getCount()); |
75 |
> |
assertThreadStaysAlive(t); |
76 |
|
l.countDown(); |
77 |
< |
assertEquals(l.getCount(), 0); |
78 |
< |
t.join(); |
77 |
> |
assertEquals(0, l.getCount()); |
78 |
> |
awaitTermination(t); |
79 |
|
} |
80 |
|
|
77 |
– |
|
81 |
|
/** |
82 |
|
* timed await returns after countDown to zero |
83 |
|
*/ |
84 |
< |
public void testTimedAwait() throws InterruptedException { |
84 |
> |
public void testTimedAwait() { |
85 |
|
final CountDownLatch l = new CountDownLatch(2); |
86 |
+ |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
87 |
|
|
88 |
< |
Thread t = new Thread(new CheckedRunnable() { |
88 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
89 |
|
public void realRun() throws InterruptedException { |
90 |
< |
threadAssertTrue(l.getCount() > 0); |
91 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
90 |
> |
assertEquals(2, l.getCount()); |
91 |
> |
pleaseCountDown.countDown(); |
92 |
> |
assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); |
93 |
> |
assertEquals(0, l.getCount()); |
94 |
|
}}); |
95 |
|
|
96 |
< |
t.start(); |
97 |
< |
assertEquals(l.getCount(), 2); |
92 |
< |
Thread.sleep(SHORT_DELAY_MS); |
96 |
> |
await(pleaseCountDown); |
97 |
> |
assertEquals(2, l.getCount()); |
98 |
|
l.countDown(); |
99 |
< |
assertEquals(l.getCount(), 1); |
99 |
> |
assertEquals(1, l.getCount()); |
100 |
> |
assertThreadStaysAlive(t); |
101 |
|
l.countDown(); |
102 |
< |
assertEquals(l.getCount(), 0); |
103 |
< |
t.join(); |
102 |
> |
assertEquals(0, l.getCount()); |
103 |
> |
awaitTermination(t); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
|
* await throws IE if interrupted before counted down |
108 |
|
*/ |
109 |
< |
public void testAwait_InterruptedException() throws InterruptedException { |
109 |
> |
public void testAwait_Interruptible() { |
110 |
|
final CountDownLatch l = new CountDownLatch(1); |
111 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
111 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
112 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
113 |
|
public void realRun() throws InterruptedException { |
114 |
< |
threadAssertTrue(l.getCount() > 0); |
115 |
< |
l.await(); |
114 |
> |
Thread.currentThread().interrupt(); |
115 |
> |
try { |
116 |
> |
l.await(); |
117 |
> |
shouldThrow(); |
118 |
> |
} catch (InterruptedException success) {} |
119 |
> |
assertFalse(Thread.interrupted()); |
120 |
> |
|
121 |
> |
pleaseInterrupt.countDown(); |
122 |
> |
try { |
123 |
> |
l.await(); |
124 |
> |
shouldThrow(); |
125 |
> |
} catch (InterruptedException success) {} |
126 |
> |
assertFalse(Thread.interrupted()); |
127 |
> |
|
128 |
> |
assertEquals(1, l.getCount()); |
129 |
|
}}); |
130 |
|
|
131 |
< |
t.start(); |
132 |
< |
assertEquals(l.getCount(), 1); |
131 |
> |
await(pleaseInterrupt); |
132 |
> |
assertThreadStaysAlive(t); |
133 |
|
t.interrupt(); |
134 |
< |
t.join(); |
134 |
> |
awaitTermination(t); |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
|
* timed await throws IE if interrupted before counted down |
139 |
|
*/ |
140 |
< |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
140 |
> |
public void testTimedAwait_Interruptible() { |
141 |
|
final CountDownLatch l = new CountDownLatch(1); |
142 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
142 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
143 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
144 |
|
public void realRun() throws InterruptedException { |
145 |
< |
threadAssertTrue(l.getCount() > 0); |
146 |
< |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
145 |
> |
Thread.currentThread().interrupt(); |
146 |
> |
try { |
147 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
148 |
> |
shouldThrow(); |
149 |
> |
} catch (InterruptedException success) {} |
150 |
> |
assertFalse(Thread.interrupted()); |
151 |
> |
|
152 |
> |
pleaseInterrupt.countDown(); |
153 |
> |
try { |
154 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
155 |
> |
shouldThrow(); |
156 |
> |
} catch (InterruptedException success) {} |
157 |
> |
assertFalse(Thread.interrupted()); |
158 |
> |
|
159 |
> |
assertEquals(1, l.getCount()); |
160 |
|
}}); |
161 |
|
|
162 |
< |
t.start(); |
163 |
< |
Thread.sleep(SHORT_DELAY_MS); |
130 |
< |
assertEquals(l.getCount(), 1); |
162 |
> |
await(pleaseInterrupt); |
163 |
> |
assertThreadStaysAlive(t); |
164 |
|
t.interrupt(); |
165 |
< |
t.join(); |
165 |
> |
awaitTermination(t); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
170 |
|
*/ |
171 |
|
public void testAwaitTimeout() throws InterruptedException { |
172 |
|
final CountDownLatch l = new CountDownLatch(1); |
173 |
< |
Thread t = new Thread(new CheckedRunnable() { |
173 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
174 |
|
public void realRun() throws InterruptedException { |
175 |
< |
threadAssertTrue(l.getCount() > 0); |
176 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
177 |
< |
threadAssertTrue(l.getCount() > 0); |
175 |
> |
assertEquals(1, l.getCount()); |
176 |
> |
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
177 |
> |
assertEquals(1, l.getCount()); |
178 |
|
}}); |
179 |
|
|
180 |
< |
t.start(); |
181 |
< |
assertEquals(l.getCount(), 1); |
149 |
< |
t.join(); |
180 |
> |
awaitTermination(t); |
181 |
> |
assertEquals(1, l.getCount()); |
182 |
|
} |
183 |
|
|
184 |
|
/** |
186 |
|
*/ |
187 |
|
public void testToString() { |
188 |
|
CountDownLatch s = new CountDownLatch(2); |
189 |
< |
String us = s.toString(); |
158 |
< |
assertTrue(us.indexOf("Count = 2") >= 0); |
189 |
> |
assertTrue(s.toString().contains("Count = 2")); |
190 |
|
s.countDown(); |
191 |
< |
String s1 = s.toString(); |
161 |
< |
assertTrue(s1.indexOf("Count = 1") >= 0); |
191 |
> |
assertTrue(s.toString().contains("Count = 1")); |
192 |
|
s.countDown(); |
193 |
< |
String s2 = s.toString(); |
164 |
< |
assertTrue(s2.indexOf("Count = 0") >= 0); |
193 |
> |
assertTrue(s.toString().contains("Count = 0")); |
194 |
|
} |
195 |
|
|
196 |
|
} |