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 |
|
|
public class CyclicBarrierTest 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(CyclicBarrierTest.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 testConstructor1(){ |
28 |
|
|
try{ |
29 |
|
|
new CyclicBarrier(-1, (Runnable)null); |
30 |
|
|
fail("should throw"); |
31 |
|
|
} catch(IllegalArgumentException e){} |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
public void testConstructor2(){ |
35 |
|
|
try{ |
36 |
|
|
new CyclicBarrier(-1); |
37 |
|
|
fail("should throw"); |
38 |
|
|
} catch(IllegalArgumentException e){} |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
public void testConstructor3(){ |
42 |
|
|
CyclicBarrier b = new CyclicBarrier(2); |
43 |
|
|
assertEquals(2, b.getParties()); |
44 |
|
|
assertEquals(0, b.getNumberWaiting()); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
public void testSingleParty() { |
48 |
|
|
try { |
49 |
|
|
CyclicBarrier b = new CyclicBarrier(1); |
50 |
|
|
assertEquals(1, b.getParties()); |
51 |
|
|
assertEquals(0, b.getNumberWaiting()); |
52 |
|
|
b.await(); |
53 |
|
|
b.await(); |
54 |
|
|
assertEquals(0, b.getNumberWaiting()); |
55 |
|
|
} |
56 |
|
|
catch(Exception e) { |
57 |
|
|
fail("unexpected exception"); |
58 |
|
|
} |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
private volatile int countAction; |
62 |
|
|
private class MyAction implements Runnable { |
63 |
|
|
public void run() { ++countAction; } |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
public void testBarrierAction() { |
67 |
|
|
try { |
68 |
|
|
countAction = 0; |
69 |
|
|
CyclicBarrier b = new CyclicBarrier(1, new MyAction()); |
70 |
|
|
assertEquals(1, b.getParties()); |
71 |
|
|
assertEquals(0, b.getNumberWaiting()); |
72 |
|
|
b.await(); |
73 |
|
|
b.await(); |
74 |
|
|
assertEquals(0, b.getNumberWaiting()); |
75 |
|
|
assertEquals(countAction, 2); |
76 |
|
|
} |
77 |
|
|
catch(Exception e) { |
78 |
|
|
fail("unexpected exception"); |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
public void testTwoParties(){ |
84 |
|
|
final CyclicBarrier b = new CyclicBarrier(2); |
85 |
|
|
Thread t = new Thread(new Runnable() { |
86 |
|
|
public void run(){ |
87 |
|
|
try { |
88 |
|
|
b.await(); |
89 |
|
|
b.await(); |
90 |
|
|
b.await(); |
91 |
|
|
b.await(); |
92 |
|
|
} catch(Exception e){ |
93 |
|
|
fail("unexpected exception"); |
94 |
|
|
}}}); |
95 |
|
|
|
96 |
|
|
try { |
97 |
|
|
t.start(); |
98 |
|
|
b.await(); |
99 |
|
|
b.await(); |
100 |
|
|
b.await(); |
101 |
|
|
b.await(); |
102 |
|
|
t.join(); |
103 |
|
|
} catch(Exception e){ |
104 |
|
|
fail("unexpected exception"); |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
public void testAwait1_Interrupted_BrokenBarrier(){ |
110 |
|
|
final CyclicBarrier c = new CyclicBarrier(3); |
111 |
|
|
Thread t1 = new Thread(new Runnable() { |
112 |
|
|
public void run(){ |
113 |
|
|
try{ |
114 |
|
|
c.await(); |
115 |
|
|
fail("should throw"); |
116 |
|
|
} catch(InterruptedException success){} |
117 |
|
|
catch(Exception b){ |
118 |
|
|
fail("should throw IE"); |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
}); |
122 |
|
|
Thread t2 = new Thread(new Runnable(){ |
123 |
|
|
public void run(){ |
124 |
|
|
try{ |
125 |
|
|
c.await(); |
126 |
|
|
fail("should throw"); |
127 |
|
|
} catch(BrokenBarrierException success){ |
128 |
|
|
} catch(Exception i){ |
129 |
|
|
fail("should throw BBE"); |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
}); |
133 |
|
|
try { |
134 |
|
|
t1.start(); |
135 |
|
|
t2.start(); |
136 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
137 |
|
|
t1.interrupt(); |
138 |
|
|
t1.join(); |
139 |
|
|
t2.join(); |
140 |
|
|
} catch(InterruptedException e){ |
141 |
|
|
fail("unexpected exception"); |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
public void testAwait2_Interrupted_BrokenBarrier(){ |
146 |
|
|
final CyclicBarrier c = new CyclicBarrier(3); |
147 |
|
|
Thread t1 = new Thread(new Runnable() { |
148 |
|
|
public void run(){ |
149 |
|
|
try{ |
150 |
|
|
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
151 |
|
|
fail("should throw"); |
152 |
|
|
} catch(InterruptedException success){ |
153 |
|
|
} catch(Exception b){ |
154 |
|
|
fail("should throw IE"); |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
}); |
158 |
|
|
Thread t2 = new Thread(new Runnable(){ |
159 |
|
|
public void run(){ |
160 |
|
|
try{ |
161 |
|
|
c.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
162 |
|
|
fail("should throw"); |
163 |
|
|
} catch(BrokenBarrierException success){ |
164 |
|
|
} catch(Exception i){ |
165 |
|
|
fail("should throw BBE"); |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
}); |
169 |
|
|
try { |
170 |
|
|
t1.start(); |
171 |
|
|
t2.start(); |
172 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
173 |
|
|
t1.interrupt(); |
174 |
|
|
t1.join(); |
175 |
|
|
t2.join(); |
176 |
|
|
} catch(InterruptedException e){ |
177 |
|
|
fail("unexpected exception"); |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
public void testAwait3_TimeOutException(){ |
182 |
|
|
final CyclicBarrier c = new CyclicBarrier(2); |
183 |
|
|
Thread t = new Thread(new Runnable() { |
184 |
|
|
public void run(){ |
185 |
|
|
try{ |
186 |
|
|
c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
187 |
|
|
fail("should throw"); |
188 |
|
|
} catch(TimeoutException success){ |
189 |
|
|
} catch(Exception b){ |
190 |
|
|
fail("should throw TOE"); |
191 |
|
|
|
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
}); |
195 |
|
|
try { |
196 |
|
|
t.start(); |
197 |
|
|
t.join(); |
198 |
|
|
} catch(InterruptedException e){ |
199 |
|
|
fail("unexpected exception"); |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
} |