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 |
public void testConstructor(){ |
21 |
try { |
22 |
new CountDownLatch(-1); |
23 |
fail("should throw IllegalArgumentException"); |
24 |
} catch(IllegalArgumentException success){} |
25 |
} |
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 testAwait(){ |
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 |
threadFail("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 |
public void testTimedAwait(){ |
62 |
final CountDownLatch l = new CountDownLatch(2); |
63 |
|
64 |
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 |
} |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
public void testAwait_InterruptedException(){ |
91 |
final CountDownLatch l = new CountDownLatch(1); |
92 |
Thread t = new Thread(new Runnable(){ |
93 |
public void run(){ |
94 |
try { |
95 |
l.await(); |
96 |
threadFail("should throw"); |
97 |
} catch(InterruptedException success){} |
98 |
} |
99 |
}); |
100 |
t.start(); |
101 |
try { |
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 testTimedAwait_InterruptedException(){ |
111 |
final CountDownLatch l = new CountDownLatch(1); |
112 |
Thread t = new Thread(new Runnable(){ |
113 |
public void run(){ |
114 |
try { |
115 |
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
116 |
threadFail("should throw"); |
117 |
} catch(InterruptedException success){} |
118 |
} |
119 |
}); |
120 |
t.start(); |
121 |
try { |
122 |
Thread.sleep(SHORT_DELAY_MS); |
123 |
assertEquals(l.getCount(), 1); |
124 |
t.interrupt(); |
125 |
t.join(); |
126 |
} catch (InterruptedException e){ |
127 |
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 |
try { |
136 |
threadAssertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
137 |
} catch(InterruptedException ie){ |
138 |
threadFail("unexpected exception"); |
139 |
} |
140 |
} |
141 |
}); |
142 |
t.start(); |
143 |
try { |
144 |
assertEquals(l.getCount(), 1); |
145 |
t.join(); |
146 |
} catch (InterruptedException e){ |
147 |
fail("unexpected exception"); |
148 |
} |
149 |
} |
150 |
|
151 |
} |