ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.2
Committed: Sun Sep 14 20:42:40 2003 UTC (21 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.1: +57 -36 lines
Log Message:
New base class JSR166TestCase

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.2 public void testConstructor(){
21     try {
22     new CountDownLatch(-1);
23     fail("should throw IllegalArgumentException");
24     } catch(IllegalArgumentException success){}
25     }
26 dl 1.1
27     public void testGetCount(){
28     final CountDownLatch l = new CountDownLatch(2);
29     assertEquals(2, l.getCount());
30     l.countDown();
31     assertEquals(1, l.getCount());
32     }
33    
34 dl 1.2 public void testAwait(){
35 dl 1.1 final CountDownLatch l = new CountDownLatch(2);
36    
37     Thread t = new Thread(new Runnable(){
38     public void run(){
39 dl 1.2 try {
40 dl 1.1 l.await();
41 dl 1.2 } catch(InterruptedException e){
42     threadFail("unexpected exception");
43 dl 1.1 }
44     }
45     });
46     t.start();
47 dl 1.2 try {
48 dl 1.1 assertEquals(l.getCount(), 2);
49     Thread.sleep(SHORT_DELAY_MS);
50     l.countDown();
51     assertEquals(l.getCount(), 1);
52     l.countDown();
53     assertEquals(l.getCount(), 0);
54     t.join();
55 dl 1.2 } catch (InterruptedException e){
56 dl 1.1 fail("unexpected exception");
57     }
58     }
59    
60    
61 dl 1.2 public void testTimedAwait(){
62     final CountDownLatch l = new CountDownLatch(2);
63 dl 1.1
64 dl 1.2 Thread t = new Thread(new Runnable(){
65     public void run(){
66     try {
67     threadAssertTrue(l.await(SMALL_DELAY_MS, TimeUnit.MILLISECONDS));
68     } catch(InterruptedException e){
69     threadFail("unexpected exception");
70     }
71     }
72     });
73     t.start();
74     try {
75     assertEquals(l.getCount(), 2);
76     Thread.sleep(SHORT_DELAY_MS);
77     l.countDown();
78     assertEquals(l.getCount(), 1);
79     l.countDown();
80     assertEquals(l.getCount(), 0);
81     t.join();
82     } catch (InterruptedException e){
83     fail("unexpected exception");
84     }
85 dl 1.1 }
86 dl 1.2
87    
88    
89 dl 1.1
90 dl 1.2 public void testAwait_InterruptedException(){
91 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
92     Thread t = new Thread(new Runnable(){
93     public void run(){
94 dl 1.2 try {
95 dl 1.1 l.await();
96 dl 1.2 threadFail("should throw");
97     } catch(InterruptedException success){}
98 dl 1.1 }
99     });
100     t.start();
101 dl 1.2 try {
102 dl 1.1 assertEquals(l.getCount(), 1);
103     t.interrupt();
104     t.join();
105 dl 1.2 } catch (InterruptedException e){
106 dl 1.1 fail("unexpected exception");
107     }
108     }
109    
110 dl 1.2 public void testTimedAwait_InterruptedException(){
111 dl 1.1 final CountDownLatch l = new CountDownLatch(1);
112     Thread t = new Thread(new Runnable(){
113     public void run(){
114 dl 1.2 try {
115 dl 1.1 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
116 dl 1.2 threadFail("should throw");
117     } catch(InterruptedException success){}
118 dl 1.1 }
119     });
120     t.start();
121 dl 1.2 try {
122 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
123     assertEquals(l.getCount(), 1);
124     t.interrupt();
125     t.join();
126 dl 1.2 } catch (InterruptedException e){
127 dl 1.1 fail("unexpected exception");
128     }
129     }
130    
131     public void testAwaitTimeout(){
132     final CountDownLatch l = new CountDownLatch(1);
133     Thread t = new Thread(new Runnable(){
134     public void run(){
135 dl 1.2 try {
136     threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
137     } catch(InterruptedException ie){
138     threadFail("unexpected exception");
139 dl 1.1 }
140     }
141     });
142     t.start();
143 dl 1.2 try {
144 dl 1.1 assertEquals(l.getCount(), 1);
145     t.join();
146 dl 1.2 } catch (InterruptedException e){
147 dl 1.1 fail("unexpected exception");
148     }
149     }
150    
151     }