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/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.*; |
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 |
|
/** |
23 |
< |
* |
23 |
> |
* negative constructor argument throws IAE |
24 |
|
*/ |
25 |
|
public void testConstructor() { |
26 |
|
try { |
27 |
|
new CountDownLatch(-1); |
28 |
|
shouldThrow(); |
29 |
< |
} catch(IllegalArgumentException success){} |
29 |
> |
} catch (IllegalArgumentException success) {} |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* |
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()); |
38 |
< |
} |
39 |
< |
|
40 |
< |
/** |
41 |
< |
* |
42 |
< |
*/ |
43 |
< |
public void testAwait() { |
44 |
< |
final CountDownLatch l = new CountDownLatch(2); |
45 |
< |
|
46 |
< |
Thread t = new Thread(new Runnable() { |
47 |
< |
public void run() { |
48 |
< |
try { |
49 |
< |
l.await(); |
50 |
< |
} catch(InterruptedException e){ |
51 |
< |
threadUnexpectedException(); |
52 |
< |
} |
53 |
< |
} |
54 |
< |
}); |
55 |
< |
t.start(); |
56 |
< |
try { |
57 |
< |
assertEquals(l.getCount(), 2); |
58 |
< |
Thread.sleep(SHORT_DELAY_MS); |
59 |
< |
l.countDown(); |
60 |
< |
assertEquals(l.getCount(), 1); |
61 |
< |
l.countDown(); |
62 |
< |
assertEquals(l.getCount(), 0); |
63 |
< |
t.join(); |
64 |
< |
} catch (InterruptedException e){ |
65 |
< |
unexpectedException(); |
66 |
< |
} |
67 |
< |
} |
68 |
< |
|
69 |
< |
|
70 |
< |
/** |
71 |
< |
* |
72 |
< |
*/ |
73 |
< |
public void testTimedAwait() { |
74 |
< |
final CountDownLatch l = new CountDownLatch(2); |
75 |
< |
|
76 |
< |
Thread t = new Thread(new Runnable() { |
77 |
< |
public void run() { |
78 |
< |
try { |
79 |
< |
threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS)); |
80 |
< |
} catch(InterruptedException e){ |
81 |
< |
threadUnexpectedException(); |
82 |
< |
} |
83 |
< |
} |
84 |
< |
}); |
85 |
< |
t.start(); |
86 |
< |
try { |
87 |
< |
assertEquals(l.getCount(), 2); |
88 |
< |
Thread.sleep(SHORT_DELAY_MS); |
89 |
< |
l.countDown(); |
90 |
< |
assertEquals(l.getCount(), 1); |
91 |
< |
l.countDown(); |
92 |
< |
assertEquals(l.getCount(), 0); |
93 |
< |
t.join(); |
94 |
< |
} catch (InterruptedException e){ |
95 |
< |
unexpectedException(); |
96 |
< |
} |
36 |
> |
final CountDownLatch l = new CountDownLatch(2); |
37 |
> |
assertEquals(2, l.getCount()); |
38 |
> |
l.countDown(); |
39 |
> |
assertEquals(1, l.getCount()); |
40 |
|
} |
98 |
– |
|
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()); |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
/** |
55 |
+ |
* await returns after countDown to zero, but not before |
56 |
+ |
*/ |
57 |
+ |
public void testAwait() throws InterruptedException { |
58 |
+ |
final CountDownLatch l = new CountDownLatch(2); |
59 |
+ |
|
60 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
61 |
+ |
public void realRun() throws InterruptedException { |
62 |
+ |
assertTrue(l.getCount() > 0); |
63 |
+ |
l.await(); |
64 |
+ |
assertEquals(0, l.getCount()); |
65 |
+ |
}}); |
66 |
+ |
|
67 |
+ |
t.start(); |
68 |
+ |
assertEquals(l.getCount(), 2); |
69 |
+ |
delay(SHORT_DELAY_MS); |
70 |
+ |
l.countDown(); |
71 |
+ |
assertEquals(l.getCount(), 1); |
72 |
+ |
l.countDown(); |
73 |
+ |
assertEquals(l.getCount(), 0); |
74 |
+ |
t.join(); |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
|
78 |
+ |
/** |
79 |
+ |
* timed await returns after countDown to zero |
80 |
+ |
*/ |
81 |
+ |
public void testTimedAwait() throws InterruptedException { |
82 |
+ |
final CountDownLatch l = new CountDownLatch(2); |
83 |
+ |
|
84 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
85 |
+ |
public void realRun() throws InterruptedException { |
86 |
+ |
assertTrue(l.getCount() > 0); |
87 |
+ |
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
+ |
}}); |
89 |
|
|
90 |
+ |
t.start(); |
91 |
+ |
assertEquals(l.getCount(), 2); |
92 |
+ |
delay(SHORT_DELAY_MS); |
93 |
+ |
l.countDown(); |
94 |
+ |
assertEquals(l.getCount(), 1); |
95 |
+ |
l.countDown(); |
96 |
+ |
assertEquals(l.getCount(), 0); |
97 |
+ |
t.join(); |
98 |
+ |
} |
99 |
|
|
100 |
|
/** |
101 |
< |
* |
101 |
> |
* await throws IE if interrupted before counted down |
102 |
|
*/ |
103 |
< |
public void testAwait_InterruptedException() { |
103 |
> |
public void testAwait_InterruptedException() throws InterruptedException { |
104 |
|
final CountDownLatch l = new CountDownLatch(1); |
105 |
< |
Thread t = new Thread(new Runnable() { |
106 |
< |
public void run() { |
107 |
< |
try { |
108 |
< |
l.await(); |
109 |
< |
threadShouldThrow(); |
110 |
< |
} catch(InterruptedException success){} |
111 |
< |
} |
112 |
< |
}); |
113 |
< |
t.start(); |
114 |
< |
try { |
117 |
< |
assertEquals(l.getCount(), 1); |
118 |
< |
t.interrupt(); |
119 |
< |
t.join(); |
120 |
< |
} catch (InterruptedException e){ |
121 |
< |
unexpectedException(); |
122 |
< |
} |
105 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
106 |
> |
public void realRun() throws InterruptedException { |
107 |
> |
assertTrue(l.getCount() > 0); |
108 |
> |
l.await(); |
109 |
> |
}}); |
110 |
> |
|
111 |
> |
t.start(); |
112 |
> |
assertEquals(l.getCount(), 1); |
113 |
> |
t.interrupt(); |
114 |
> |
t.join(); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* |
118 |
> |
* timed await throws IE if interrupted before counted down |
119 |
|
*/ |
120 |
< |
public void testTimedAwait_InterruptedException() { |
120 |
> |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
121 |
|
final CountDownLatch l = new CountDownLatch(1); |
122 |
< |
Thread t = new Thread(new Runnable() { |
123 |
< |
public void run() { |
124 |
< |
try { |
125 |
< |
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
126 |
< |
threadShouldThrow(); |
127 |
< |
} catch(InterruptedException success){} |
136 |
< |
} |
137 |
< |
}); |
122 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
123 |
> |
public void realRun() throws InterruptedException { |
124 |
> |
assertTrue(l.getCount() > 0); |
125 |
> |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
126 |
> |
}}); |
127 |
> |
|
128 |
|
t.start(); |
129 |
< |
try { |
130 |
< |
Thread.sleep(SHORT_DELAY_MS); |
131 |
< |
assertEquals(l.getCount(), 1); |
132 |
< |
t.interrupt(); |
143 |
< |
t.join(); |
144 |
< |
} catch (InterruptedException e){ |
145 |
< |
unexpectedException(); |
146 |
< |
} |
129 |
> |
delay(SHORT_DELAY_MS); |
130 |
> |
assertEquals(l.getCount(), 1); |
131 |
> |
t.interrupt(); |
132 |
> |
t.join(); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* |
136 |
> |
* timed await times out if not counted down before timeout |
137 |
|
*/ |
138 |
< |
public void testAwaitTimeout() { |
138 |
> |
public void testAwaitTimeout() throws InterruptedException { |
139 |
|
final CountDownLatch l = new CountDownLatch(1); |
140 |
< |
Thread t = new Thread(new Runnable() { |
141 |
< |
public void run() { |
142 |
< |
try { |
143 |
< |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
144 |
< |
} catch(InterruptedException ie){ |
145 |
< |
threadUnexpectedException(); |
146 |
< |
} |
161 |
< |
} |
162 |
< |
}); |
140 |
> |
Thread t = new Thread(new CheckedRunnable() { |
141 |
> |
public void realRun() throws InterruptedException { |
142 |
> |
assertTrue(l.getCount() > 0); |
143 |
> |
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
144 |
> |
assertTrue(l.getCount() > 0); |
145 |
> |
}}); |
146 |
> |
|
147 |
|
t.start(); |
148 |
< |
try { |
149 |
< |
assertEquals(l.getCount(), 1); |
150 |
< |
t.join(); |
151 |
< |
} catch (InterruptedException e){ |
152 |
< |
unexpectedException(); |
153 |
< |
} |
148 |
> |
assertEquals(l.getCount(), 1); |
149 |
> |
t.join(); |
150 |
> |
} |
151 |
> |
|
152 |
> |
/** |
153 |
> |
* toString indicates current count |
154 |
> |
*/ |
155 |
> |
public void testToString() { |
156 |
> |
CountDownLatch s = new CountDownLatch(2); |
157 |
> |
String us = s.toString(); |
158 |
> |
assertTrue(us.indexOf("Count = 2") >= 0); |
159 |
> |
s.countDown(); |
160 |
> |
String s1 = s.toString(); |
161 |
> |
assertTrue(s1.indexOf("Count = 1") >= 0); |
162 |
> |
s.countDown(); |
163 |
> |
String s2 = s.toString(); |
164 |
> |
assertTrue(s2.indexOf("Count = 0") >= 0); |
165 |
|
} |
166 |
|
|
167 |
|
} |