9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
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 |
|
|
60 |
< |
Thread t = new Thread(new Runnable() { |
61 |
< |
public void run() { |
62 |
< |
try { |
60 |
> |
Thread t = new Thread(new Runnable() { |
61 |
> |
public void run() { |
62 |
> |
try { |
63 |
|
threadAssertTrue(l.getCount() > 0); |
64 |
< |
l.await(); |
64 |
> |
l.await(); |
65 |
|
threadAssertTrue(l.getCount() == 0); |
66 |
< |
} catch (InterruptedException e){ |
66 |
> |
} catch (InterruptedException e) { |
67 |
|
threadUnexpectedException(); |
68 |
|
} |
69 |
< |
} |
70 |
< |
}); |
71 |
< |
t.start(); |
72 |
< |
try { |
69 |
> |
} |
70 |
> |
}); |
71 |
> |
t.start(); |
72 |
> |
try { |
73 |
|
assertEquals(l.getCount(), 2); |
74 |
|
Thread.sleep(SHORT_DELAY_MS); |
75 |
|
l.countDown(); |
77 |
|
l.countDown(); |
78 |
|
assertEquals(l.getCount(), 0); |
79 |
|
t.join(); |
80 |
< |
} catch (InterruptedException e){ |
80 |
> |
} catch (InterruptedException e) { |
81 |
|
unexpectedException(); |
82 |
|
} |
83 |
|
} |
87 |
|
* timed await returns after countDown to zero |
88 |
|
*/ |
89 |
|
public void testTimedAwait() { |
90 |
< |
final CountDownLatch l = new CountDownLatch(2); |
90 |
> |
final CountDownLatch l = new CountDownLatch(2); |
91 |
|
|
92 |
< |
Thread t = new Thread(new Runnable() { |
93 |
< |
public void run() { |
94 |
< |
try { |
92 |
> |
Thread t = new Thread(new Runnable() { |
93 |
> |
public void run() { |
94 |
> |
try { |
95 |
|
threadAssertTrue(l.getCount() > 0); |
96 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
97 |
< |
} catch (InterruptedException e){ |
96 |
> |
threadAssertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
97 |
> |
} catch (InterruptedException e) { |
98 |
|
threadUnexpectedException(); |
99 |
|
} |
100 |
< |
} |
101 |
< |
}); |
102 |
< |
t.start(); |
103 |
< |
try { |
100 |
> |
} |
101 |
> |
}); |
102 |
> |
t.start(); |
103 |
> |
try { |
104 |
|
assertEquals(l.getCount(), 2); |
105 |
|
Thread.sleep(SHORT_DELAY_MS); |
106 |
|
l.countDown(); |
108 |
|
l.countDown(); |
109 |
|
assertEquals(l.getCount(), 0); |
110 |
|
t.join(); |
111 |
< |
} catch (InterruptedException e){ |
111 |
> |
} catch (InterruptedException e) { |
112 |
|
unexpectedException(); |
113 |
|
} |
114 |
|
} |
124 |
|
threadAssertTrue(l.getCount() > 0); |
125 |
|
l.await(); |
126 |
|
threadShouldThrow(); |
127 |
< |
} catch (InterruptedException success){} |
127 |
> |
} catch (InterruptedException success) {} |
128 |
|
} |
129 |
|
}); |
130 |
< |
t.start(); |
131 |
< |
try { |
130 |
> |
t.start(); |
131 |
> |
try { |
132 |
|
assertEquals(l.getCount(), 1); |
133 |
|
t.interrupt(); |
134 |
|
t.join(); |
135 |
< |
} catch (InterruptedException e){ |
135 |
> |
} catch (InterruptedException e) { |
136 |
|
unexpectedException(); |
137 |
|
} |
138 |
|
} |
146 |
|
public void run() { |
147 |
|
try { |
148 |
|
threadAssertTrue(l.getCount() > 0); |
149 |
< |
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
149 |
> |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
150 |
|
threadShouldThrow(); |
151 |
< |
} catch (InterruptedException success){} |
151 |
> |
} catch (InterruptedException success) {} |
152 |
|
} |
153 |
|
}); |
154 |
|
t.start(); |
157 |
|
assertEquals(l.getCount(), 1); |
158 |
|
t.interrupt(); |
159 |
|
t.join(); |
160 |
< |
} catch (InterruptedException e){ |
160 |
> |
} catch (InterruptedException e) { |
161 |
|
unexpectedException(); |
162 |
|
} |
163 |
|
} |
171 |
|
public void run() { |
172 |
|
try { |
173 |
|
threadAssertTrue(l.getCount() > 0); |
174 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
174 |
> |
threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
175 |
|
threadAssertTrue(l.getCount() > 0); |
176 |
< |
} catch (InterruptedException ie){ |
176 |
> |
} catch (InterruptedException ie) { |
177 |
|
threadUnexpectedException(); |
178 |
|
} |
179 |
|
} |
182 |
|
try { |
183 |
|
assertEquals(l.getCount(), 1); |
184 |
|
t.join(); |
185 |
< |
} catch (InterruptedException e){ |
185 |
> |
} catch (InterruptedException e) { |
186 |
|
unexpectedException(); |
187 |
|
} |
188 |
|
} |