ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.12
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +4 -3 lines
Log Message:
import static TimeUnit.MILLISECONDS

File Contents

# Content
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
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());
17 }
18 public static Test suite() {
19 return new TestSuite(CountDownLatchTest.class);
20 }
21
22 /**
23 * negative constructor argument throws IAE
24 */
25 public void testConstructor() {
26 try {
27 new CountDownLatch(-1);
28 shouldThrow();
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());
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());
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);
59
60 Thread t = new Thread(new Runnable() {
61 public void run() {
62 try {
63 threadAssertTrue(l.getCount() > 0);
64 l.await();
65 threadAssertTrue(l.getCount() == 0);
66 } catch (InterruptedException e) {
67 threadUnexpectedException();
68 }
69 }
70 });
71 t.start();
72 try {
73 assertEquals(l.getCount(), 2);
74 Thread.sleep(SHORT_DELAY_MS);
75 l.countDown();
76 assertEquals(l.getCount(), 1);
77 l.countDown();
78 assertEquals(l.getCount(), 0);
79 t.join();
80 } catch (InterruptedException e) {
81 unexpectedException();
82 }
83 }
84
85
86 /**
87 * timed await returns after countDown to zero
88 */
89 public void testTimedAwait() {
90 final CountDownLatch l = new CountDownLatch(2);
91
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, MILLISECONDS));
97 } catch (InterruptedException e) {
98 threadUnexpectedException();
99 }
100 }
101 });
102 t.start();
103 try {
104 assertEquals(l.getCount(), 2);
105 Thread.sleep(SHORT_DELAY_MS);
106 l.countDown();
107 assertEquals(l.getCount(), 1);
108 l.countDown();
109 assertEquals(l.getCount(), 0);
110 t.join();
111 } catch (InterruptedException e) {
112 unexpectedException();
113 }
114 }
115
116 /**
117 * await throws IE if interrupted before counted down
118 */
119 public void testAwait_InterruptedException() {
120 final CountDownLatch l = new CountDownLatch(1);
121 Thread t = new Thread(new Runnable() {
122 public void run() {
123 try {
124 threadAssertTrue(l.getCount() > 0);
125 l.await();
126 threadShouldThrow();
127 } catch (InterruptedException success) {}
128 }
129 });
130 t.start();
131 try {
132 assertEquals(l.getCount(), 1);
133 t.interrupt();
134 t.join();
135 } catch (InterruptedException e) {
136 unexpectedException();
137 }
138 }
139
140 /**
141 * timed await throws IE if interrupted before counted down
142 */
143 public void testTimedAwait_InterruptedException() {
144 final CountDownLatch l = new CountDownLatch(1);
145 Thread t = new Thread(new Runnable() {
146 public void run() {
147 try {
148 threadAssertTrue(l.getCount() > 0);
149 l.await(MEDIUM_DELAY_MS, MILLISECONDS);
150 threadShouldThrow();
151 } catch (InterruptedException success) {}
152 }
153 });
154 t.start();
155 try {
156 Thread.sleep(SHORT_DELAY_MS);
157 assertEquals(l.getCount(), 1);
158 t.interrupt();
159 t.join();
160 } catch (InterruptedException e) {
161 unexpectedException();
162 }
163 }
164
165 /**
166 * timed await times out if not counted down before timeout
167 */
168 public void testAwaitTimeout() {
169 final CountDownLatch l = new CountDownLatch(1);
170 Thread t = new Thread(new Runnable() {
171 public void run() {
172 try {
173 threadAssertTrue(l.getCount() > 0);
174 threadAssertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
175 threadAssertTrue(l.getCount() > 0);
176 } catch (InterruptedException ie) {
177 threadUnexpectedException();
178 }
179 }
180 });
181 t.start();
182 try {
183 assertEquals(l.getCount(), 1);
184 t.join();
185 } catch (InterruptedException e) {
186 unexpectedException();
187 }
188 }
189
190 /**
191 * toString indicates current count
192 */
193 public void testToString() {
194 CountDownLatch s = new CountDownLatch(2);
195 String us = s.toString();
196 assertTrue(us.indexOf("Count = 2") >= 0);
197 s.countDown();
198 String s1 = s.toString();
199 assertTrue(s1.indexOf("Count = 1") >= 0);
200 s.countDown();
201 String s2 = s.toString();
202 assertTrue(s2.indexOf("Count = 0") >= 0);
203 }
204
205 }