1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
* Other contributors include John Vint |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.32 |
import junit.framework.*; |
9 |
dl |
1.1 |
import java.util.ArrayList; |
10 |
|
|
import java.util.List; |
11 |
jsr166 |
1.32 |
import java.util.concurrent.Phaser; |
12 |
|
|
import java.util.concurrent.CountDownLatch; |
13 |
jsr166 |
1.16 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
jsr166 |
1.19 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
jsr166 |
1.32 |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
17 |
dl |
1.1 |
|
18 |
|
|
public class PhaserTest extends JSR166TestCase { |
19 |
|
|
|
20 |
|
|
public static void main(String[] args) { |
21 |
|
|
junit.textui.TestRunner.run(suite()); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public static Test suite() { |
25 |
|
|
return new TestSuite(PhaserTest.class); |
26 |
|
|
} |
27 |
|
|
|
28 |
jsr166 |
1.21 |
private static final int maxParties = 65535; |
29 |
jsr166 |
1.22 |
|
30 |
jsr166 |
1.28 |
/** Checks state of unterminated phaser. */ |
31 |
jsr166 |
1.19 |
protected void assertState(Phaser phaser, |
32 |
|
|
int phase, int parties, int unarrived) { |
33 |
|
|
assertEquals(phase, phaser.getPhase()); |
34 |
|
|
assertEquals(parties, phaser.getRegisteredParties()); |
35 |
|
|
assertEquals(unarrived, phaser.getUnarrivedParties()); |
36 |
|
|
assertEquals(parties - unarrived, phaser.getArrivedParties()); |
37 |
jsr166 |
1.28 |
assertFalse(phaser.isTerminated()); |
38 |
jsr166 |
1.19 |
} |
39 |
|
|
|
40 |
|
|
/** Checks state of terminated phaser. */ |
41 |
jsr166 |
1.28 |
protected void assertTerminated(Phaser phaser, int maxPhase, int parties) { |
42 |
jsr166 |
1.19 |
assertTrue(phaser.isTerminated()); |
43 |
jsr166 |
1.25 |
int expectedPhase = maxPhase + Integer.MIN_VALUE; |
44 |
dl |
1.26 |
assertEquals(expectedPhase, phaser.getPhase()); |
45 |
|
|
assertEquals(parties, phaser.getRegisteredParties()); |
46 |
jsr166 |
1.25 |
assertEquals(expectedPhase, phaser.register()); |
47 |
|
|
assertEquals(expectedPhase, phaser.arrive()); |
48 |
|
|
assertEquals(expectedPhase, phaser.arriveAndDeregister()); |
49 |
jsr166 |
1.19 |
} |
50 |
|
|
|
51 |
jsr166 |
1.25 |
protected void assertTerminated(Phaser phaser, int maxPhase) { |
52 |
jsr166 |
1.28 |
assertTerminated(phaser, maxPhase, 0); |
53 |
jsr166 |
1.19 |
} |
54 |
|
|
|
55 |
dl |
1.1 |
/** |
56 |
|
|
* Empty constructor builds a new Phaser with no parent, no registered |
57 |
|
|
* parties and initial phase number of 0 |
58 |
|
|
*/ |
59 |
jsr166 |
1.21 |
public void testConstructorDefaultValues() { |
60 |
dl |
1.1 |
Phaser phaser = new Phaser(); |
61 |
|
|
assertNull(phaser.getParent()); |
62 |
jsr166 |
1.21 |
assertEquals(0, phaser.getRegisteredParties()); |
63 |
dl |
1.1 |
assertEquals(0, phaser.getArrivedParties()); |
64 |
jsr166 |
1.21 |
assertEquals(0, phaser.getUnarrivedParties()); |
65 |
dl |
1.1 |
assertEquals(0, phaser.getPhase()); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/** |
69 |
jsr166 |
1.21 |
* Constructing with a negative number of parties throws |
70 |
|
|
* IllegalArgumentException |
71 |
dl |
1.1 |
*/ |
72 |
jsr166 |
1.21 |
public void testConstructorNegativeParties() { |
73 |
dl |
1.1 |
try { |
74 |
|
|
new Phaser(-1); |
75 |
jsr166 |
1.6 |
shouldThrow(); |
76 |
jsr166 |
1.8 |
} catch (IllegalArgumentException success) {} |
77 |
dl |
1.1 |
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
jsr166 |
1.21 |
* Constructing with a negative number of parties throws |
81 |
|
|
* IllegalArgumentException |
82 |
dl |
1.1 |
*/ |
83 |
jsr166 |
1.21 |
public void testConstructorNegativeParties2() { |
84 |
|
|
try { |
85 |
|
|
new Phaser(new Phaser(), -1); |
86 |
|
|
shouldThrow(); |
87 |
|
|
} catch (IllegalArgumentException success) {} |
88 |
dl |
1.1 |
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
jsr166 |
1.21 |
* Constructing with a number of parties > 65535 throws |
92 |
|
|
* IllegalArgumentException |
93 |
dl |
1.1 |
*/ |
94 |
jsr166 |
1.21 |
public void testConstructorPartiesExceedsLimit() { |
95 |
|
|
new Phaser(maxParties); |
96 |
dl |
1.1 |
try { |
97 |
jsr166 |
1.21 |
new Phaser(maxParties + 1); |
98 |
|
|
shouldThrow(); |
99 |
|
|
} catch (IllegalArgumentException success) {} |
100 |
|
|
|
101 |
|
|
new Phaser(new Phaser(), maxParties); |
102 |
|
|
try { |
103 |
|
|
new Phaser(new Phaser(), maxParties + 1); |
104 |
dl |
1.1 |
shouldThrow(); |
105 |
jsr166 |
1.8 |
} catch (IllegalArgumentException success) {} |
106 |
dl |
1.1 |
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
jsr166 |
1.21 |
* The parent provided to the constructor should be returned from |
110 |
|
|
* a later call to getParent |
111 |
|
|
*/ |
112 |
|
|
public void testConstructor3() { |
113 |
|
|
Phaser parent = new Phaser(); |
114 |
|
|
assertSame(parent, new Phaser(parent).getParent()); |
115 |
|
|
assertNull(new Phaser(null).getParent()); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/** |
119 |
dl |
1.1 |
* The parent being input into the parameter should equal the original |
120 |
|
|
* parent when being returned |
121 |
|
|
*/ |
122 |
|
|
public void testConstructor5() { |
123 |
|
|
Phaser parent = new Phaser(); |
124 |
jsr166 |
1.21 |
assertSame(parent, new Phaser(parent, 0).getParent()); |
125 |
|
|
assertNull(new Phaser(null, 0).getParent()); |
126 |
dl |
1.1 |
} |
127 |
|
|
|
128 |
|
|
/** |
129 |
jsr166 |
1.21 |
* register() will increment the number of unarrived parties by |
130 |
|
|
* one and not affect its arrived parties |
131 |
dl |
1.1 |
*/ |
132 |
|
|
public void testRegister1() { |
133 |
|
|
Phaser phaser = new Phaser(); |
134 |
jsr166 |
1.19 |
assertState(phaser, 0, 0, 0); |
135 |
|
|
assertEquals(0, phaser.register()); |
136 |
|
|
assertState(phaser, 0, 1, 1); |
137 |
dl |
1.1 |
} |
138 |
|
|
|
139 |
|
|
/** |
140 |
jsr166 |
1.4 |
* Registering more than 65536 parties causes IllegalStateException |
141 |
dl |
1.1 |
*/ |
142 |
|
|
public void testRegister2() { |
143 |
|
|
Phaser phaser = new Phaser(0); |
144 |
jsr166 |
1.19 |
assertState(phaser, 0, 0, 0); |
145 |
|
|
assertEquals(0, phaser.bulkRegister(maxParties - 10)); |
146 |
|
|
assertState(phaser, 0, maxParties - 10, maxParties - 10); |
147 |
|
|
for (int i = 0; i < 10; i++) { |
148 |
|
|
assertState(phaser, 0, maxParties - 10 + i, maxParties - 10 + i); |
149 |
|
|
assertEquals(0, phaser.register()); |
150 |
dl |
1.1 |
} |
151 |
jsr166 |
1.19 |
assertState(phaser, 0, maxParties, maxParties); |
152 |
dl |
1.1 |
try { |
153 |
|
|
phaser.register(); |
154 |
|
|
shouldThrow(); |
155 |
jsr166 |
1.8 |
} catch (IllegalStateException success) {} |
156 |
jsr166 |
1.21 |
|
157 |
|
|
try { |
158 |
|
|
phaser.bulkRegister(Integer.MAX_VALUE); |
159 |
|
|
shouldThrow(); |
160 |
|
|
} catch (IllegalStateException success) {} |
161 |
jsr166 |
1.30 |
|
162 |
|
|
assertEquals(0, phaser.bulkRegister(0)); |
163 |
|
|
assertState(phaser, 0, maxParties, maxParties); |
164 |
dl |
1.1 |
} |
165 |
|
|
|
166 |
|
|
/** |
167 |
|
|
* register() correctly returns the current barrier phase number when |
168 |
|
|
* invoked |
169 |
|
|
*/ |
170 |
|
|
public void testRegister3() { |
171 |
|
|
Phaser phaser = new Phaser(); |
172 |
|
|
assertEquals(0, phaser.register()); |
173 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
174 |
dl |
1.1 |
assertEquals(1, phaser.register()); |
175 |
jsr166 |
1.19 |
assertState(phaser, 1, 2, 2); |
176 |
dl |
1.1 |
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* register causes the next arrive to not increment the phase rather retain |
180 |
|
|
* the phase number |
181 |
|
|
*/ |
182 |
|
|
public void testRegister4() { |
183 |
|
|
Phaser phaser = new Phaser(1); |
184 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
185 |
|
|
assertEquals(1, phaser.register()); |
186 |
|
|
assertEquals(1, phaser.arrive()); |
187 |
|
|
assertState(phaser, 1, 2, 1); |
188 |
dl |
1.1 |
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
|
|
* Invoking bulkRegister with a negative parameter throws an |
192 |
jsr166 |
1.4 |
* IllegalArgumentException |
193 |
dl |
1.1 |
*/ |
194 |
|
|
public void testBulkRegister1() { |
195 |
|
|
try { |
196 |
|
|
new Phaser().bulkRegister(-1); |
197 |
|
|
shouldThrow(); |
198 |
jsr166 |
1.8 |
} catch (IllegalArgumentException success) {} |
199 |
dl |
1.1 |
} |
200 |
|
|
|
201 |
|
|
/** |
202 |
|
|
* bulkRegister should correctly record the number of unarrived parties with |
203 |
|
|
* the number of parties being registered |
204 |
|
|
*/ |
205 |
|
|
public void testBulkRegister2() { |
206 |
|
|
Phaser phaser = new Phaser(); |
207 |
jsr166 |
1.30 |
assertEquals(0, phaser.bulkRegister(0)); |
208 |
|
|
assertState(phaser, 0, 0, 0); |
209 |
jsr166 |
1.19 |
assertEquals(0, phaser.bulkRegister(20)); |
210 |
|
|
assertState(phaser, 0, 20, 20); |
211 |
dl |
1.1 |
} |
212 |
|
|
|
213 |
|
|
/** |
214 |
jsr166 |
1.4 |
* Registering with a number of parties greater than or equal to 1<<16 |
215 |
|
|
* throws IllegalStateException. |
216 |
dl |
1.1 |
*/ |
217 |
|
|
public void testBulkRegister3() { |
218 |
jsr166 |
1.19 |
assertEquals(0, new Phaser().bulkRegister((1 << 16) - 1)); |
219 |
|
|
|
220 |
dl |
1.1 |
try { |
221 |
|
|
new Phaser().bulkRegister(1 << 16); |
222 |
|
|
shouldThrow(); |
223 |
jsr166 |
1.8 |
} catch (IllegalStateException success) {} |
224 |
jsr166 |
1.19 |
|
225 |
|
|
try { |
226 |
|
|
new Phaser(2).bulkRegister((1 << 16) - 2); |
227 |
|
|
shouldThrow(); |
228 |
|
|
} catch (IllegalStateException success) {} |
229 |
dl |
1.1 |
} |
230 |
|
|
|
231 |
|
|
/** |
232 |
|
|
* the phase number increments correctly when tripping the barrier |
233 |
|
|
*/ |
234 |
|
|
public void testPhaseIncrement1() { |
235 |
|
|
for (int size = 1; size < nine; size++) { |
236 |
|
|
final Phaser phaser = new Phaser(size); |
237 |
|
|
for (int index = 0; index <= (1 << size); index++) { |
238 |
|
|
int phase = phaser.arrive(); |
239 |
|
|
assertTrue(index % size == 0 ? (index / size) == phase : index - (phase * size) > 0); |
240 |
|
|
} |
241 |
|
|
} |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
/** |
245 |
jsr166 |
1.19 |
* arrive() on a registered phaser increments phase. |
246 |
dl |
1.1 |
*/ |
247 |
|
|
public void testArrive1() { |
248 |
|
|
Phaser phaser = new Phaser(1); |
249 |
jsr166 |
1.19 |
assertState(phaser, 0, 1, 1); |
250 |
|
|
assertEquals(0, phaser.arrive()); |
251 |
|
|
assertState(phaser, 1, 1, 1); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
/** |
255 |
|
|
* arriveAndDeregister does not wait for others to arrive at barrier |
256 |
|
|
*/ |
257 |
|
|
public void testArriveAndDeregister() throws InterruptedException { |
258 |
|
|
final Phaser phaser = new Phaser(1); |
259 |
|
|
for (int i = 0; i < 10; i++) { |
260 |
|
|
assertState(phaser, 0, 1, 1); |
261 |
|
|
assertEquals(0, phaser.register()); |
262 |
|
|
assertState(phaser, 0, 2, 2); |
263 |
|
|
assertEquals(0, phaser.arriveAndDeregister()); |
264 |
|
|
assertState(phaser, 0, 1, 1); |
265 |
|
|
} |
266 |
|
|
assertEquals(0, phaser.arriveAndDeregister()); |
267 |
jsr166 |
1.25 |
assertTerminated(phaser, 1); |
268 |
dl |
1.1 |
} |
269 |
|
|
|
270 |
|
|
/** |
271 |
jsr166 |
1.7 |
* arriveAndDeregister does not wait for others to arrive at barrier |
272 |
dl |
1.1 |
*/ |
273 |
jsr166 |
1.7 |
public void testArrive2() throws InterruptedException { |
274 |
jsr166 |
1.19 |
final Phaser phaser = new Phaser(); |
275 |
|
|
assertEquals(0, phaser.register()); |
276 |
jsr166 |
1.7 |
List<Thread> threads = new ArrayList<Thread>(); |
277 |
jsr166 |
1.19 |
for (int i = 0; i < 10; i++) { |
278 |
|
|
assertEquals(0, phaser.register()); |
279 |
jsr166 |
1.7 |
threads.add(newStartedThread(new CheckedRunnable() { |
280 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
281 |
jsr166 |
1.19 |
assertEquals(0, phaser.arriveAndDeregister()); |
282 |
jsr166 |
1.7 |
}})); |
283 |
jsr166 |
1.19 |
} |
284 |
dl |
1.1 |
|
285 |
jsr166 |
1.7 |
for (Thread thread : threads) |
286 |
jsr166 |
1.19 |
awaitTermination(thread, LONG_DELAY_MS); |
287 |
|
|
assertState(phaser, 0, 1, 1); |
288 |
|
|
assertEquals(0, phaser.arrive()); |
289 |
|
|
assertState(phaser, 1, 1, 1); |
290 |
dl |
1.1 |
} |
291 |
|
|
|
292 |
|
|
/** |
293 |
jsr166 |
1.3 |
* arrive() returns a negative number if the Phaser is terminated |
294 |
dl |
1.1 |
*/ |
295 |
|
|
public void testArrive3() { |
296 |
|
|
Phaser phaser = new Phaser(1); |
297 |
|
|
phaser.forceTermination(); |
298 |
jsr166 |
1.28 |
assertTerminated(phaser, 0, 1); |
299 |
dl |
1.24 |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
300 |
dl |
1.1 |
assertTrue(phaser.arrive() < 0); |
301 |
jsr166 |
1.19 |
assertTrue(phaser.register() < 0); |
302 |
|
|
assertTrue(phaser.arriveAndDeregister() < 0); |
303 |
|
|
assertTrue(phaser.awaitAdvance(1) < 0); |
304 |
|
|
assertTrue(phaser.getPhase() < 0); |
305 |
dl |
1.1 |
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
|
|
* arriveAndDeregister() throws IllegalStateException if number of |
309 |
jsr166 |
1.3 |
* registered or unarrived parties would become negative |
310 |
dl |
1.1 |
*/ |
311 |
|
|
public void testArriveAndDeregister1() { |
312 |
|
|
try { |
313 |
|
|
Phaser phaser = new Phaser(); |
314 |
|
|
phaser.arriveAndDeregister(); |
315 |
|
|
shouldThrow(); |
316 |
jsr166 |
1.8 |
} catch (IllegalStateException success) {} |
317 |
dl |
1.1 |
} |
318 |
|
|
|
319 |
|
|
/** |
320 |
jsr166 |
1.19 |
* arriveAndDeregister reduces the number of arrived parties |
321 |
dl |
1.1 |
*/ |
322 |
jsr166 |
1.19 |
public void testArriveAndDeregister2() { |
323 |
dl |
1.1 |
final Phaser phaser = new Phaser(1); |
324 |
jsr166 |
1.19 |
assertEquals(0, phaser.register()); |
325 |
|
|
assertEquals(0, phaser.arrive()); |
326 |
|
|
assertState(phaser, 0, 2, 1); |
327 |
|
|
assertEquals(0, phaser.arriveAndDeregister()); |
328 |
|
|
assertState(phaser, 1, 1, 1); |
329 |
dl |
1.1 |
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
jsr166 |
1.19 |
* arriveAndDeregister arrives at the barrier on a phaser with a parent and |
333 |
dl |
1.1 |
* when a deregistration occurs and causes the phaser to have zero parties |
334 |
|
|
* its parent will be deregistered as well |
335 |
|
|
*/ |
336 |
jsr166 |
1.19 |
public void testArriveAndDeregister3() { |
337 |
dl |
1.1 |
Phaser parent = new Phaser(); |
338 |
jsr166 |
1.19 |
Phaser child = new Phaser(parent); |
339 |
|
|
assertState(child, 0, 0, 0); |
340 |
dl |
1.23 |
assertState(parent, 0, 0, 0); |
341 |
jsr166 |
1.19 |
assertEquals(0, child.register()); |
342 |
|
|
assertState(child, 0, 1, 1); |
343 |
|
|
assertState(parent, 0, 1, 1); |
344 |
|
|
assertEquals(0, child.arriveAndDeregister()); |
345 |
jsr166 |
1.25 |
assertTerminated(child, 1); |
346 |
|
|
assertTerminated(parent, 1); |
347 |
dl |
1.1 |
} |
348 |
|
|
|
349 |
|
|
/** |
350 |
|
|
* arriveAndDeregister deregisters one party from its parent when |
351 |
jsr166 |
1.19 |
* the number of parties of child is zero after deregistration |
352 |
dl |
1.1 |
*/ |
353 |
jsr166 |
1.19 |
public void testArriveAndDeregister4() { |
354 |
dl |
1.1 |
Phaser parent = new Phaser(); |
355 |
jsr166 |
1.19 |
Phaser child = new Phaser(parent); |
356 |
|
|
assertEquals(0, parent.register()); |
357 |
|
|
assertEquals(0, child.register()); |
358 |
|
|
assertState(child, 0, 1, 1); |
359 |
|
|
assertState(parent, 0, 2, 2); |
360 |
|
|
assertEquals(0, child.arriveAndDeregister()); |
361 |
|
|
assertState(child, 0, 0, 0); |
362 |
|
|
assertState(parent, 0, 1, 1); |
363 |
dl |
1.1 |
} |
364 |
|
|
|
365 |
|
|
/** |
366 |
|
|
* arriveAndDeregister deregisters one party from its parent when |
367 |
|
|
* the number of parties of root is nonzero after deregistration. |
368 |
|
|
*/ |
369 |
|
|
public void testArriveAndDeregister5() { |
370 |
jsr166 |
1.19 |
Phaser root = new Phaser(); |
371 |
|
|
Phaser parent = new Phaser(root); |
372 |
dl |
1.1 |
Phaser child = new Phaser(parent); |
373 |
dl |
1.23 |
assertState(root, 0, 0, 0); |
374 |
|
|
assertState(parent, 0, 0, 0); |
375 |
jsr166 |
1.19 |
assertState(child, 0, 0, 0); |
376 |
|
|
assertEquals(0, child.register()); |
377 |
|
|
assertState(root, 0, 1, 1); |
378 |
|
|
assertState(parent, 0, 1, 1); |
379 |
|
|
assertState(child, 0, 1, 1); |
380 |
|
|
assertEquals(0, child.arriveAndDeregister()); |
381 |
jsr166 |
1.25 |
assertTerminated(child, 1); |
382 |
|
|
assertTerminated(parent, 1); |
383 |
|
|
assertTerminated(root, 1); |
384 |
dl |
1.1 |
} |
385 |
|
|
|
386 |
|
|
/** |
387 |
|
|
* arriveAndDeregister returns the phase in which it leaves the |
388 |
|
|
* phaser in after deregistration |
389 |
|
|
*/ |
390 |
jsr166 |
1.7 |
public void testArriveAndDeregister6() throws InterruptedException { |
391 |
dl |
1.1 |
final Phaser phaser = new Phaser(2); |
392 |
jsr166 |
1.7 |
Thread t = newStartedThread(new CheckedRunnable() { |
393 |
jsr166 |
1.9 |
public void realRun() { |
394 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
395 |
jsr166 |
1.7 |
}}); |
396 |
jsr166 |
1.19 |
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
397 |
|
|
assertState(phaser, 1, 2, 2); |
398 |
|
|
assertEquals(1, phaser.arriveAndDeregister()); |
399 |
|
|
assertState(phaser, 1, 1, 1); |
400 |
|
|
assertEquals(1, phaser.arriveAndDeregister()); |
401 |
jsr166 |
1.25 |
assertTerminated(phaser, 2); |
402 |
jsr166 |
1.19 |
awaitTermination(t, SHORT_DELAY_MS); |
403 |
dl |
1.1 |
} |
404 |
|
|
|
405 |
|
|
/** |
406 |
|
|
* awaitAdvance succeeds upon advance |
407 |
|
|
*/ |
408 |
|
|
public void testAwaitAdvance1() { |
409 |
|
|
final Phaser phaser = new Phaser(1); |
410 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
411 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
412 |
dl |
1.1 |
} |
413 |
|
|
|
414 |
|
|
/** |
415 |
|
|
* awaitAdvance with a negative parameter will return without affecting the |
416 |
|
|
* phaser |
417 |
|
|
*/ |
418 |
|
|
public void testAwaitAdvance2() { |
419 |
jsr166 |
1.6 |
Phaser phaser = new Phaser(); |
420 |
jsr166 |
1.19 |
assertTrue(phaser.awaitAdvance(-1) < 0); |
421 |
|
|
assertState(phaser, 0, 0, 0); |
422 |
dl |
1.1 |
} |
423 |
|
|
|
424 |
|
|
/** |
425 |
jsr166 |
1.20 |
* awaitAdvance continues waiting if interrupted before waiting |
426 |
dl |
1.1 |
*/ |
427 |
jsr166 |
1.20 |
public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { |
428 |
dl |
1.1 |
final Phaser phaser = new Phaser(); |
429 |
jsr166 |
1.19 |
assertEquals(0, phaser.register()); |
430 |
jsr166 |
1.17 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
431 |
dl |
1.1 |
|
432 |
jsr166 |
1.7 |
Thread t = newStartedThread(new CheckedRunnable() { |
433 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
434 |
jsr166 |
1.20 |
Thread.currentThread().interrupt(); |
435 |
jsr166 |
1.19 |
assertEquals(0, phaser.register()); |
436 |
jsr166 |
1.20 |
assertEquals(0, phaser.arrive()); |
437 |
jsr166 |
1.17 |
threadStarted.countDown(); |
438 |
jsr166 |
1.20 |
assertTrue(Thread.currentThread().isInterrupted()); |
439 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
440 |
jsr166 |
1.17 |
assertTrue(Thread.currentThread().isInterrupted()); |
441 |
jsr166 |
1.5 |
}}); |
442 |
jsr166 |
1.20 |
|
443 |
jsr166 |
1.17 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
444 |
jsr166 |
1.20 |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
445 |
|
|
assertEquals(0, phaser.arrive()); |
446 |
|
|
awaitTermination(t, SMALL_DELAY_MS); |
447 |
|
|
|
448 |
|
|
Thread.currentThread().interrupt(); |
449 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
450 |
|
|
assertTrue(Thread.interrupted()); |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
/** |
454 |
|
|
* awaitAdvance continues waiting if interrupted while waiting |
455 |
|
|
*/ |
456 |
|
|
public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { |
457 |
|
|
final Phaser phaser = new Phaser(); |
458 |
|
|
assertEquals(0, phaser.register()); |
459 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
460 |
|
|
|
461 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
462 |
|
|
public void realRun() throws InterruptedException { |
463 |
|
|
assertEquals(0, phaser.register()); |
464 |
|
|
assertEquals(0, phaser.arrive()); |
465 |
|
|
threadStarted.countDown(); |
466 |
|
|
assertFalse(Thread.currentThread().isInterrupted()); |
467 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
468 |
|
|
assertTrue(Thread.currentThread().isInterrupted()); |
469 |
|
|
}}); |
470 |
|
|
|
471 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
472 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
473 |
jsr166 |
1.7 |
t.interrupt(); |
474 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
475 |
jsr166 |
1.17 |
awaitTermination(t, SMALL_DELAY_MS); |
476 |
jsr166 |
1.20 |
|
477 |
|
|
Thread.currentThread().interrupt(); |
478 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
479 |
|
|
assertTrue(Thread.interrupted()); |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
/** |
483 |
|
|
* arriveAndAwaitAdvance continues waiting if interrupted before waiting |
484 |
|
|
*/ |
485 |
|
|
public void testArriveAndAwaitAdvanceAfterInterrupt() |
486 |
|
|
throws InterruptedException { |
487 |
|
|
final Phaser phaser = new Phaser(); |
488 |
|
|
assertEquals(0, phaser.register()); |
489 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
490 |
|
|
|
491 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
492 |
|
|
public void realRun() throws InterruptedException { |
493 |
|
|
Thread.currentThread().interrupt(); |
494 |
|
|
assertEquals(0, phaser.register()); |
495 |
|
|
threadStarted.countDown(); |
496 |
|
|
assertTrue(Thread.currentThread().isInterrupted()); |
497 |
|
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
498 |
|
|
assertTrue(Thread.currentThread().isInterrupted()); |
499 |
|
|
}}); |
500 |
|
|
|
501 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
502 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
503 |
|
|
Thread.currentThread().interrupt(); |
504 |
|
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
505 |
|
|
assertTrue(Thread.interrupted()); |
506 |
|
|
awaitTermination(t, SMALL_DELAY_MS); |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
/** |
510 |
|
|
* arriveAndAwaitAdvance continues waiting if interrupted while waiting |
511 |
|
|
*/ |
512 |
|
|
public void testArriveAndAwaitAdvanceBeforeInterrupt() |
513 |
|
|
throws InterruptedException { |
514 |
|
|
final Phaser phaser = new Phaser(); |
515 |
|
|
assertEquals(0, phaser.register()); |
516 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
517 |
|
|
|
518 |
|
|
Thread t = newStartedThread(new CheckedRunnable() { |
519 |
|
|
public void realRun() throws InterruptedException { |
520 |
|
|
assertEquals(0, phaser.register()); |
521 |
|
|
threadStarted.countDown(); |
522 |
|
|
assertFalse(Thread.currentThread().isInterrupted()); |
523 |
|
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
524 |
|
|
assertTrue(Thread.currentThread().isInterrupted()); |
525 |
|
|
}}); |
526 |
|
|
|
527 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
528 |
|
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
529 |
|
|
t.interrupt(); |
530 |
|
|
Thread.currentThread().interrupt(); |
531 |
|
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
532 |
|
|
assertTrue(Thread.interrupted()); |
533 |
|
|
awaitTermination(t, SMALL_DELAY_MS); |
534 |
dl |
1.1 |
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
|
|
* awaitAdvance atomically waits for all parties within the same phase to |
538 |
|
|
* complete before continuing |
539 |
|
|
*/ |
540 |
jsr166 |
1.5 |
public void testAwaitAdvance4() throws InterruptedException { |
541 |
|
|
final Phaser phaser = new Phaser(4); |
542 |
jsr166 |
1.19 |
final AtomicInteger count = new AtomicInteger(0); |
543 |
jsr166 |
1.5 |
List<Thread> threads = new ArrayList<Thread>(); |
544 |
jsr166 |
1.19 |
for (int i = 0; i < 4; i++) |
545 |
jsr166 |
1.7 |
threads.add(newStartedThread(new CheckedRunnable() { |
546 |
jsr166 |
1.9 |
public void realRun() { |
547 |
jsr166 |
1.19 |
for (int k = 0; k < 3; k++) { |
548 |
|
|
assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); |
549 |
|
|
count.incrementAndGet(); |
550 |
|
|
assertEquals(2*k+1, phaser.arrive()); |
551 |
|
|
assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); |
552 |
|
|
assertEquals(count.get(), 4*(k+1)); |
553 |
|
|
}}})); |
554 |
|
|
|
555 |
jsr166 |
1.5 |
for (Thread thread : threads) |
556 |
jsr166 |
1.19 |
awaitTermination(thread, MEDIUM_DELAY_MS); |
557 |
dl |
1.1 |
} |
558 |
|
|
|
559 |
|
|
/** |
560 |
|
|
* awaitAdvance returns the current phase |
561 |
|
|
*/ |
562 |
jsr166 |
1.7 |
public void testAwaitAdvance5() throws InterruptedException { |
563 |
dl |
1.1 |
final Phaser phaser = new Phaser(1); |
564 |
jsr166 |
1.19 |
assertEquals(1, phaser.awaitAdvance(phaser.arrive())); |
565 |
|
|
assertEquals(1, phaser.getPhase()); |
566 |
|
|
assertEquals(1, phaser.register()); |
567 |
jsr166 |
1.7 |
List<Thread> threads = new ArrayList<Thread>(); |
568 |
|
|
for (int i = 0; i < 8; i++) { |
569 |
jsr166 |
1.18 |
final CountDownLatch latch = new CountDownLatch(1); |
570 |
|
|
final boolean goesFirst = ((i & 1) == 0); |
571 |
jsr166 |
1.7 |
threads.add(newStartedThread(new CheckedRunnable() { |
572 |
jsr166 |
1.18 |
public void realRun() throws InterruptedException { |
573 |
|
|
if (goesFirst) |
574 |
|
|
latch.countDown(); |
575 |
|
|
else |
576 |
|
|
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
577 |
dl |
1.1 |
phaser.arrive(); |
578 |
jsr166 |
1.7 |
}})); |
579 |
jsr166 |
1.18 |
if (goesFirst) |
580 |
|
|
assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); |
581 |
|
|
else |
582 |
|
|
latch.countDown(); |
583 |
jsr166 |
1.19 |
assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive())); |
584 |
|
|
assertEquals(i + 2, phaser.getPhase()); |
585 |
dl |
1.1 |
} |
586 |
jsr166 |
1.7 |
for (Thread thread : threads) |
587 |
jsr166 |
1.18 |
awaitTermination(thread, SMALL_DELAY_MS); |
588 |
dl |
1.1 |
} |
589 |
|
|
|
590 |
|
|
/** |
591 |
jsr166 |
1.29 |
* awaitAdvance returns the current phase in child phasers |
592 |
|
|
*/ |
593 |
|
|
public void testAwaitAdvanceTieredPhaser() throws Exception { |
594 |
|
|
final Phaser parent = new Phaser(); |
595 |
|
|
final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3); |
596 |
|
|
final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3); |
597 |
|
|
for (int i = 0; i < 3; i++) { |
598 |
|
|
zeroPartyChildren.add(new Phaser(parent, 0)); |
599 |
|
|
onePartyChildren.add(new Phaser(parent, 1)); |
600 |
|
|
} |
601 |
|
|
final List<Phaser> phasers = new ArrayList<Phaser>(); |
602 |
|
|
phasers.addAll(zeroPartyChildren); |
603 |
|
|
phasers.addAll(onePartyChildren); |
604 |
|
|
phasers.add(parent); |
605 |
|
|
for (Phaser phaser : phasers) { |
606 |
|
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
607 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
608 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
609 |
|
|
} |
610 |
|
|
|
611 |
|
|
for (Phaser child : onePartyChildren) |
612 |
|
|
assertEquals(0, child.arrive()); |
613 |
|
|
for (Phaser phaser : phasers) { |
614 |
|
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
615 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
616 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
617 |
|
|
assertEquals(1, phaser.awaitAdvance(0)); |
618 |
|
|
assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); |
619 |
|
|
assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
620 |
|
|
} |
621 |
|
|
|
622 |
|
|
for (Phaser child : onePartyChildren) |
623 |
|
|
assertEquals(1, child.arrive()); |
624 |
|
|
for (Phaser phaser : phasers) { |
625 |
|
|
assertEquals(-42, phaser.awaitAdvance(-42)); |
626 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); |
627 |
|
|
assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); |
628 |
|
|
assertEquals(2, phaser.awaitAdvance(0)); |
629 |
|
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); |
630 |
|
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); |
631 |
|
|
assertEquals(2, phaser.awaitAdvance(1)); |
632 |
|
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); |
633 |
|
|
assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); |
634 |
|
|
} |
635 |
|
|
} |
636 |
|
|
|
637 |
|
|
/** |
638 |
dl |
1.1 |
* awaitAdvance returns when the phaser is externally terminated |
639 |
|
|
*/ |
640 |
jsr166 |
1.7 |
public void testAwaitAdvance6() throws InterruptedException { |
641 |
dl |
1.1 |
final Phaser phaser = new Phaser(3); |
642 |
jsr166 |
1.18 |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
643 |
|
|
final List<Thread> threads = new ArrayList<Thread>(); |
644 |
|
|
for (int i = 0; i < 2; i++) { |
645 |
|
|
Runnable r = new CheckedRunnable() { |
646 |
|
|
public void realRun() { |
647 |
jsr166 |
1.19 |
assertEquals(0, phaser.arrive()); |
648 |
jsr166 |
1.18 |
threadsStarted.countDown(); |
649 |
jsr166 |
1.19 |
assertTrue(phaser.awaitAdvance(0) < 0); |
650 |
jsr166 |
1.18 |
assertTrue(phaser.isTerminated()); |
651 |
jsr166 |
1.19 |
assertTrue(phaser.getPhase() < 0); |
652 |
dl |
1.24 |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
653 |
jsr166 |
1.19 |
assertEquals(3, phaser.getRegisteredParties()); |
654 |
jsr166 |
1.18 |
}}; |
655 |
|
|
threads.add(newStartedThread(r)); |
656 |
|
|
} |
657 |
|
|
threadsStarted.await(); |
658 |
dl |
1.1 |
phaser.forceTermination(); |
659 |
jsr166 |
1.27 |
assertTrue(phaser.isTerminated()); |
660 |
dl |
1.24 |
assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE); |
661 |
jsr166 |
1.18 |
for (Thread thread : threads) |
662 |
|
|
awaitTermination(thread, SMALL_DELAY_MS); |
663 |
jsr166 |
1.19 |
assertEquals(3, phaser.getRegisteredParties()); |
664 |
dl |
1.1 |
} |
665 |
|
|
|
666 |
|
|
/** |
667 |
|
|
* arriveAndAwaitAdvance throws IllegalStateException with no |
668 |
|
|
* unarrived parties |
669 |
|
|
*/ |
670 |
|
|
public void testArriveAndAwaitAdvance1() { |
671 |
|
|
try { |
672 |
|
|
Phaser phaser = new Phaser(); |
673 |
|
|
phaser.arriveAndAwaitAdvance(); |
674 |
|
|
shouldThrow(); |
675 |
jsr166 |
1.8 |
} catch (IllegalStateException success) {} |
676 |
dl |
1.1 |
} |
677 |
|
|
|
678 |
|
|
/** |
679 |
|
|
* arriveAndAwaitAdvance waits for all threads to arrive, the |
680 |
|
|
* number of arrived parties is the same number that is accounted |
681 |
|
|
* for when the main thread awaitsAdvance |
682 |
|
|
*/ |
683 |
jsr166 |
1.7 |
public void testArriveAndAwaitAdvance3() throws InterruptedException { |
684 |
dl |
1.1 |
final Phaser phaser = new Phaser(1); |
685 |
jsr166 |
1.19 |
final int THREADS = 3; |
686 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
687 |
jsr166 |
1.7 |
final List<Thread> threads = new ArrayList<Thread>(); |
688 |
jsr166 |
1.19 |
for (int i = 0; i < THREADS; i++) |
689 |
jsr166 |
1.7 |
threads.add(newStartedThread(new CheckedRunnable() { |
690 |
jsr166 |
1.19 |
public void realRun() throws InterruptedException { |
691 |
|
|
assertEquals(0, phaser.register()); |
692 |
|
|
threadsStarted.countDown(); |
693 |
|
|
assertEquals(1, phaser.arriveAndAwaitAdvance()); |
694 |
|
|
}})); |
695 |
|
|
|
696 |
|
|
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
|
|
long t0 = System.nanoTime(); |
698 |
|
|
while (phaser.getArrivedParties() < THREADS) |
699 |
|
|
Thread.yield(); |
700 |
|
|
assertEquals(THREADS, phaser.getArrivedParties()); |
701 |
|
|
assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS); |
702 |
|
|
for (Thread thread : threads) |
703 |
|
|
assertTrue(thread.isAlive()); |
704 |
|
|
assertState(phaser, 0, THREADS + 1, 1); |
705 |
dl |
1.12 |
phaser.arriveAndAwaitAdvance(); |
706 |
jsr166 |
1.7 |
for (Thread thread : threads) |
707 |
jsr166 |
1.19 |
awaitTermination(thread, SMALL_DELAY_MS); |
708 |
|
|
assertState(phaser, 1, THREADS + 1, THREADS + 1); |
709 |
dl |
1.1 |
} |
710 |
|
|
|
711 |
|
|
} |