ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.3
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +49 -28 lines
Log Message:
Documentation scaffolding

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 *
22 */
23 public void testConstructor() {
24 try {
25 new CountDownLatch(-1);
26 shouldThrow();
27 } catch(IllegalArgumentException success){}
28 }
29
30 /**
31 *
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 *
42 */
43 public void testAwait() {
44 final CountDownLatch l = new CountDownLatch(2);
45
46 Thread t = new Thread(new Runnable() {
47 public void run() {
48 try {
49 l.await();
50 } catch(InterruptedException e){
51 threadUnexpectedException();
52 }
53 }
54 });
55 t.start();
56 try {
57 assertEquals(l.getCount(), 2);
58 Thread.sleep(SHORT_DELAY_MS);
59 l.countDown();
60 assertEquals(l.getCount(), 1);
61 l.countDown();
62 assertEquals(l.getCount(), 0);
63 t.join();
64 } catch (InterruptedException e){
65 unexpectedException();
66 }
67 }
68
69
70 /**
71 *
72 */
73 public void testTimedAwait() {
74 final CountDownLatch l = new CountDownLatch(2);
75
76 Thread t = new Thread(new Runnable() {
77 public void run() {
78 try {
79 threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
80 } catch(InterruptedException e){
81 threadUnexpectedException();
82 }
83 }
84 });
85 t.start();
86 try {
87 assertEquals(l.getCount(), 2);
88 Thread.sleep(SHORT_DELAY_MS);
89 l.countDown();
90 assertEquals(l.getCount(), 1);
91 l.countDown();
92 assertEquals(l.getCount(), 0);
93 t.join();
94 } catch (InterruptedException e){
95 unexpectedException();
96 }
97 }
98
99
100
101
102 /**
103 *
104 */
105 public void testAwait_InterruptedException() {
106 final CountDownLatch l = new CountDownLatch(1);
107 Thread t = new Thread(new Runnable() {
108 public void run() {
109 try {
110 l.await();
111 threadShouldThrow();
112 } catch(InterruptedException success){}
113 }
114 });
115 t.start();
116 try {
117 assertEquals(l.getCount(), 1);
118 t.interrupt();
119 t.join();
120 } catch (InterruptedException e){
121 unexpectedException();
122 }
123 }
124
125 /**
126 *
127 */
128 public void testTimedAwait_InterruptedException() {
129 final CountDownLatch l = new CountDownLatch(1);
130 Thread t = new Thread(new Runnable() {
131 public void run() {
132 try {
133 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
134 threadShouldThrow();
135 } catch(InterruptedException success){}
136 }
137 });
138 t.start();
139 try {
140 Thread.sleep(SHORT_DELAY_MS);
141 assertEquals(l.getCount(), 1);
142 t.interrupt();
143 t.join();
144 } catch (InterruptedException e){
145 unexpectedException();
146 }
147 }
148
149 /**
150 *
151 */
152 public void testAwaitTimeout() {
153 final CountDownLatch l = new CountDownLatch(1);
154 Thread t = new Thread(new Runnable() {
155 public void run() {
156 try {
157 threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
158 } catch(InterruptedException ie){
159 threadUnexpectedException();
160 }
161 }
162 });
163 t.start();
164 try {
165 assertEquals(l.getCount(), 1);
166 t.join();
167 } catch (InterruptedException e){
168 unexpectedException();
169 }
170 }
171
172 }