ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CountDownLatchTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:54 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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 TestCase{
13
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17
18
19 public static Test suite() {
20 return new TestSuite(CountDownLatchTest.class);
21 }
22
23 private static long SHORT_DELAY_MS = 100;
24 private static long MEDIUM_DELAY_MS = 1000;
25 private static long LONG_DELAY_MS = 10000;
26
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 public void testAwait1(){
35 final CountDownLatch l = new CountDownLatch(2);
36
37 Thread t = new Thread(new Runnable(){
38 public void run(){
39 try{
40 l.await();
41 }catch(InterruptedException e){
42 fail("unexpected exception");
43 }
44 }
45 });
46 t.start();
47 try{
48 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 }catch (InterruptedException e){
56 fail("unexpected exception");
57 }
58 }
59
60
61
62 public void testConstructor(){
63 try{
64 new CountDownLatch(-1);
65 fail("should throw IllegalArgumentException");
66 }catch(IllegalArgumentException success){}
67 }
68
69 public void testAwait1_InterruptedException(){
70 final CountDownLatch l = new CountDownLatch(1);
71 Thread t = new Thread(new Runnable(){
72 public void run(){
73 try{
74 l.await();
75 fail("should throw");
76 }catch(InterruptedException success){}
77 }
78 });
79 t.start();
80 try{
81 assertEquals(l.getCount(), 1);
82 t.interrupt();
83 t.join();
84 }catch (InterruptedException e){
85 fail("unexpected exception");
86 }
87 }
88
89 public void testAwait2_InterruptedException(){
90 final CountDownLatch l = new CountDownLatch(1);
91 Thread t = new Thread(new Runnable(){
92 public void run(){
93 try{
94 l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
95 fail("should throw");
96 }catch(InterruptedException success){}
97 }
98 });
99 t.start();
100 try{
101 Thread.sleep(SHORT_DELAY_MS);
102 assertEquals(l.getCount(), 1);
103 t.interrupt();
104 t.join();
105 }catch (InterruptedException e){
106 fail("unexpected exception");
107 }
108 }
109
110 public void testAwaitTimeout(){
111 final CountDownLatch l = new CountDownLatch(1);
112 Thread t = new Thread(new Runnable(){
113 public void run(){
114 try{
115 assertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
116 }catch(InterruptedException ie){
117 fail("unexpected exception");
118 }
119 }
120 });
121 t.start();
122 try{
123 assertEquals(l.getCount(), 1);
124 t.join();
125 }catch (InterruptedException e){
126 fail("unexpected exception");
127 }
128 }
129
130 }