ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.25
Committed: Sun May 14 01:37:52 2017 UTC (7 years ago) by jsr166
Branch: MAIN
Changes since 1.24: +2 -2 lines
Log Message:
improve test descriptions

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 jsr166 1.22 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10    
11 jsr166 1.21 import java.util.concurrent.CountDownLatch;
12 jsr166 1.22
13     import junit.framework.Test;
14     import junit.framework.TestSuite;
15 dl 1.1
16 dl 1.2 public class CountDownLatchTest extends JSR166TestCase {
17 dl 1.1 public static void main(String[] args) {
18 jsr166 1.23 main(suite(), args);
19 dl 1.1 }
20     public static Test suite() {
21 jsr166 1.11 return new TestSuite(CountDownLatchTest.class);
22 dl 1.1 }
23    
24 dl 1.3 /**
25 dl 1.4 * negative constructor argument throws IAE
26 dl 1.3 */
27     public void testConstructor() {
28 dl 1.2 try {
29     new CountDownLatch(-1);
30 dl 1.3 shouldThrow();
31 jsr166 1.10 } catch (IllegalArgumentException success) {}
32 dl 1.2 }
33 dl 1.1
34 dl 1.3 /**
35 dl 1.4 * getCount returns initial count and decreases after countDown
36 dl 1.3 */
37     public void testGetCount() {
38 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
39     assertEquals(2, l.getCount());
40     l.countDown();
41     assertEquals(1, l.getCount());
42 dl 1.1 }
43    
44 dl 1.3 /**
45 dl 1.5 * countDown decrements count when positive and has no effect when zero
46 dl 1.4 */
47 dl 1.5 public void testCountDown() {
48 jsr166 1.11 final CountDownLatch l = new CountDownLatch(1);
49     assertEquals(1, l.getCount());
50     l.countDown();
51     assertEquals(0, l.getCount());
52     l.countDown();
53     assertEquals(0, l.getCount());
54 dl 1.4 }
55    
56     /**
57     * await returns after countDown to zero, but not before
58 dl 1.3 */
59 jsr166 1.20 public void testAwait() {
60 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
61 jsr166 1.20 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
62 dl 1.1
63 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
64 jsr166 1.13 public void realRun() throws InterruptedException {
65 jsr166 1.20 assertEquals(2, l.getCount());
66     pleaseCountDown.countDown();
67 jsr166 1.13 l.await();
68 jsr166 1.15 assertEquals(0, l.getCount());
69 jsr166 1.13 }});
70    
71 jsr166 1.20 await(pleaseCountDown);
72     assertEquals(2, l.getCount());
73 jsr166 1.13 l.countDown();
74 jsr166 1.20 assertEquals(1, l.getCount());
75 jsr166 1.24 assertThreadBlocks(t, Thread.State.WAITING);
76 jsr166 1.13 l.countDown();
77 jsr166 1.20 assertEquals(0, l.getCount());
78     awaitTermination(t);
79 dl 1.1 }
80 jsr166 1.8
81 dl 1.3 /**
82 dl 1.4 * timed await returns after countDown to zero
83 dl 1.3 */
84 jsr166 1.20 public void testTimedAwait() {
85 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
86 jsr166 1.20 final CountDownLatch pleaseCountDown = new CountDownLatch(1);
87 dl 1.1
88 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
89 jsr166 1.13 public void realRun() throws InterruptedException {
90 jsr166 1.20 assertEquals(2, l.getCount());
91     pleaseCountDown.countDown();
92     assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS));
93     assertEquals(0, l.getCount());
94 jsr166 1.13 }});
95    
96 jsr166 1.20 await(pleaseCountDown);
97     assertEquals(2, l.getCount());
98 jsr166 1.13 l.countDown();
99 jsr166 1.20 assertEquals(1, l.getCount());
100 jsr166 1.24 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
101 jsr166 1.13 l.countDown();
102 jsr166 1.20 assertEquals(0, l.getCount());
103     awaitTermination(t);
104 dl 1.1 }
105 jsr166 1.8
106 dl 1.3 /**
107 jsr166 1.25 * await throws InterruptedException if interrupted before counted down
108 dl 1.3 */
109 jsr166 1.20 public void testAwait_Interruptible() {
110 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
111 jsr166 1.20 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
112     Thread t = newStartedThread(new CheckedRunnable() {
113 jsr166 1.13 public void realRun() throws InterruptedException {
114 jsr166 1.20 Thread.currentThread().interrupt();
115     try {
116     l.await();
117     shouldThrow();
118     } catch (InterruptedException success) {}
119     assertFalse(Thread.interrupted());
120    
121     pleaseInterrupt.countDown();
122     try {
123     l.await();
124     shouldThrow();
125     } catch (InterruptedException success) {}
126     assertFalse(Thread.interrupted());
127    
128     assertEquals(1, l.getCount());
129 jsr166 1.13 }});
130    
131 jsr166 1.20 await(pleaseInterrupt);
132 jsr166 1.24 assertThreadBlocks(t, Thread.State.WAITING);
133 jsr166 1.13 t.interrupt();
134 jsr166 1.20 awaitTermination(t);
135 dl 1.1 }
136    
137 dl 1.3 /**
138 jsr166 1.25 * timed await throws InterruptedException if interrupted before counted down
139 dl 1.3 */
140 jsr166 1.20 public void testTimedAwait_Interruptible() {
141 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
142 jsr166 1.20 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
143     Thread t = newStartedThread(new CheckedRunnable() {
144 jsr166 1.13 public void realRun() throws InterruptedException {
145 jsr166 1.20 Thread.currentThread().interrupt();
146     try {
147     l.await(LONG_DELAY_MS, MILLISECONDS);
148     shouldThrow();
149     } catch (InterruptedException success) {}
150     assertFalse(Thread.interrupted());
151    
152     pleaseInterrupt.countDown();
153     try {
154     l.await(LONG_DELAY_MS, MILLISECONDS);
155     shouldThrow();
156     } catch (InterruptedException success) {}
157     assertFalse(Thread.interrupted());
158    
159     assertEquals(1, l.getCount());
160 jsr166 1.13 }});
161    
162 jsr166 1.20 await(pleaseInterrupt);
163 jsr166 1.24 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
164 jsr166 1.13 t.interrupt();
165 jsr166 1.20 awaitTermination(t);
166 dl 1.1 }
167    
168 dl 1.3 /**
169 dl 1.4 * timed await times out if not counted down before timeout
170 dl 1.3 */
171 jsr166 1.13 public void testAwaitTimeout() throws InterruptedException {
172 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
173 jsr166 1.20 Thread t = newStartedThread(new CheckedRunnable() {
174 jsr166 1.13 public void realRun() throws InterruptedException {
175 jsr166 1.20 assertEquals(1, l.getCount());
176     assertFalse(l.await(timeoutMillis(), MILLISECONDS));
177     assertEquals(1, l.getCount());
178 jsr166 1.13 }});
179    
180 jsr166 1.20 awaitTermination(t);
181     assertEquals(1, l.getCount());
182 dl 1.1 }
183    
184 dl 1.7 /**
185     * toString indicates current count
186     */
187     public void testToString() {
188     CountDownLatch s = new CountDownLatch(2);
189 jsr166 1.18 assertTrue(s.toString().contains("Count = 2"));
190 dl 1.7 s.countDown();
191 jsr166 1.18 assertTrue(s.toString().contains("Count = 1"));
192 dl 1.7 s.countDown();
193 jsr166 1.18 assertTrue(s.toString().contains("Count = 0"));
194 dl 1.7 }
195    
196 dl 1.1 }