1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.16 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
jsr166 |
1.12 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.1 |
|
14 |
dl |
1.2 |
public class CountDownLatchTest extends JSR166TestCase { |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.14 |
junit.textui.TestRunner.run(suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
jsr166 |
1.11 |
return new TestSuite(CountDownLatchTest.class); |
20 |
dl |
1.1 |
} |
21 |
|
|
|
22 |
dl |
1.3 |
/** |
23 |
dl |
1.4 |
* negative constructor argument throws IAE |
24 |
dl |
1.3 |
*/ |
25 |
|
|
public void testConstructor() { |
26 |
dl |
1.2 |
try { |
27 |
|
|
new CountDownLatch(-1); |
28 |
dl |
1.3 |
shouldThrow(); |
29 |
jsr166 |
1.10 |
} catch (IllegalArgumentException success) {} |
30 |
dl |
1.2 |
} |
31 |
dl |
1.1 |
|
32 |
dl |
1.3 |
/** |
33 |
dl |
1.4 |
* getCount returns initial count and decreases after countDown |
34 |
dl |
1.3 |
*/ |
35 |
|
|
public void testGetCount() { |
36 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
37 |
|
|
assertEquals(2, l.getCount()); |
38 |
|
|
l.countDown(); |
39 |
|
|
assertEquals(1, l.getCount()); |
40 |
dl |
1.1 |
} |
41 |
|
|
|
42 |
dl |
1.3 |
/** |
43 |
dl |
1.5 |
* countDown decrements count when positive and has no effect when zero |
44 |
dl |
1.4 |
*/ |
45 |
dl |
1.5 |
public void testCountDown() { |
46 |
jsr166 |
1.11 |
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 |
dl |
1.4 |
} |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* await returns after countDown to zero, but not before |
56 |
dl |
1.3 |
*/ |
57 |
jsr166 |
1.13 |
public void testAwait() throws InterruptedException { |
58 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
59 |
dl |
1.1 |
|
60 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedRunnable() { |
61 |
|
|
public void realRun() throws InterruptedException { |
62 |
jsr166 |
1.15 |
assertTrue(l.getCount() > 0); |
63 |
jsr166 |
1.13 |
l.await(); |
64 |
jsr166 |
1.15 |
assertEquals(0, l.getCount()); |
65 |
jsr166 |
1.13 |
}}); |
66 |
|
|
|
67 |
jsr166 |
1.11 |
t.start(); |
68 |
jsr166 |
1.13 |
assertEquals(l.getCount(), 2); |
69 |
dl |
1.17 |
delay(SHORT_DELAY_MS); |
70 |
jsr166 |
1.13 |
l.countDown(); |
71 |
|
|
assertEquals(l.getCount(), 1); |
72 |
|
|
l.countDown(); |
73 |
|
|
assertEquals(l.getCount(), 0); |
74 |
|
|
t.join(); |
75 |
dl |
1.1 |
} |
76 |
jsr166 |
1.8 |
|
77 |
dl |
1.1 |
|
78 |
dl |
1.3 |
/** |
79 |
dl |
1.4 |
* timed await returns after countDown to zero |
80 |
dl |
1.3 |
*/ |
81 |
jsr166 |
1.13 |
public void testTimedAwait() throws InterruptedException { |
82 |
jsr166 |
1.11 |
final CountDownLatch l = new CountDownLatch(2); |
83 |
dl |
1.1 |
|
84 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedRunnable() { |
85 |
|
|
public void realRun() throws InterruptedException { |
86 |
jsr166 |
1.15 |
assertTrue(l.getCount() > 0); |
87 |
|
|
assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
jsr166 |
1.13 |
}}); |
89 |
|
|
|
90 |
jsr166 |
1.11 |
t.start(); |
91 |
jsr166 |
1.13 |
assertEquals(l.getCount(), 2); |
92 |
dl |
1.17 |
delay(SHORT_DELAY_MS); |
93 |
jsr166 |
1.13 |
l.countDown(); |
94 |
|
|
assertEquals(l.getCount(), 1); |
95 |
|
|
l.countDown(); |
96 |
|
|
assertEquals(l.getCount(), 0); |
97 |
|
|
t.join(); |
98 |
dl |
1.1 |
} |
99 |
jsr166 |
1.8 |
|
100 |
dl |
1.3 |
/** |
101 |
dl |
1.5 |
* await throws IE if interrupted before counted down |
102 |
dl |
1.3 |
*/ |
103 |
jsr166 |
1.13 |
public void testAwait_InterruptedException() throws InterruptedException { |
104 |
dl |
1.1 |
final CountDownLatch l = new CountDownLatch(1); |
105 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
106 |
|
|
public void realRun() throws InterruptedException { |
107 |
jsr166 |
1.15 |
assertTrue(l.getCount() > 0); |
108 |
jsr166 |
1.13 |
l.await(); |
109 |
|
|
}}); |
110 |
|
|
|
111 |
jsr166 |
1.11 |
t.start(); |
112 |
jsr166 |
1.13 |
assertEquals(l.getCount(), 1); |
113 |
|
|
t.interrupt(); |
114 |
|
|
t.join(); |
115 |
dl |
1.1 |
} |
116 |
|
|
|
117 |
dl |
1.3 |
/** |
118 |
dl |
1.5 |
* timed await throws IE if interrupted before counted down |
119 |
dl |
1.3 |
*/ |
120 |
jsr166 |
1.13 |
public void testTimedAwait_InterruptedException() throws InterruptedException { |
121 |
dl |
1.1 |
final CountDownLatch l = new CountDownLatch(1); |
122 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
123 |
|
|
public void realRun() throws InterruptedException { |
124 |
jsr166 |
1.15 |
assertTrue(l.getCount() > 0); |
125 |
jsr166 |
1.13 |
l.await(MEDIUM_DELAY_MS, MILLISECONDS); |
126 |
|
|
}}); |
127 |
|
|
|
128 |
dl |
1.1 |
t.start(); |
129 |
dl |
1.17 |
delay(SHORT_DELAY_MS); |
130 |
jsr166 |
1.13 |
assertEquals(l.getCount(), 1); |
131 |
|
|
t.interrupt(); |
132 |
|
|
t.join(); |
133 |
dl |
1.1 |
} |
134 |
|
|
|
135 |
dl |
1.3 |
/** |
136 |
dl |
1.4 |
* timed await times out if not counted down before timeout |
137 |
dl |
1.3 |
*/ |
138 |
jsr166 |
1.13 |
public void testAwaitTimeout() throws InterruptedException { |
139 |
dl |
1.1 |
final CountDownLatch l = new CountDownLatch(1); |
140 |
jsr166 |
1.13 |
Thread t = new Thread(new CheckedRunnable() { |
141 |
|
|
public void realRun() throws InterruptedException { |
142 |
jsr166 |
1.15 |
assertTrue(l.getCount() > 0); |
143 |
|
|
assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); |
144 |
|
|
assertTrue(l.getCount() > 0); |
145 |
jsr166 |
1.13 |
}}); |
146 |
|
|
|
147 |
dl |
1.1 |
t.start(); |
148 |
jsr166 |
1.13 |
assertEquals(l.getCount(), 1); |
149 |
|
|
t.join(); |
150 |
dl |
1.1 |
} |
151 |
|
|
|
152 |
dl |
1.7 |
/** |
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 |
dl |
1.1 |
} |