1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
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.*; |
12 |
|
|
13 |
< |
public class CountDownLatchTest extends TestCase{ |
13 |
< |
|
13 |
> |
public class CountDownLatchTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
17 |
– |
|
18 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(CountDownLatchTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
private static long SHORT_DELAY_MS = 100; |
22 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
23 |
< |
private static long LONG_DELAY_MS = 10000; |
21 |
> |
/** |
22 |
> |
* negative constructor argument throws IAE |
23 |
> |
*/ |
24 |
> |
public void testConstructor() { |
25 |
> |
try { |
26 |
> |
new CountDownLatch(-1); |
27 |
> |
shouldThrow(); |
28 |
> |
} catch (IllegalArgumentException success) {} |
29 |
> |
} |
30 |
|
|
31 |
< |
public void testGetCount(){ |
31 |
> |
/** |
32 |
> |
* getCount returns initial count and decreases after countDown |
33 |
> |
*/ |
34 |
> |
public void testGetCount() { |
35 |
|
final CountDownLatch l = new CountDownLatch(2); |
36 |
|
assertEquals(2, l.getCount()); |
37 |
|
l.countDown(); |
38 |
|
assertEquals(1, l.getCount()); |
39 |
|
} |
40 |
|
|
41 |
< |
public void testAwait1(){ |
41 |
> |
/** |
42 |
> |
* countDown decrements count when positive and has no effect when zero |
43 |
> |
*/ |
44 |
> |
public void testCountDown() { |
45 |
> |
final CountDownLatch l = new CountDownLatch(1); |
46 |
> |
assertEquals(1, l.getCount()); |
47 |
> |
l.countDown(); |
48 |
> |
assertEquals(0, l.getCount()); |
49 |
> |
l.countDown(); |
50 |
> |
assertEquals(0, l.getCount()); |
51 |
> |
} |
52 |
> |
|
53 |
> |
/** |
54 |
> |
* await returns after countDown to zero, but not before |
55 |
> |
*/ |
56 |
> |
public void testAwait() { |
57 |
|
final CountDownLatch l = new CountDownLatch(2); |
58 |
|
|
59 |
< |
Thread t = new Thread(new Runnable(){ |
60 |
< |
public void run(){ |
61 |
< |
try{ |
59 |
> |
Thread t = new Thread(new Runnable() { |
60 |
> |
public void run() { |
61 |
> |
try { |
62 |
> |
threadAssertTrue(l.getCount() > 0); |
63 |
|
l.await(); |
64 |
< |
}catch(InterruptedException e){ |
65 |
< |
fail("unexpected exception"); |
64 |
> |
threadAssertTrue(l.getCount() == 0); |
65 |
> |
} catch (InterruptedException e) { |
66 |
> |
threadUnexpectedException(); |
67 |
|
} |
68 |
|
} |
69 |
|
}); |
70 |
|
t.start(); |
71 |
< |
try{ |
71 |
> |
try { |
72 |
|
assertEquals(l.getCount(), 2); |
73 |
|
Thread.sleep(SHORT_DELAY_MS); |
74 |
|
l.countDown(); |
76 |
|
l.countDown(); |
77 |
|
assertEquals(l.getCount(), 0); |
78 |
|
t.join(); |
79 |
< |
}catch (InterruptedException e){ |
80 |
< |
fail("unexpected exception"); |
79 |
> |
} catch (InterruptedException e) { |
80 |
> |
unexpectedException(); |
81 |
|
} |
82 |
|
} |
59 |
– |
|
83 |
|
|
84 |
|
|
85 |
< |
public void testConstructor(){ |
86 |
< |
try{ |
87 |
< |
new CountDownLatch(-1); |
88 |
< |
fail("should throw IllegalArgumentException"); |
89 |
< |
}catch(IllegalArgumentException success){} |
85 |
> |
/** |
86 |
> |
* timed await returns after countDown to zero |
87 |
> |
*/ |
88 |
> |
public void testTimedAwait() { |
89 |
> |
final CountDownLatch l = new CountDownLatch(2); |
90 |
> |
|
91 |
> |
Thread t = new Thread(new Runnable() { |
92 |
> |
public void run() { |
93 |
> |
try { |
94 |
> |
threadAssertTrue(l.getCount() > 0); |
95 |
> |
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
96 |
> |
} catch (InterruptedException e) { |
97 |
> |
threadUnexpectedException(); |
98 |
> |
} |
99 |
> |
} |
100 |
> |
}); |
101 |
> |
t.start(); |
102 |
> |
try { |
103 |
> |
assertEquals(l.getCount(), 2); |
104 |
> |
Thread.sleep(SHORT_DELAY_MS); |
105 |
> |
l.countDown(); |
106 |
> |
assertEquals(l.getCount(), 1); |
107 |
> |
l.countDown(); |
108 |
> |
assertEquals(l.getCount(), 0); |
109 |
> |
t.join(); |
110 |
> |
} catch (InterruptedException e) { |
111 |
> |
unexpectedException(); |
112 |
> |
} |
113 |
|
} |
114 |
|
|
115 |
< |
public void testAwait1_InterruptedException(){ |
115 |
> |
/** |
116 |
> |
* await throws IE if interrupted before counted down |
117 |
> |
*/ |
118 |
> |
public void testAwait_InterruptedException() { |
119 |
|
final CountDownLatch l = new CountDownLatch(1); |
120 |
< |
Thread t = new Thread(new Runnable(){ |
121 |
< |
public void run(){ |
122 |
< |
try{ |
120 |
> |
Thread t = new Thread(new Runnable() { |
121 |
> |
public void run() { |
122 |
> |
try { |
123 |
> |
threadAssertTrue(l.getCount() > 0); |
124 |
|
l.await(); |
125 |
< |
fail("should throw"); |
126 |
< |
}catch(InterruptedException success){} |
125 |
> |
threadShouldThrow(); |
126 |
> |
} catch (InterruptedException success) {} |
127 |
|
} |
128 |
|
}); |
129 |
|
t.start(); |
130 |
< |
try{ |
130 |
> |
try { |
131 |
|
assertEquals(l.getCount(), 1); |
132 |
|
t.interrupt(); |
133 |
|
t.join(); |
134 |
< |
}catch (InterruptedException e){ |
135 |
< |
fail("unexpected exception"); |
134 |
> |
} catch (InterruptedException e) { |
135 |
> |
unexpectedException(); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
< |
public void testAwait2_InterruptedException(){ |
139 |
> |
/** |
140 |
> |
* timed await throws IE if interrupted before counted down |
141 |
> |
*/ |
142 |
> |
public void testTimedAwait_InterruptedException() { |
143 |
|
final CountDownLatch l = new CountDownLatch(1); |
144 |
< |
Thread t = new Thread(new Runnable(){ |
145 |
< |
public void run(){ |
146 |
< |
try{ |
144 |
> |
Thread t = new Thread(new Runnable() { |
145 |
> |
public void run() { |
146 |
> |
try { |
147 |
> |
threadAssertTrue(l.getCount() > 0); |
148 |
|
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
149 |
< |
fail("should throw"); |
150 |
< |
}catch(InterruptedException success){} |
149 |
> |
threadShouldThrow(); |
150 |
> |
} catch (InterruptedException success) {} |
151 |
|
} |
152 |
|
}); |
153 |
|
t.start(); |
154 |
< |
try{ |
154 |
> |
try { |
155 |
|
Thread.sleep(SHORT_DELAY_MS); |
156 |
|
assertEquals(l.getCount(), 1); |
157 |
|
t.interrupt(); |
158 |
|
t.join(); |
159 |
< |
}catch (InterruptedException e){ |
160 |
< |
fail("unexpected exception"); |
159 |
> |
} catch (InterruptedException e) { |
160 |
> |
unexpectedException(); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
< |
public void testAwaitTimeout(){ |
164 |
> |
/** |
165 |
> |
* timed await times out if not counted down before timeout |
166 |
> |
*/ |
167 |
> |
public void testAwaitTimeout() { |
168 |
|
final CountDownLatch l = new CountDownLatch(1); |
169 |
< |
Thread t = new Thread(new Runnable(){ |
170 |
< |
public void run(){ |
171 |
< |
try{ |
172 |
< |
assertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
173 |
< |
}catch(InterruptedException ie){ |
174 |
< |
fail("unexpected exception"); |
169 |
> |
Thread t = new Thread(new Runnable() { |
170 |
> |
public void run() { |
171 |
> |
try { |
172 |
> |
threadAssertTrue(l.getCount() > 0); |
173 |
> |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
174 |
> |
threadAssertTrue(l.getCount() > 0); |
175 |
> |
} catch (InterruptedException ie) { |
176 |
> |
threadUnexpectedException(); |
177 |
|
} |
178 |
|
} |
179 |
|
}); |
180 |
|
t.start(); |
181 |
< |
try{ |
181 |
> |
try { |
182 |
|
assertEquals(l.getCount(), 1); |
183 |
|
t.join(); |
184 |
< |
}catch (InterruptedException e){ |
185 |
< |
fail("unexpected exception"); |
184 |
> |
} catch (InterruptedException e) { |
185 |
> |
unexpectedException(); |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
+ |
/** |
190 |
+ |
* toString indicates current count |
191 |
+ |
*/ |
192 |
+ |
public void testToString() { |
193 |
+ |
CountDownLatch s = new CountDownLatch(2); |
194 |
+ |
String us = s.toString(); |
195 |
+ |
assertTrue(us.indexOf("Count = 2") >= 0); |
196 |
+ |
s.countDown(); |
197 |
+ |
String s1 = s.toString(); |
198 |
+ |
assertTrue(s1.indexOf("Count = 1") >= 0); |
199 |
+ |
s.countDown(); |
200 |
+ |
String s2 = s.toString(); |
201 |
+ |
assertTrue(s2.indexOf("Count = 0") >= 0); |
202 |
+ |
} |
203 |
+ |
|
204 |
|
} |