10 |
|
import java.util.concurrent.*; |
11 |
|
|
12 |
|
public class CyclicBarrierTest extends JSR166TestCase{ |
13 |
– |
|
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(CyclicBarrierTest.class); |
18 |
|
} |
19 |
+ |
|
20 |
+ |
private volatile int countAction; |
21 |
+ |
private class MyAction implements Runnable { |
22 |
+ |
public void run() { ++countAction; } |
23 |
+ |
} |
24 |
|
|
25 |
< |
public void testConstructor1(){ |
26 |
< |
try{ |
25 |
> |
/** |
26 |
> |
* |
27 |
> |
*/ |
28 |
> |
public void testConstructor1() { |
29 |
> |
try { |
30 |
|
new CyclicBarrier(-1, (Runnable)null); |
31 |
< |
fail("should throw"); |
31 |
> |
shouldThrow(); |
32 |
|
} catch(IllegalArgumentException e){} |
33 |
|
} |
34 |
|
|
35 |
< |
public void testConstructor2(){ |
36 |
< |
try{ |
35 |
> |
/** |
36 |
> |
* |
37 |
> |
*/ |
38 |
> |
public void testConstructor2() { |
39 |
> |
try { |
40 |
|
new CyclicBarrier(-1); |
41 |
< |
fail("should throw"); |
41 |
> |
shouldThrow(); |
42 |
|
} catch(IllegalArgumentException e){} |
43 |
|
} |
44 |
|
|
45 |
< |
public void testConstructor3(){ |
45 |
> |
/** |
46 |
> |
* |
47 |
> |
*/ |
48 |
> |
public void testConstructor3() { |
49 |
|
CyclicBarrier b = new CyclicBarrier(2); |
50 |
|
assertEquals(2, b.getParties()); |
51 |
|
assertEquals(0, b.getNumberWaiting()); |
52 |
|
} |
53 |
|
|
54 |
+ |
/** |
55 |
+ |
* |
56 |
+ |
*/ |
57 |
|
public void testSingleParty() { |
58 |
|
try { |
59 |
|
CyclicBarrier b = new CyclicBarrier(1); |
64 |
|
assertEquals(0, b.getNumberWaiting()); |
65 |
|
} |
66 |
|
catch(Exception e) { |
67 |
< |
fail("unexpected exception"); |
67 |
> |
unexpectedException(); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
< |
private volatile int countAction; |
72 |
< |
private class MyAction implements Runnable { |
73 |
< |
public void run() { ++countAction; } |
60 |
< |
} |
61 |
< |
|
71 |
> |
/** |
72 |
> |
* |
73 |
> |
*/ |
74 |
|
public void testBarrierAction() { |
75 |
|
try { |
76 |
|
countAction = 0; |
83 |
|
assertEquals(countAction, 2); |
84 |
|
} |
85 |
|
catch(Exception e) { |
86 |
< |
fail("unexpected exception"); |
86 |
> |
unexpectedException(); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
< |
public void testTwoParties(){ |
91 |
> |
/** |
92 |
> |
* |
93 |
> |
*/ |
94 |
> |
public void testTwoParties() { |
95 |
|
final CyclicBarrier b = new CyclicBarrier(2); |
96 |
|
Thread t = new Thread(new Runnable() { |
97 |
< |
public void run(){ |
97 |
> |
public void run() { |
98 |
|
try { |
99 |
|
b.await(); |
100 |
|
b.await(); |
101 |
|
b.await(); |
102 |
|
b.await(); |
103 |
|
} catch(Exception e){ |
104 |
< |
threadFail("unexpected exception"); |
104 |
> |
threadUnexpectedException(); |
105 |
|
}}}); |
106 |
|
|
107 |
|
try { |
112 |
|
b.await(); |
113 |
|
t.join(); |
114 |
|
} catch(Exception e){ |
115 |
< |
fail("unexpected exception"); |
115 |
> |
unexpectedException(); |
116 |
|
} |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
public void testAwait1_Interrupted_BrokenBarrier(){ |
120 |
> |
/** |
121 |
> |
* |
122 |
> |
*/ |
123 |
> |
public void testAwait1_Interrupted_BrokenBarrier() { |
124 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
125 |
|
Thread t1 = new Thread(new Runnable() { |
126 |
< |
public void run(){ |
127 |
< |
try{ |
126 |
> |
public void run() { |
127 |
> |
try { |
128 |
|
c.await(); |
129 |
< |
threadFail("should throw"); |
129 |
> |
threadShouldThrow(); |
130 |
|
} catch(InterruptedException success){} |
131 |
|
catch(Exception b){ |
132 |
|
threadFail("should throw IE"); |
133 |
|
} |
134 |
|
} |
135 |
|
}); |
136 |
< |
Thread t2 = new Thread(new Runnable(){ |
137 |
< |
public void run(){ |
138 |
< |
try{ |
136 |
> |
Thread t2 = new Thread(new Runnable() { |
137 |
> |
public void run() { |
138 |
> |
try { |
139 |
|
c.await(); |
140 |
< |
threadFail("should throw"); |
140 |
> |
threadShouldThrow(); |
141 |
|
} catch(BrokenBarrierException success){ |
142 |
|
} catch(Exception i){ |
143 |
|
threadFail("should throw BBE"); |
152 |
|
t1.join(); |
153 |
|
t2.join(); |
154 |
|
} catch(InterruptedException e){ |
155 |
< |
fail("unexpected exception"); |
155 |
> |
unexpectedException(); |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
< |
public void testAwait2_Interrupted_BrokenBarrier(){ |
159 |
> |
/** |
160 |
> |
* |
161 |
> |
*/ |
162 |
> |
public void testAwait2_Interrupted_BrokenBarrier() { |
163 |
|
final CyclicBarrier c = new CyclicBarrier(3); |
164 |
|
Thread t1 = new Thread(new Runnable() { |
165 |
< |
public void run(){ |
166 |
< |
try{ |
165 |
> |
public void run() { |
166 |
> |
try { |
167 |
|
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
168 |
< |
threadFail("should throw"); |
168 |
> |
threadShouldThrow(); |
169 |
|
} catch(InterruptedException success){ |
170 |
|
} catch(Exception b){ |
171 |
|
threadFail("should throw IE"); |
172 |
|
} |
173 |
|
} |
174 |
|
}); |
175 |
< |
Thread t2 = new Thread(new Runnable(){ |
176 |
< |
public void run(){ |
177 |
< |
try{ |
175 |
> |
Thread t2 = new Thread(new Runnable() { |
176 |
> |
public void run() { |
177 |
> |
try { |
178 |
|
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
179 |
< |
threadFail("should throw"); |
179 |
> |
threadShouldThrow(); |
180 |
|
} catch(BrokenBarrierException success){ |
181 |
|
} catch(Exception i){ |
182 |
|
threadFail("should throw BBE"); |
191 |
|
t1.join(); |
192 |
|
t2.join(); |
193 |
|
} catch(InterruptedException e){ |
194 |
< |
fail("unexpected exception"); |
194 |
> |
unexpectedException(); |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
< |
public void testAwait3_TimeOutException(){ |
198 |
> |
/** |
199 |
> |
* |
200 |
> |
*/ |
201 |
> |
public void testAwait3_TimeOutException() { |
202 |
|
final CyclicBarrier c = new CyclicBarrier(2); |
203 |
|
Thread t = new Thread(new Runnable() { |
204 |
< |
public void run(){ |
205 |
< |
try{ |
204 |
> |
public void run() { |
205 |
> |
try { |
206 |
|
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
207 |
< |
threadFail("should throw"); |
207 |
> |
threadShouldThrow(); |
208 |
|
} catch(TimeoutException success){ |
209 |
|
} catch(Exception b){ |
210 |
|
threadFail("should throw TOE"); |
216 |
|
t.start(); |
217 |
|
t.join(); |
218 |
|
} catch(InterruptedException e){ |
219 |
< |
fail("unexpected exception"); |
219 |
> |
unexpectedException(); |
220 |
|
} |
221 |
|
} |
222 |
|
|