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

# User Rev Content
1 dl 1.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 dl 1.2 public class CountDownLatchTest extends JSR166TestCase {
13 dl 1.1 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 dl 1.3 /**
21     *
22     */
23     public void testConstructor() {
24 dl 1.2 try {
25     new CountDownLatch(-1);
26 dl 1.3 shouldThrow();
27 dl 1.2 } catch(IllegalArgumentException success){}
28     }
29 dl 1.1
30 dl 1.3 /**
31     *
32     */
33     public void testGetCount() {
34 dl 1.1 final CountDownLatch l = new CountDownLatch(2);
35     assertEquals(2, l.getCount());
36     l.countDown();
37     assertEquals(1, l.getCount());
38     }
39    
40 dl 1.3 /**
41     *
42     */
43     public void testAwait() {
44 dl 1.1 final CountDownLatch l = new CountDownLatch(2);
45    
46 dl 1.3 Thread t = new Thread(new Runnable() {
47     public void run() {
48 dl 1.2 try {
49 dl 1.1 l.await();
50 dl 1.2 } catch(InterruptedException e){
51 dl 1.3 threadUnexpectedException();
52 dl 1.1 }
53     }
54     });
55     t.start();
56 dl 1.2 try {
57 dl 1.1 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 dl 1.2 } catch (InterruptedException e){
65 dl 1.3 unexpectedException();
66 dl 1.1 }
67     }
68    
69    
70 dl 1.3 /**
71     *
72     */
73     public void testTimedAwait() {
74 dl 1.2 final CountDownLatch l = new CountDownLatch(2);
75 dl 1.1
76 dl 1.3 Thread t = new Thread(new Runnable() {
77     public void run() {
78 dl 1.2 try {
79     threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
80     } catch(InterruptedException e){
81 dl 1.3 threadUnexpectedException();
82 dl 1.2 }
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 dl 1.3 unexpectedException();
96 dl 1.2 }
97 dl 1.1 }
98 dl 1.2
99    
100    
101 dl 1.1
102 dl 1.3 /**
103     *
104     */
105     public void testAwait_InterruptedException() {
106 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
107 dl 1.3 Thread t = new Thread(new Runnable() {
108     public void run() {
109 dl 1.2 try {
110 dl 1.1 l.await();
111 dl 1.3 threadShouldThrow();
112 dl 1.2 } catch(InterruptedException success){}
113 dl 1.1 }
114     });
115     t.start();
116 dl 1.2 try {
117 dl 1.1 assertEquals(l.getCount(), 1);
118     t.interrupt();
119     t.join();
120 dl 1.2 } catch (InterruptedException e){
121 dl 1.3 unexpectedException();
122 dl 1.1 }
123     }
124    
125 dl 1.3 /**
126     *
127     */
128     public void testTimedAwait_InterruptedException() {
129 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
130 dl 1.3 Thread t = new Thread(new Runnable() {
131     public void run() {
132 dl 1.2 try {
133 dl 1.1 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
134 dl 1.3 threadShouldThrow();
135 dl 1.2 } catch(InterruptedException success){}
136 dl 1.1 }
137     });
138     t.start();
139 dl 1.2 try {
140 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
141     assertEquals(l.getCount(), 1);
142     t.interrupt();
143     t.join();
144 dl 1.2 } catch (InterruptedException e){
145 dl 1.3 unexpectedException();
146 dl 1.1 }
147     }
148    
149 dl 1.3 /**
150     *
151     */
152     public void testAwaitTimeout() {
153 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
154 dl 1.3 Thread t = new Thread(new Runnable() {
155     public void run() {
156 dl 1.2 try {
157     threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
158     } catch(InterruptedException ie){
159 dl 1.3 threadUnexpectedException();
160 dl 1.1 }
161     }
162     });
163     t.start();
164 dl 1.2 try {
165 dl 1.1 assertEquals(l.getCount(), 1);
166     t.join();
167 dl 1.2 } catch (InterruptedException e){
168 dl 1.3 unexpectedException();
169 dl 1.1 }
170     }
171    
172     }