ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.5
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +4 -4 lines
Log Message:
Added tests and documentation

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11
12 public class CountDownLatchTest extends JSR166TestCase {
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16 public static Test suite() {
17 return new TestSuite(CountDownLatchTest.class);
18 }
19
20 /**
21 * negative constructor argument throws IAE
22 */
23 public void testConstructor() {
24 try {
25 new CountDownLatch(-1);
26 shouldThrow();
27 } catch(IllegalArgumentException success){}
28 }
29
30 /**
31 * getCount returns initial count and decreases after countDown
32 */
33 public void testGetCount() {
34 final CountDownLatch l = new CountDownLatch(2);
35 assertEquals(2, l.getCount());
36 l.countDown();
37 assertEquals(1, l.getCount());
38 }
39
40 /**
41 * countDown decrements count when positive and has no effect when zero
42 */
43 public void testCountDown() {
44 final CountDownLatch l = new CountDownLatch(1);
45 assertEquals(1, l.getCount());
46 l.countDown();
47 assertEquals(0, l.getCount());
48 l.countDown();
49 assertEquals(0, l.getCount());
50 }
51
52 /**
53 * await returns after countDown to zero, but not before
54 */
55 public void testAwait() {
56 final CountDownLatch l = new CountDownLatch(2);
57
58 Thread t = new Thread(new Runnable() {
59 public void run() {
60 try {
61 threadAssertTrue(l.getCount() > 0);
62 l.await();
63 threadAssertTrue(l.getCount() == 0);
64 } catch(InterruptedException e){
65 threadUnexpectedException();
66 }
67 }
68 });
69 t.start();
70 try {
71 assertEquals(l.getCount(), 2);
72 Thread.sleep(SHORT_DELAY_MS);
73 l.countDown();
74 assertEquals(l.getCount(), 1);
75 l.countDown();
76 assertEquals(l.getCount(), 0);
77 t.join();
78 } catch (InterruptedException e){
79 unexpectedException();
80 }
81 }
82
83
84 /**
85 * timed await returns after countDown to zero
86 */
87 public void testTimedAwait() {
88 final CountDownLatch l = new CountDownLatch(2);
89
90 Thread t = new Thread(new Runnable() {
91 public void run() {
92 try {
93 threadAssertTrue(l.getCount() > 0);
94 threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
95 } catch(InterruptedException e){
96 threadUnexpectedException();
97 }
98 }
99 });
100 t.start();
101 try {
102 assertEquals(l.getCount(), 2);
103 Thread.sleep(SHORT_DELAY_MS);
104 l.countDown();
105 assertEquals(l.getCount(), 1);
106 l.countDown();
107 assertEquals(l.getCount(), 0);
108 t.join();
109 } catch (InterruptedException e){
110 unexpectedException();
111 }
112 }
113
114 /**
115 * await throws IE if interrupted before counted down
116 */
117 public void testAwait_InterruptedException() {
118 final CountDownLatch l = new CountDownLatch(1);
119 Thread t = new Thread(new Runnable() {
120 public void run() {
121 try {
122 threadAssertTrue(l.getCount() > 0);
123 l.await();
124 threadShouldThrow();
125 } catch(InterruptedException success){}
126 }
127 });
128 t.start();
129 try {
130 assertEquals(l.getCount(), 1);
131 t.interrupt();
132 t.join();
133 } catch (InterruptedException e){
134 unexpectedException();
135 }
136 }
137
138 /**
139 * timed await throws IE if interrupted before counted down
140 */
141 public void testTimedAwait_InterruptedException() {
142 final CountDownLatch l = new CountDownLatch(1);
143 Thread t = new Thread(new Runnable() {
144 public void run() {
145 try {
146 threadAssertTrue(l.getCount() > 0);
147 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
148 threadShouldThrow();
149 } catch(InterruptedException success){}
150 }
151 });
152 t.start();
153 try {
154 Thread.sleep(SHORT_DELAY_MS);
155 assertEquals(l.getCount(), 1);
156 t.interrupt();
157 t.join();
158 } catch (InterruptedException e){
159 unexpectedException();
160 }
161 }
162
163 /**
164 * timed await times out if not counted down before timeout
165 */
166 public void testAwaitTimeout() {
167 final CountDownLatch l = new CountDownLatch(1);
168 Thread t = new Thread(new Runnable() {
169 public void run() {
170 try {
171 threadAssertTrue(l.getCount() > 0);
172 threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
173 threadAssertTrue(l.getCount() > 0);
174 } catch(InterruptedException ie){
175 threadUnexpectedException();
176 }
177 }
178 });
179 t.start();
180 try {
181 assertEquals(l.getCount(), 1);
182 t.join();
183 } catch (InterruptedException e){
184 unexpectedException();
185 }
186 }
187
188 }