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.*; |
11 |
> |
import java.util.concurrent.CountDownLatch; |
12 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
|
14 |
|
public class CountDownLatchTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(CountDownLatchTest.class); |
19 |
> |
return new TestSuite(CountDownLatchTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
26 |
|
try { |
27 |
|
new CountDownLatch(-1); |
28 |
|
shouldThrow(); |
29 |
< |
} catch(IllegalArgumentException success){} |
29 |
> |
} catch (IllegalArgumentException success) {} |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
|
* getCount returns initial count and decreases after countDown |
34 |
|
*/ |
35 |
|
public void testGetCount() { |
36 |
< |
final CountDownLatch l = new CountDownLatch(2); |
37 |
< |
assertEquals(2, l.getCount()); |
38 |
< |
l.countDown(); |
39 |
< |
assertEquals(1, l.getCount()); |
36 |
> |
final CountDownLatch l = new CountDownLatch(2); |
37 |
> |
assertEquals(2, l.getCount()); |
38 |
> |
l.countDown(); |
39 |
> |
assertEquals(1, l.getCount()); |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
|
* countDown decrements count when positive and has no effect when zero |
44 |
|
*/ |
45 |
|
public void testCountDown() { |
46 |
< |
final CountDownLatch l = new CountDownLatch(1); |
47 |
< |
assertEquals(1, l.getCount()); |
48 |
< |
l.countDown(); |
49 |
< |
assertEquals(0, l.getCount()); |
50 |
< |
l.countDown(); |
51 |
< |
assertEquals(0, l.getCount()); |
46 |
> |
final CountDownLatch l = new CountDownLatch(1); |
47 |
> |
assertEquals(1, l.getCount()); |
48 |
> |
l.countDown(); |
49 |
> |
assertEquals(0, l.getCount()); |
50 |
> |
l.countDown(); |
51 |
> |
assertEquals(0, l.getCount()); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
|
* await returns after countDown to zero, but not before |
56 |
|
*/ |
57 |
|
public void testAwait() { |
58 |
< |
final CountDownLatch l = new CountDownLatch(2); |
58 |
> |
final CountDownLatch l = new CountDownLatch(2); |
59 |
> |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
60 |
|
|
61 |
< |
Thread t = new Thread(new Runnable() { |
62 |
< |
public void run() { |
63 |
< |
try { |
64 |
< |
threadAssertTrue(l.getCount() > 0); |
65 |
< |
l.await(); |
66 |
< |
threadAssertTrue(l.getCount() == 0); |
67 |
< |
} catch(InterruptedException e){ |
68 |
< |
threadUnexpectedException(); |
69 |
< |
} |
70 |
< |
} |
71 |
< |
}); |
72 |
< |
t.start(); |
73 |
< |
try { |
74 |
< |
assertEquals(l.getCount(), 2); |
75 |
< |
Thread.sleep(SHORT_DELAY_MS); |
76 |
< |
l.countDown(); |
75 |
< |
assertEquals(l.getCount(), 1); |
76 |
< |
l.countDown(); |
77 |
< |
assertEquals(l.getCount(), 0); |
78 |
< |
t.join(); |
79 |
< |
} catch (InterruptedException e){ |
80 |
< |
unexpectedException(); |
81 |
< |
} |
61 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
62 |
> |
public void realRun() throws InterruptedException { |
63 |
> |
assertEquals(2, l.getCount()); |
64 |
> |
pleaseCountDown.countDown(); |
65 |
> |
l.await(); |
66 |
> |
assertEquals(0, l.getCount()); |
67 |
> |
}}); |
68 |
> |
|
69 |
> |
await(pleaseCountDown); |
70 |
> |
assertEquals(2, l.getCount()); |
71 |
> |
l.countDown(); |
72 |
> |
assertEquals(1, l.getCount()); |
73 |
> |
assertThreadStaysAlive(t); |
74 |
> |
l.countDown(); |
75 |
> |
assertEquals(0, l.getCount()); |
76 |
> |
awaitTermination(t); |
77 |
|
} |
78 |
|
|
84 |
– |
|
79 |
|
/** |
80 |
|
* timed await returns after countDown to zero |
81 |
|
*/ |
82 |
|
public void testTimedAwait() { |
83 |
< |
final CountDownLatch l = new CountDownLatch(2); |
83 |
> |
final CountDownLatch l = new CountDownLatch(2); |
84 |
> |
final CountDownLatch pleaseCountDown = new CountDownLatch(1); |
85 |
|
|
86 |
< |
Thread t = new Thread(new Runnable() { |
87 |
< |
public void run() { |
88 |
< |
try { |
89 |
< |
threadAssertTrue(l.getCount() > 0); |
90 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
91 |
< |
} catch(InterruptedException e){ |
92 |
< |
threadUnexpectedException(); |
93 |
< |
} |
94 |
< |
} |
95 |
< |
}); |
96 |
< |
t.start(); |
97 |
< |
try { |
98 |
< |
assertEquals(l.getCount(), 2); |
99 |
< |
Thread.sleep(SHORT_DELAY_MS); |
100 |
< |
l.countDown(); |
101 |
< |
assertEquals(l.getCount(), 1); |
107 |
< |
l.countDown(); |
108 |
< |
assertEquals(l.getCount(), 0); |
109 |
< |
t.join(); |
110 |
< |
} catch (InterruptedException e){ |
111 |
< |
unexpectedException(); |
112 |
< |
} |
86 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
87 |
> |
public void realRun() throws InterruptedException { |
88 |
> |
assertEquals(2, l.getCount()); |
89 |
> |
pleaseCountDown.countDown(); |
90 |
> |
assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); |
91 |
> |
assertEquals(0, l.getCount()); |
92 |
> |
}}); |
93 |
> |
|
94 |
> |
await(pleaseCountDown); |
95 |
> |
assertEquals(2, l.getCount()); |
96 |
> |
l.countDown(); |
97 |
> |
assertEquals(1, l.getCount()); |
98 |
> |
assertThreadStaysAlive(t); |
99 |
> |
l.countDown(); |
100 |
> |
assertEquals(0, l.getCount()); |
101 |
> |
awaitTermination(t); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
|
* await throws IE if interrupted before counted down |
106 |
|
*/ |
107 |
< |
public void testAwait_InterruptedException() { |
107 |
> |
public void testAwait_Interruptible() { |
108 |
|
final CountDownLatch l = new CountDownLatch(1); |
109 |
< |
Thread t = new Thread(new Runnable() { |
110 |
< |
public void run() { |
111 |
< |
try { |
112 |
< |
threadAssertTrue(l.getCount() > 0); |
113 |
< |
l.await(); |
114 |
< |
threadShouldThrow(); |
115 |
< |
} catch(InterruptedException success){} |
116 |
< |
} |
117 |
< |
}); |
118 |
< |
t.start(); |
119 |
< |
try { |
120 |
< |
assertEquals(l.getCount(), 1); |
121 |
< |
t.interrupt(); |
122 |
< |
t.join(); |
123 |
< |
} catch (InterruptedException e){ |
124 |
< |
unexpectedException(); |
125 |
< |
} |
109 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
110 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
111 |
> |
public void realRun() throws InterruptedException { |
112 |
> |
Thread.currentThread().interrupt(); |
113 |
> |
try { |
114 |
> |
l.await(); |
115 |
> |
shouldThrow(); |
116 |
> |
} catch (InterruptedException success) {} |
117 |
> |
assertFalse(Thread.interrupted()); |
118 |
> |
|
119 |
> |
pleaseInterrupt.countDown(); |
120 |
> |
try { |
121 |
> |
l.await(); |
122 |
> |
shouldThrow(); |
123 |
> |
} catch (InterruptedException success) {} |
124 |
> |
assertFalse(Thread.interrupted()); |
125 |
> |
|
126 |
> |
assertEquals(1, l.getCount()); |
127 |
> |
}}); |
128 |
> |
|
129 |
> |
await(pleaseInterrupt); |
130 |
> |
assertThreadStaysAlive(t); |
131 |
> |
t.interrupt(); |
132 |
> |
awaitTermination(t); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
|
* timed await throws IE if interrupted before counted down |
137 |
|
*/ |
138 |
< |
public void testTimedAwait_InterruptedException() { |
138 |
> |
public void testTimedAwait_Interruptible() { |
139 |
|
final CountDownLatch l = new CountDownLatch(1); |
140 |
< |
Thread t = new Thread(new Runnable() { |
141 |
< |
public void run() { |
142 |
< |
try { |
143 |
< |
threadAssertTrue(l.getCount() > 0); |
144 |
< |
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
145 |
< |
threadShouldThrow(); |
146 |
< |
} catch(InterruptedException success){} |
147 |
< |
} |
148 |
< |
}); |
149 |
< |
t.start(); |
150 |
< |
try { |
151 |
< |
Thread.sleep(SHORT_DELAY_MS); |
152 |
< |
assertEquals(l.getCount(), 1); |
153 |
< |
t.interrupt(); |
154 |
< |
t.join(); |
155 |
< |
} catch (InterruptedException e){ |
156 |
< |
unexpectedException(); |
157 |
< |
} |
140 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
141 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
142 |
> |
public void realRun() throws InterruptedException { |
143 |
> |
Thread.currentThread().interrupt(); |
144 |
> |
try { |
145 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (InterruptedException success) {} |
148 |
> |
assertFalse(Thread.interrupted()); |
149 |
> |
|
150 |
> |
pleaseInterrupt.countDown(); |
151 |
> |
try { |
152 |
> |
l.await(LONG_DELAY_MS, MILLISECONDS); |
153 |
> |
shouldThrow(); |
154 |
> |
} catch (InterruptedException success) {} |
155 |
> |
assertFalse(Thread.interrupted()); |
156 |
> |
|
157 |
> |
assertEquals(1, l.getCount()); |
158 |
> |
}}); |
159 |
> |
|
160 |
> |
await(pleaseInterrupt); |
161 |
> |
assertThreadStaysAlive(t); |
162 |
> |
t.interrupt(); |
163 |
> |
awaitTermination(t); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
|
* timed await times out if not counted down before timeout |
168 |
|
*/ |
169 |
< |
public void testAwaitTimeout() { |
169 |
> |
public void testAwaitTimeout() throws InterruptedException { |
170 |
|
final CountDownLatch l = new CountDownLatch(1); |
171 |
< |
Thread t = new Thread(new Runnable() { |
172 |
< |
public void run() { |
173 |
< |
try { |
174 |
< |
threadAssertTrue(l.getCount() > 0); |
175 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
176 |
< |
threadAssertTrue(l.getCount() > 0); |
177 |
< |
} catch(InterruptedException ie){ |
178 |
< |
threadUnexpectedException(); |
179 |
< |
} |
178 |
< |
} |
179 |
< |
}); |
180 |
< |
t.start(); |
181 |
< |
try { |
182 |
< |
assertEquals(l.getCount(), 1); |
183 |
< |
t.join(); |
184 |
< |
} catch (InterruptedException e){ |
185 |
< |
unexpectedException(); |
186 |
< |
} |
171 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
172 |
> |
public void realRun() throws InterruptedException { |
173 |
> |
assertEquals(1, l.getCount()); |
174 |
> |
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); |
175 |
> |
assertEquals(1, l.getCount()); |
176 |
> |
}}); |
177 |
> |
|
178 |
> |
awaitTermination(t); |
179 |
> |
assertEquals(1, l.getCount()); |
180 |
|
} |
181 |
|
|
182 |
|
/** |
184 |
|
*/ |
185 |
|
public void testToString() { |
186 |
|
CountDownLatch s = new CountDownLatch(2); |
187 |
< |
String us = s.toString(); |
195 |
< |
assertTrue(us.indexOf("Count = 2") >= 0); |
187 |
> |
assertTrue(s.toString().contains("Count = 2")); |
188 |
|
s.countDown(); |
189 |
< |
String s1 = s.toString(); |
198 |
< |
assertTrue(s1.indexOf("Count = 1") >= 0); |
189 |
> |
assertTrue(s.toString().contains("Count = 1")); |
190 |
|
s.countDown(); |
191 |
< |
String s2 = s.toString(); |
201 |
< |
assertTrue(s2.indexOf("Count = 0") >= 0); |
191 |
> |
assertTrue(s.toString().contains("Count = 0")); |
192 |
|
} |
193 |
|
|
194 |
|
} |