ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.20
Committed: Sat May 28 12:37:00 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +72 -41 lines
Log Message:
various test case improvements

File Contents

# User Rev Content
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.20 public void testAwait() {
58 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
59 jsr166 1.20 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
60 dl 1.1
61 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
62 jsr166 1.13 public void realRun() throws InterruptedException {
63 jsr166 1.20 assertEquals(2, l.getCount());
64     pleaseCountDown.countDown();
65 jsr166 1.13 l.await();
66 jsr166 1.15 assertEquals(0, l.getCount());
67 jsr166 1.13 }});
68    
69 jsr166 1.20 await(pleaseCountDown);
70     assertEquals(2, l.getCount());
71 jsr166 1.13 l.countDown();
72 jsr166 1.20 assertEquals(1, l.getCount());
73     assertThreadStaysAlive(t);
74 jsr166 1.13 l.countDown();
75 jsr166 1.20 assertEquals(0, l.getCount());
76     awaitTermination(t);
77 dl 1.1 }
78 jsr166 1.8
79 dl 1.3 /**
80 dl 1.4 * timed await returns after countDown to zero
81 dl 1.3 */
82 jsr166 1.20 public void testTimedAwait() {
83 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
84 jsr166 1.20 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
85 dl 1.1
86 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
87 jsr166 1.13 public void realRun() throws InterruptedException {
88 jsr166 1.20 assertEquals(2, l.getCount());
89     pleaseCountDown.countDown();
90     assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS));
91     assertEquals(0, l.getCount());
92 jsr166 1.13 }});
93    
94 jsr166 1.20 await(pleaseCountDown);
95     assertEquals(2, l.getCount());
96 jsr166 1.13 l.countDown();
97 jsr166 1.20 assertEquals(1, l.getCount());
98     assertThreadStaysAlive(t);
99 jsr166 1.13 l.countDown();
100 jsr166 1.20 assertEquals(0, l.getCount());
101     awaitTermination(t);
102 dl 1.1 }
103 jsr166 1.8
104 dl 1.3 /**
105 dl 1.5 * await throws IE if interrupted before counted down
106 dl 1.3 */
107 jsr166 1.20 public void testAwait_Interruptible() {
108 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
109 jsr166 1.20 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
110     Thread t = newStartedThread(new CheckedRunnable() {
111 jsr166 1.13 public void realRun() throws InterruptedException {
112 jsr166 1.20 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 jsr166 1.13 }});
128    
129 jsr166 1.20 await(pleaseInterrupt);
130     assertThreadStaysAlive(t);
131 jsr166 1.13 t.interrupt();
132 jsr166 1.20 awaitTermination(t);
133 dl 1.1 }
134    
135 dl 1.3 /**
136 dl 1.5 * timed await throws IE if interrupted before counted down
137 dl 1.3 */
138 jsr166 1.20 public void testTimedAwait_Interruptible() {
139 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
140 jsr166 1.20 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
141     Thread t = newStartedThread(new CheckedRunnable() {
142 jsr166 1.13 public void realRun() throws InterruptedException {
143 jsr166 1.20 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 jsr166 1.13 }});
159    
160 jsr166 1.20 await(pleaseInterrupt);
161     assertThreadStaysAlive(t);
162 jsr166 1.13 t.interrupt();
163 jsr166 1.20 awaitTermination(t);
164 dl 1.1 }
165    
166 dl 1.3 /**
167 dl 1.4 * timed await times out if not counted down before timeout
168 dl 1.3 */
169 jsr166 1.13 public void testAwaitTimeout() throws InterruptedException {
170 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
171 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
172 jsr166 1.13 public void realRun() throws InterruptedException {
173 jsr166 1.20 assertEquals(1, l.getCount());
174     assertFalse(l.await(timeoutMillis(), MILLISECONDS));
175     assertEquals(1, l.getCount());
176 jsr166 1.13 }});
177    
178 jsr166 1.20 awaitTermination(t);
179     assertEquals(1, l.getCount());
180 dl 1.1 }
181    
182 dl 1.7 /**
183     * toString indicates current count
184     */
185     public void testToString() {
186     CountDownLatch s = new CountDownLatch(2);
187 jsr166 1.18 assertTrue(s.toString().contains("Count = 2"));
188 dl 1.7 s.countDown();
189 jsr166 1.18 assertTrue(s.toString().contains("Count = 1"));
190 dl 1.7 s.countDown();
191 jsr166 1.18 assertTrue(s.toString().contains("Count = 0"));
192 dl 1.7 }
193    
194 dl 1.1 }