ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.19
Committed: Fri May 27 19:40:19 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +0 -1 lines
Log Message:
whitespace

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.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.3 /**
78 dl 1.4 * timed await returns after countDown to zero
79 dl 1.3 */
80 jsr166 1.13 public void testTimedAwait() throws InterruptedException {
81 jsr166 1.11 final CountDownLatch l = new CountDownLatch(2);
82 dl 1.1
83 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
84     public void realRun() throws InterruptedException {
85 jsr166 1.15 assertTrue(l.getCount() > 0);
86     assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS));
87 jsr166 1.13 }});
88    
89 jsr166 1.11 t.start();
90 jsr166 1.13 assertEquals(l.getCount(), 2);
91 dl 1.17 delay(SHORT_DELAY_MS);
92 jsr166 1.13 l.countDown();
93     assertEquals(l.getCount(), 1);
94     l.countDown();
95     assertEquals(l.getCount(), 0);
96     t.join();
97 dl 1.1 }
98 jsr166 1.8
99 dl 1.3 /**
100 dl 1.5 * await throws IE if interrupted before counted down
101 dl 1.3 */
102 jsr166 1.13 public void testAwait_InterruptedException() throws InterruptedException {
103 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
104 jsr166 1.13 Thread t = new Thread(new CheckedInterruptedRunnable() {
105     public void realRun() throws InterruptedException {
106 jsr166 1.15 assertTrue(l.getCount() > 0);
107 jsr166 1.13 l.await();
108     }});
109    
110 jsr166 1.11 t.start();
111 jsr166 1.13 assertEquals(l.getCount(), 1);
112     t.interrupt();
113     t.join();
114 dl 1.1 }
115    
116 dl 1.3 /**
117 dl 1.5 * timed await throws IE if interrupted before counted down
118 dl 1.3 */
119 jsr166 1.13 public void testTimedAwait_InterruptedException() throws InterruptedException {
120 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
121 jsr166 1.13 Thread t = new Thread(new CheckedInterruptedRunnable() {
122     public void realRun() throws InterruptedException {
123 jsr166 1.15 assertTrue(l.getCount() > 0);
124 jsr166 1.13 l.await(MEDIUM_DELAY_MS, MILLISECONDS);
125     }});
126    
127 dl 1.1 t.start();
128 dl 1.17 delay(SHORT_DELAY_MS);
129 jsr166 1.13 assertEquals(l.getCount(), 1);
130     t.interrupt();
131     t.join();
132 dl 1.1 }
133    
134 dl 1.3 /**
135 dl 1.4 * timed await times out if not counted down before timeout
136 dl 1.3 */
137 jsr166 1.13 public void testAwaitTimeout() throws InterruptedException {
138 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
139 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
140     public void realRun() throws InterruptedException {
141 jsr166 1.15 assertTrue(l.getCount() > 0);
142     assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS));
143     assertTrue(l.getCount() > 0);
144 jsr166 1.13 }});
145    
146 dl 1.1 t.start();
147 jsr166 1.13 assertEquals(l.getCount(), 1);
148     t.join();
149 dl 1.1 }
150    
151 dl 1.7 /**
152     * toString indicates current count
153     */
154     public void testToString() {
155     CountDownLatch s = new CountDownLatch(2);
156 jsr166 1.18 assertTrue(s.toString().contains("Count = 2"));
157 dl 1.7 s.countDown();
158 jsr166 1.18 assertTrue(s.toString().contains("Count = 1"));
159 dl 1.7 s.countDown();
160 jsr166 1.18 assertTrue(s.toString().contains("Count = 0"));
161 dl 1.7 }
162    
163 dl 1.1 }