ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.21
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
use serialClone in serialization tests; update imports

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/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.CountDownLatch;
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 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
60
61 Thread t = newStartedThread(new CheckedRunnable() {
62 public void realRun() throws InterruptedException {
63 assertEquals(2, l.getCount());
64 pleaseCountDown.countDown();
65 l.await();
66 assertEquals(0, l.getCount());
67 }});
68
69 await(pleaseCountDown);
70 assertEquals(2, l.getCount());
71 l.countDown();
72 assertEquals(1, l.getCount());
73 assertThreadStaysAlive(t);
74 l.countDown();
75 assertEquals(0, l.getCount());
76 awaitTermination(t);
77 }
78
79 /**
80 * timed await returns after countDown to zero
81 */
82 public void testTimedAwait() {
83 final CountDownLatch l = new CountDownLatch(2);
84 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
85
86 Thread t = newStartedThread(new CheckedRunnable() {
87 public void realRun() throws InterruptedException {
88 assertEquals(2, l.getCount());
89 pleaseCountDown.countDown();
90 assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS));
91 assertEquals(0, l.getCount());
92 }});
93
94 await(pleaseCountDown);
95 assertEquals(2, l.getCount());
96 l.countDown();
97 assertEquals(1, l.getCount());
98 assertThreadStaysAlive(t);
99 l.countDown();
100 assertEquals(0, l.getCount());
101 awaitTermination(t);
102 }
103
104 /**
105 * await throws IE if interrupted before counted down
106 */
107 public void testAwait_Interruptible() {
108 final CountDownLatch l = new CountDownLatch(1);
109 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
110 Thread t = newStartedThread(new CheckedRunnable() {
111 public void realRun() throws InterruptedException {
112 Thread.currentThread().interrupt();
113 try {
114 l.await();
115 shouldThrow();
116 } catch (InterruptedException success) {}
117 assertFalse(Thread.interrupted());
118
119 pleaseInterrupt.countDown();
120 try {
121 l.await();
122 shouldThrow();
123 } catch (InterruptedException success) {}
124 assertFalse(Thread.interrupted());
125
126 assertEquals(1, l.getCount());
127 }});
128
129 await(pleaseInterrupt);
130 assertThreadStaysAlive(t);
131 t.interrupt();
132 awaitTermination(t);
133 }
134
135 /**
136 * timed await throws IE if interrupted before counted down
137 */
138 public void testTimedAwait_Interruptible() {
139 final CountDownLatch l = new CountDownLatch(1);
140 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
141 Thread t = newStartedThread(new CheckedRunnable() {
142 public void realRun() throws InterruptedException {
143 Thread.currentThread().interrupt();
144 try {
145 l.await(LONG_DELAY_MS, MILLISECONDS);
146 shouldThrow();
147 } catch (InterruptedException success) {}
148 assertFalse(Thread.interrupted());
149
150 pleaseInterrupt.countDown();
151 try {
152 l.await(LONG_DELAY_MS, MILLISECONDS);
153 shouldThrow();
154 } catch (InterruptedException success) {}
155 assertFalse(Thread.interrupted());
156
157 assertEquals(1, l.getCount());
158 }});
159
160 await(pleaseInterrupt);
161 assertThreadStaysAlive(t);
162 t.interrupt();
163 awaitTermination(t);
164 }
165
166 /**
167 * timed await times out if not counted down before timeout
168 */
169 public void testAwaitTimeout() throws InterruptedException {
170 final CountDownLatch l = new CountDownLatch(1);
171 Thread t = newStartedThread(new CheckedRunnable() {
172 public void realRun() throws InterruptedException {
173 assertEquals(1, l.getCount());
174 assertFalse(l.await(timeoutMillis(), MILLISECONDS));
175 assertEquals(1, l.getCount());
176 }});
177
178 awaitTermination(t);
179 assertEquals(1, l.getCount());
180 }
181
182 /**
183 * toString indicates current count
184 */
185 public void testToString() {
186 CountDownLatch s = new CountDownLatch(2);
187 assertTrue(s.toString().contains("Count = 2"));
188 s.countDown();
189 assertTrue(s.toString().contains("Count = 1"));
190 s.countDown();
191 assertTrue(s.toString().contains("Count = 0"));
192 }
193
194 }