9 |
|
import java.util.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
< |
public class CountDownLatchTest extends TestCase{ |
13 |
< |
|
12 |
> |
public class CountDownLatchTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
– |
|
18 |
– |
|
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(CountDownLatchTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
private static long SHORT_DELAY_MS = 100; |
21 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
22 |
< |
private static long LONG_DELAY_MS = 10000; |
20 |
> |
/** |
21 |
> |
* |
22 |
> |
*/ |
23 |
> |
public void testConstructor() { |
24 |
> |
try { |
25 |
> |
new CountDownLatch(-1); |
26 |
> |
shouldThrow(); |
27 |
> |
} catch(IllegalArgumentException success){} |
28 |
> |
} |
29 |
|
|
30 |
< |
public void testGetCount(){ |
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 |
< |
public void testAwait1(){ |
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{ |
46 |
> |
Thread t = new Thread(new Runnable() { |
47 |
> |
public void run() { |
48 |
> |
try { |
49 |
|
l.await(); |
50 |
< |
}catch(InterruptedException e){ |
51 |
< |
fail("unexpected exception"); |
50 |
> |
} catch(InterruptedException e){ |
51 |
> |
threadUnexpectedException(); |
52 |
|
} |
53 |
|
} |
54 |
|
}); |
55 |
|
t.start(); |
56 |
< |
try{ |
56 |
> |
try { |
57 |
|
assertEquals(l.getCount(), 2); |
58 |
|
Thread.sleep(SHORT_DELAY_MS); |
59 |
|
l.countDown(); |
61 |
|
l.countDown(); |
62 |
|
assertEquals(l.getCount(), 0); |
63 |
|
t.join(); |
64 |
< |
}catch (InterruptedException e){ |
65 |
< |
fail("unexpected exception"); |
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 |
< |
public void testConstructor(){ |
77 |
< |
try{ |
78 |
< |
new CountDownLatch(-1); |
79 |
< |
fail("should throw IllegalArgumentException"); |
80 |
< |
}catch(IllegalArgumentException success){} |
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 |
< |
public void testAwait1_InterruptedException(){ |
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{ |
107 |
> |
Thread t = new Thread(new Runnable() { |
108 |
> |
public void run() { |
109 |
> |
try { |
110 |
|
l.await(); |
111 |
< |
fail("should throw"); |
112 |
< |
}catch(InterruptedException success){} |
111 |
> |
threadShouldThrow(); |
112 |
> |
} catch(InterruptedException success){} |
113 |
|
} |
114 |
|
}); |
115 |
|
t.start(); |
116 |
< |
try{ |
116 |
> |
try { |
117 |
|
assertEquals(l.getCount(), 1); |
118 |
|
t.interrupt(); |
119 |
|
t.join(); |
120 |
< |
}catch (InterruptedException e){ |
121 |
< |
fail("unexpected exception"); |
120 |
> |
} catch (InterruptedException e){ |
121 |
> |
unexpectedException(); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
< |
public void testAwait2_InterruptedException(){ |
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{ |
130 |
> |
Thread t = new Thread(new Runnable() { |
131 |
> |
public void run() { |
132 |
> |
try { |
133 |
|
l.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
134 |
< |
fail("should throw"); |
135 |
< |
}catch(InterruptedException success){} |
134 |
> |
threadShouldThrow(); |
135 |
> |
} catch(InterruptedException success){} |
136 |
|
} |
137 |
|
}); |
138 |
|
t.start(); |
139 |
< |
try{ |
139 |
> |
try { |
140 |
|
Thread.sleep(SHORT_DELAY_MS); |
141 |
|
assertEquals(l.getCount(), 1); |
142 |
|
t.interrupt(); |
143 |
|
t.join(); |
144 |
< |
}catch (InterruptedException e){ |
145 |
< |
fail("unexpected exception"); |
144 |
> |
} catch (InterruptedException e){ |
145 |
> |
unexpectedException(); |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
< |
public void testAwaitTimeout(){ |
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 |
< |
assertFalse(l.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
158 |
< |
}catch(InterruptedException ie){ |
159 |
< |
fail("unexpected exception"); |
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{ |
164 |
> |
try { |
165 |
|
assertEquals(l.getCount(), 1); |
166 |
|
t.join(); |
167 |
< |
}catch (InterruptedException e){ |
168 |
< |
fail("unexpected exception"); |
167 |
> |
} catch (InterruptedException e){ |
168 |
> |
unexpectedException(); |
169 |
|
} |
170 |
|
} |
171 |
|
|