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. |
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 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import java.util.concurrent.*; |
13 |
|
|
14 |
|
public class DelayQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
|
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(DelayQueueTest.class); |
20 |
> |
return new TestSuite(DelayQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
24 |
|
|
25 |
|
/** |
26 |
< |
* A delayed implmentation for testing. |
27 |
< |
* Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
26 |
> |
* A delayed implementation for testing. |
27 |
> |
* Most tests use Pseudodelays, where delays are all elapsed |
28 |
|
* (so, no blocking solely for delays) but are still ordered |
29 |
< |
*/ |
30 |
< |
static class PDelay implements Delayed { |
29 |
> |
*/ |
30 |
> |
static class PDelay implements Delayed { |
31 |
|
int pseudodelay; |
32 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
33 |
< |
public int compareTo(Object y) { |
33 |
> |
public int compareTo(PDelay y) { |
34 |
|
int i = pseudodelay; |
35 |
|
int j = ((PDelay)y).pseudodelay; |
36 |
|
if (i < j) return -1; |
38 |
|
return 0; |
39 |
|
} |
40 |
|
|
41 |
< |
public int compareTo(PDelay y) { |
41 |
> |
public int compareTo(Delayed y) { |
42 |
|
int i = pseudodelay; |
43 |
|
int j = ((PDelay)y).pseudodelay; |
44 |
|
if (i < j) return -1; |
70 |
|
/** |
71 |
|
* Delayed implementation that actually delays |
72 |
|
*/ |
73 |
< |
static class NanoDelay implements Delayed { |
73 |
> |
static class NanoDelay implements Delayed { |
74 |
|
long trigger; |
75 |
< |
NanoDelay(long i) { |
75 |
> |
NanoDelay(long i) { |
76 |
|
trigger = System.nanoTime() + i; |
77 |
|
} |
78 |
< |
public int compareTo(Object y) { |
78 |
> |
public int compareTo(NanoDelay y) { |
79 |
|
long i = trigger; |
80 |
|
long j = ((NanoDelay)y).trigger; |
81 |
|
if (i < j) return -1; |
83 |
|
return 0; |
84 |
|
} |
85 |
|
|
86 |
< |
public int compareTo(NanoDelay y) { |
86 |
> |
public int compareTo(Delayed y) { |
87 |
|
long i = trigger; |
88 |
|
long j = ((NanoDelay)y).trigger; |
89 |
|
if (i < j) return -1; |
120 |
|
private DelayQueue populatedQueue(int n) { |
121 |
|
DelayQueue q = new DelayQueue(); |
122 |
|
assertTrue(q.isEmpty()); |
123 |
< |
for(int i = n-1; i >= 0; i-=2) |
124 |
< |
assertTrue(q.offer(new PDelay(i))); |
125 |
< |
for(int i = (n & 1); i < n; i+=2) |
126 |
< |
assertTrue(q.offer(new PDelay(i))); |
123 |
> |
for (int i = n-1; i >= 0; i-=2) |
124 |
> |
assertTrue(q.offer(new PDelay(i))); |
125 |
> |
for (int i = (n & 1); i < n; i+=2) |
126 |
> |
assertTrue(q.offer(new PDelay(i))); |
127 |
|
assertFalse(q.isEmpty()); |
128 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
129 |
< |
assertEquals(n, q.size()); |
129 |
> |
assertEquals(n, q.size()); |
130 |
|
return q; |
131 |
|
} |
132 |
< |
|
132 |
> |
|
133 |
|
/** |
134 |
< |
* |
134 |
> |
* A new queue has unbounded capacity |
135 |
|
*/ |
136 |
|
public void testConstructor1() { |
137 |
|
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* |
141 |
> |
* Initializing from null Collection throws NPE |
142 |
|
*/ |
143 |
|
public void testConstructor3() { |
144 |
|
try { |
145 |
|
DelayQueue q = new DelayQueue(null); |
146 |
|
shouldThrow(); |
147 |
< |
} |
146 |
< |
catch (NullPointerException success) {} |
147 |
> |
} catch (NullPointerException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* |
151 |
> |
* Initializing from Collection of null elements throws NPE |
152 |
|
*/ |
153 |
|
public void testConstructor4() { |
154 |
|
try { |
155 |
|
PDelay[] ints = new PDelay[SIZE]; |
156 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
157 |
|
shouldThrow(); |
158 |
< |
} |
158 |
< |
catch (NullPointerException success) {} |
158 |
> |
} catch (NullPointerException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* |
162 |
> |
* Initializing from Collection with some null elements throws NPE |
163 |
|
*/ |
164 |
|
public void testConstructor5() { |
165 |
|
try { |
168 |
|
ints[i] = new PDelay(i); |
169 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
|
shouldThrow(); |
171 |
< |
} |
172 |
< |
catch (NullPointerException success) {} |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* |
175 |
> |
* Queue contains all elements of collection used to initialize |
176 |
|
*/ |
177 |
|
public void testConstructor6() { |
178 |
|
try { |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* |
190 |
> |
* isEmpty is true before add, false after |
191 |
|
*/ |
192 |
|
public void testEmpty() { |
193 |
|
DelayQueue q = new DelayQueue(); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* |
205 |
> |
* remainingCapacity does not change when elementa added or removed, |
206 |
> |
* but size does |
207 |
|
*/ |
208 |
|
public void testRemainingCapacity() { |
209 |
|
DelayQueue q = populatedQueue(SIZE); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* |
223 |
> |
* offer(null) throws NPE |
224 |
|
*/ |
225 |
|
public void testOfferNull() { |
226 |
< |
try { |
226 |
> |
try { |
227 |
|
DelayQueue q = new DelayQueue(); |
228 |
|
q.offer(null); |
229 |
|
shouldThrow(); |
230 |
< |
} catch (NullPointerException success) { } |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
< |
* |
234 |
> |
* add(null) throws NPE |
235 |
> |
*/ |
236 |
> |
public void testAddNull() { |
237 |
> |
try { |
238 |
> |
DelayQueue q = new DelayQueue(); |
239 |
> |
q.add(null); |
240 |
> |
shouldThrow(); |
241 |
> |
} catch (NullPointerException success) {} |
242 |
> |
} |
243 |
> |
|
244 |
> |
/** |
245 |
> |
* offer non-null succeeds |
246 |
|
*/ |
247 |
|
public void testOffer() { |
248 |
|
DelayQueue q = new DelayQueue(); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* |
254 |
> |
* add succeeds |
255 |
|
*/ |
256 |
|
public void testAdd() { |
257 |
|
DelayQueue q = new DelayQueue(); |
262 |
|
} |
263 |
|
|
264 |
|
/** |
265 |
< |
* |
265 |
> |
* addAll(null) throws NPE |
266 |
|
*/ |
267 |
|
public void testAddAll1() { |
268 |
|
try { |
269 |
|
DelayQueue q = new DelayQueue(); |
270 |
|
q.addAll(null); |
271 |
|
shouldThrow(); |
272 |
< |
} |
273 |
< |
catch (NullPointerException success) {} |
272 |
> |
} catch (NullPointerException success) {} |
273 |
> |
} |
274 |
> |
|
275 |
> |
|
276 |
> |
/** |
277 |
> |
* addAll(this) throws IAE |
278 |
> |
*/ |
279 |
> |
public void testAddAllSelf() { |
280 |
> |
try { |
281 |
> |
DelayQueue q = populatedQueue(SIZE); |
282 |
> |
q.addAll(q); |
283 |
> |
shouldThrow(); |
284 |
> |
} catch (IllegalArgumentException success) {} |
285 |
|
} |
286 |
+ |
|
287 |
|
/** |
288 |
< |
* |
288 |
> |
* addAll of a collection with null elements throws NPE |
289 |
|
*/ |
290 |
|
public void testAddAll2() { |
291 |
|
try { |
293 |
|
PDelay[] ints = new PDelay[SIZE]; |
294 |
|
q.addAll(Arrays.asList(ints)); |
295 |
|
shouldThrow(); |
296 |
< |
} |
274 |
< |
catch (NullPointerException success) {} |
296 |
> |
} catch (NullPointerException success) {} |
297 |
|
} |
298 |
|
/** |
299 |
< |
* |
299 |
> |
* addAll of a collection with any null elements throws NPE after |
300 |
> |
* possibly adding some elements |
301 |
|
*/ |
302 |
|
public void testAddAll3() { |
303 |
|
try { |
307 |
|
ints[i] = new PDelay(i); |
308 |
|
q.addAll(Arrays.asList(ints)); |
309 |
|
shouldThrow(); |
310 |
< |
} |
288 |
< |
catch (NullPointerException success) {} |
310 |
> |
} catch (NullPointerException success) {} |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
< |
* |
314 |
> |
* Queue contains all elements of successful addAll |
315 |
|
*/ |
316 |
|
public void testAddAll5() { |
317 |
|
try { |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* |
332 |
> |
* put(null) throws NPE |
333 |
|
*/ |
334 |
|
public void testPutNull() { |
335 |
< |
try { |
335 |
> |
try { |
336 |
|
DelayQueue q = new DelayQueue(); |
337 |
|
q.put(null); |
338 |
|
shouldThrow(); |
339 |
< |
} |
318 |
< |
catch (NullPointerException success){ |
319 |
< |
} |
339 |
> |
} catch (NullPointerException success) {} |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* |
343 |
> |
* all elements successfully put are contained |
344 |
|
*/ |
345 |
|
public void testPut() { |
346 |
< |
try { |
347 |
< |
DelayQueue q = new DelayQueue(); |
348 |
< |
for (int i = 0; i < SIZE; ++i) { |
349 |
< |
PDelay I = new PDelay(i); |
350 |
< |
q.put(I); |
331 |
< |
assertTrue(q.contains(I)); |
332 |
< |
} |
333 |
< |
assertEquals(SIZE, q.size()); |
346 |
> |
DelayQueue q = new DelayQueue(); |
347 |
> |
for (int i = 0; i < SIZE; ++i) { |
348 |
> |
PDelay I = new PDelay(i); |
349 |
> |
q.put(I); |
350 |
> |
assertTrue(q.contains(I)); |
351 |
|
} |
352 |
< |
finally { |
336 |
< |
} |
352 |
> |
assertEquals(SIZE, q.size()); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
< |
* |
356 |
> |
* put doesn't block waiting for take |
357 |
|
*/ |
358 |
< |
public void testPutWithTake() { |
358 |
> |
public void testPutWithTake() throws InterruptedException { |
359 |
|
final DelayQueue q = new DelayQueue(); |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run() { |
362 |
< |
int added = 0; |
363 |
< |
try { |
364 |
< |
q.put(new PDelay(0)); |
365 |
< |
++added; |
366 |
< |
q.put(new PDelay(0)); |
367 |
< |
++added; |
368 |
< |
q.put(new PDelay(0)); |
369 |
< |
++added; |
370 |
< |
q.put(new PDelay(0)); |
371 |
< |
++added; |
372 |
< |
threadAssertTrue(added == 4); |
357 |
< |
} finally { |
358 |
< |
} |
359 |
< |
} |
360 |
< |
}); |
361 |
< |
try { |
362 |
< |
t.start(); |
363 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
< |
q.take(); |
365 |
< |
t.interrupt(); |
366 |
< |
t.join(); |
367 |
< |
} catch (Exception e){ |
368 |
< |
unexpectedException(); |
369 |
< |
} |
360 |
> |
Thread t = new Thread(new CheckedRunnable() { |
361 |
> |
public void realRun() { |
362 |
> |
q.put(new PDelay(0)); |
363 |
> |
q.put(new PDelay(0)); |
364 |
> |
q.put(new PDelay(0)); |
365 |
> |
q.put(new PDelay(0)); |
366 |
> |
}}); |
367 |
> |
|
368 |
> |
t.start(); |
369 |
> |
Thread.sleep(SHORT_DELAY_MS); |
370 |
> |
q.take(); |
371 |
> |
t.interrupt(); |
372 |
> |
t.join(); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
< |
* |
376 |
> |
* timed offer does not time out |
377 |
|
*/ |
378 |
< |
public void testTimedOffer() { |
378 |
> |
public void testTimedOffer() throws InterruptedException { |
379 |
|
final DelayQueue q = new DelayQueue(); |
380 |
< |
Thread t = new Thread(new Runnable() { |
381 |
< |
public void run() { |
382 |
< |
try { |
383 |
< |
q.put(new PDelay(0)); |
384 |
< |
q.put(new PDelay(0)); |
385 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
386 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
387 |
< |
} finally { } |
388 |
< |
} |
389 |
< |
}); |
390 |
< |
|
391 |
< |
try { |
389 |
< |
t.start(); |
390 |
< |
Thread.sleep(SMALL_DELAY_MS); |
391 |
< |
t.interrupt(); |
392 |
< |
t.join(); |
393 |
< |
} catch (Exception e){ |
394 |
< |
unexpectedException(); |
395 |
< |
} |
380 |
> |
Thread t = new Thread(new CheckedRunnable() { |
381 |
> |
public void realRun() throws InterruptedException { |
382 |
> |
q.put(new PDelay(0)); |
383 |
> |
q.put(new PDelay(0)); |
384 |
> |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
385 |
> |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
386 |
> |
}}); |
387 |
> |
|
388 |
> |
t.start(); |
389 |
> |
Thread.sleep(SMALL_DELAY_MS); |
390 |
> |
t.interrupt(); |
391 |
> |
t.join(); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* |
395 |
> |
* take retrieves elements in priority order |
396 |
|
*/ |
397 |
< |
public void testTake() { |
398 |
< |
try { |
399 |
< |
DelayQueue q = populatedQueue(SIZE); |
400 |
< |
for (int i = 0; i < SIZE; ++i) { |
401 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
406 |
< |
} |
407 |
< |
} catch (InterruptedException e){ |
408 |
< |
unexpectedException(); |
409 |
< |
} |
397 |
> |
public void testTake() throws InterruptedException { |
398 |
> |
DelayQueue q = populatedQueue(SIZE); |
399 |
> |
for (int i = 0; i < SIZE; ++i) { |
400 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
401 |
> |
} |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
< |
* |
405 |
> |
* take blocks interruptibly when empty |
406 |
|
*/ |
407 |
< |
public void testTakeFromEmpty() { |
407 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
408 |
|
final DelayQueue q = new DelayQueue(); |
409 |
< |
Thread t = new Thread(new Runnable() { |
410 |
< |
public void run() { |
411 |
< |
try { |
412 |
< |
q.take(); |
413 |
< |
threadShouldThrow(); |
414 |
< |
} catch (InterruptedException success){ } |
415 |
< |
} |
416 |
< |
}); |
417 |
< |
try { |
426 |
< |
t.start(); |
427 |
< |
Thread.sleep(SHORT_DELAY_MS); |
428 |
< |
t.interrupt(); |
429 |
< |
t.join(); |
430 |
< |
} catch (Exception e){ |
431 |
< |
unexpectedException(); |
432 |
< |
} |
409 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
410 |
> |
public void realRun() throws InterruptedException { |
411 |
> |
q.take(); |
412 |
> |
}}; |
413 |
> |
|
414 |
> |
t.start(); |
415 |
> |
Thread.sleep(SHORT_DELAY_MS); |
416 |
> |
t.interrupt(); |
417 |
> |
t.join(); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* |
422 |
< |
*/ |
423 |
< |
public void testBlockingTake() { |
424 |
< |
Thread t = new Thread(new Runnable() { |
425 |
< |
public void run() { |
426 |
< |
try { |
427 |
< |
DelayQueue q = populatedQueue(SIZE); |
428 |
< |
for (int i = 0; i < SIZE; ++i) { |
429 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
430 |
< |
} |
431 |
< |
q.take(); |
432 |
< |
threadShouldThrow(); |
448 |
< |
} catch (InterruptedException success){ |
449 |
< |
} |
450 |
< |
}}); |
421 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
422 |
> |
*/ |
423 |
> |
public void testBlockingTake() throws InterruptedException { |
424 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
425 |
> |
public void realRun() throws InterruptedException { |
426 |
> |
DelayQueue q = populatedQueue(SIZE); |
427 |
> |
for (int i = 0; i < SIZE; ++i) { |
428 |
> |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
429 |
> |
} |
430 |
> |
q.take(); |
431 |
> |
}}; |
432 |
> |
|
433 |
|
t.start(); |
434 |
< |
try { |
435 |
< |
Thread.sleep(SHORT_DELAY_MS); |
436 |
< |
t.interrupt(); |
455 |
< |
t.join(); |
456 |
< |
} |
457 |
< |
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
459 |
< |
} |
434 |
> |
Thread.sleep(SHORT_DELAY_MS); |
435 |
> |
t.interrupt(); |
436 |
> |
t.join(); |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
|
/** |
441 |
< |
* |
441 |
> |
* poll succeeds unless empty |
442 |
|
*/ |
443 |
|
public void testPoll() { |
444 |
|
DelayQueue q = populatedQueue(SIZE); |
445 |
|
for (int i = 0; i < SIZE; ++i) { |
446 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
447 |
|
} |
448 |
< |
assertNull(q.poll()); |
448 |
> |
assertNull(q.poll()); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
< |
* |
452 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
453 |
|
*/ |
454 |
< |
public void testTimedPoll0() { |
455 |
< |
try { |
456 |
< |
DelayQueue q = populatedQueue(SIZE); |
457 |
< |
for (int i = 0; i < SIZE; ++i) { |
458 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
459 |
< |
} |
483 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
454 |
> |
public void testTimedPoll0() throws InterruptedException { |
455 |
> |
DelayQueue q = populatedQueue(SIZE); |
456 |
> |
for (int i = 0; i < SIZE; ++i) { |
457 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
458 |
> |
} |
459 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* |
463 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
464 |
|
*/ |
465 |
< |
public void testTimedPoll() { |
466 |
< |
try { |
467 |
< |
DelayQueue q = populatedQueue(SIZE); |
468 |
< |
for (int i = 0; i < SIZE; ++i) { |
469 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
470 |
< |
} |
498 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
465 |
> |
public void testTimedPoll() throws InterruptedException { |
466 |
> |
DelayQueue q = populatedQueue(SIZE); |
467 |
> |
for (int i = 0; i < SIZE; ++i) { |
468 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
469 |
> |
} |
470 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* |
475 |
< |
*/ |
476 |
< |
public void testInterruptedTimedPoll() { |
477 |
< |
Thread t = new Thread(new Runnable() { |
478 |
< |
public void run() { |
479 |
< |
try { |
480 |
< |
DelayQueue q = populatedQueue(SIZE); |
481 |
< |
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
483 |
< |
} |
484 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
485 |
< |
} catch (InterruptedException success){ |
486 |
< |
} |
487 |
< |
}}); |
474 |
> |
* Interrupted timed poll throws InterruptedException instead of |
475 |
> |
* returning timeout status |
476 |
> |
*/ |
477 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
478 |
> |
Thread t = new Thread(new CheckedRunnable() { |
479 |
> |
public void realRun() throws InterruptedException { |
480 |
> |
DelayQueue q = populatedQueue(SIZE); |
481 |
> |
for (int i = 0; i < SIZE; ++i) { |
482 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
483 |
> |
} |
484 |
> |
try { |
485 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
> |
shouldThrow(); |
487 |
> |
} catch (InterruptedException success) {} |
488 |
> |
}}); |
489 |
> |
|
490 |
|
t.start(); |
491 |
< |
try { |
492 |
< |
Thread.sleep(SHORT_DELAY_MS); |
493 |
< |
t.interrupt(); |
523 |
< |
t.join(); |
524 |
< |
} |
525 |
< |
catch (InterruptedException ie) { |
526 |
< |
unexpectedException(); |
527 |
< |
} |
491 |
> |
Thread.sleep(SHORT_DELAY_MS); |
492 |
> |
t.interrupt(); |
493 |
> |
t.join(); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
< |
* |
497 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
498 |
> |
* on interruption throws |
499 |
|
*/ |
500 |
< |
public void testTimedPollWithOffer() { |
500 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
501 |
|
final DelayQueue q = new DelayQueue(); |
502 |
< |
Thread t = new Thread(new Runnable() { |
503 |
< |
public void run() { |
504 |
< |
try { |
505 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
506 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
507 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
508 |
< |
threadFail("Should block"); |
509 |
< |
} catch (InterruptedException success) { } |
510 |
< |
} |
511 |
< |
}); |
512 |
< |
try { |
513 |
< |
t.start(); |
514 |
< |
Thread.sleep(SMALL_DELAY_MS); |
515 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
516 |
< |
t.interrupt(); |
517 |
< |
t.join(); |
551 |
< |
} catch (Exception e){ |
552 |
< |
unexpectedException(); |
553 |
< |
} |
554 |
< |
} |
502 |
> |
Thread t = new Thread(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
505 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
506 |
> |
try { |
507 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
508 |
> |
shouldThrow(); |
509 |
> |
} catch (InterruptedException success) {} |
510 |
> |
}}); |
511 |
> |
|
512 |
> |
t.start(); |
513 |
> |
Thread.sleep(SMALL_DELAY_MS); |
514 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
515 |
> |
t.interrupt(); |
516 |
> |
t.join(); |
517 |
> |
} |
518 |
|
|
519 |
|
|
520 |
|
/** |
521 |
< |
* |
521 |
> |
* peek returns next element, or null if empty |
522 |
|
*/ |
523 |
|
public void testPeek() { |
524 |
|
DelayQueue q = populatedQueue(SIZE); |
525 |
|
for (int i = 0; i < SIZE; ++i) { |
526 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
527 |
|
q.poll(); |
528 |
< |
assertTrue(q.peek() == null || |
529 |
< |
i != ((PDelay)q.peek()).intValue()); |
528 |
> |
if (q.isEmpty()) |
529 |
> |
assertNull(q.peek()); |
530 |
> |
else |
531 |
> |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
532 |
|
} |
533 |
< |
assertNull(q.peek()); |
533 |
> |
assertNull(q.peek()); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
537 |
< |
* |
537 |
> |
* element returns next element, or throws NSEE if empty |
538 |
|
*/ |
539 |
|
public void testElement() { |
540 |
|
DelayQueue q = populatedQueue(SIZE); |
545 |
|
try { |
546 |
|
q.element(); |
547 |
|
shouldThrow(); |
548 |
< |
} |
584 |
< |
catch (NoSuchElementException success) {} |
548 |
> |
} catch (NoSuchElementException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
< |
* |
552 |
> |
* remove removes next element, or throws NSEE if empty |
553 |
|
*/ |
554 |
|
public void testRemove() { |
555 |
|
DelayQueue q = populatedQueue(SIZE); |
559 |
|
try { |
560 |
|
q.remove(); |
561 |
|
shouldThrow(); |
562 |
< |
} catch (NoSuchElementException success){ |
599 |
< |
} |
562 |
> |
} catch (NoSuchElementException success) {} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
< |
* |
566 |
> |
* remove(x) removes x and returns true if present |
567 |
|
*/ |
568 |
|
public void testRemoveElement() { |
569 |
|
DelayQueue q = populatedQueue(SIZE); |
576 |
|
} |
577 |
|
assertTrue(q.isEmpty()); |
578 |
|
} |
579 |
< |
|
579 |
> |
|
580 |
|
/** |
581 |
< |
* |
581 |
> |
* contains(x) reports true when elements added but not yet removed |
582 |
|
*/ |
583 |
|
public void testContains() { |
584 |
|
DelayQueue q = populatedQueue(SIZE); |
590 |
|
} |
591 |
|
|
592 |
|
/** |
593 |
< |
* |
593 |
> |
* clear removes all elements |
594 |
|
*/ |
595 |
|
public void testClear() { |
596 |
|
DelayQueue q = populatedQueue(SIZE); |
598 |
|
assertTrue(q.isEmpty()); |
599 |
|
assertEquals(0, q.size()); |
600 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
601 |
< |
q.add(new PDelay(1)); |
601 |
> |
PDelay x = new PDelay(1); |
602 |
> |
q.add(x); |
603 |
|
assertFalse(q.isEmpty()); |
604 |
+ |
assertTrue(q.contains(x)); |
605 |
|
q.clear(); |
606 |
|
assertTrue(q.isEmpty()); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
< |
* |
610 |
> |
* containsAll(c) is true when c contains a subset of elements |
611 |
|
*/ |
612 |
|
public void testContainsAll() { |
613 |
|
DelayQueue q = populatedQueue(SIZE); |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
< |
* |
624 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
625 |
|
*/ |
626 |
|
public void testRetainAll() { |
627 |
|
DelayQueue q = populatedQueue(SIZE); |
640 |
|
} |
641 |
|
|
642 |
|
/** |
643 |
< |
* |
643 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
644 |
|
*/ |
645 |
|
public void testRemoveAll() { |
646 |
|
for (int i = 1; i < SIZE; ++i) { |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* |
659 |
> |
* toArray contains all elements |
660 |
|
*/ |
661 |
< |
public void testToArray() { |
661 |
> |
public void testToArray() throws InterruptedException { |
662 |
|
DelayQueue q = populatedQueue(SIZE); |
663 |
< |
Object[] o = q.toArray(); |
663 |
> |
Object[] o = q.toArray(); |
664 |
|
Arrays.sort(o); |
665 |
< |
try { |
666 |
< |
for(int i = 0; i < o.length; i++) |
702 |
< |
assertEquals(o[i], q.take()); |
703 |
< |
} catch (InterruptedException e){ |
704 |
< |
unexpectedException(); |
705 |
< |
} |
665 |
> |
for (int i = 0; i < o.length; i++) |
666 |
> |
assertEquals(o[i], q.take()); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* |
670 |
> |
* toArray(a) contains all elements |
671 |
|
*/ |
672 |
< |
public void testToArray2() { |
672 |
> |
public void testToArray2() throws InterruptedException { |
673 |
|
DelayQueue q = populatedQueue(SIZE); |
674 |
< |
PDelay[] ints = new PDelay[SIZE]; |
675 |
< |
ints = (PDelay[])q.toArray(ints); |
674 |
> |
PDelay[] ints = new PDelay[SIZE]; |
675 |
> |
ints = (PDelay[])q.toArray(ints); |
676 |
|
Arrays.sort(ints); |
677 |
< |
try { |
678 |
< |
for(int i = 0; i < ints.length; i++) |
679 |
< |
assertEquals(ints[i], q.take()); |
680 |
< |
} catch (InterruptedException e){ |
681 |
< |
unexpectedException(); |
682 |
< |
} |
677 |
> |
for (int i = 0; i < ints.length; i++) |
678 |
> |
assertEquals(ints[i], q.take()); |
679 |
> |
} |
680 |
> |
|
681 |
> |
|
682 |
> |
/** |
683 |
> |
* toArray(null) throws NPE |
684 |
> |
*/ |
685 |
> |
public void testToArray_BadArg() { |
686 |
> |
try { |
687 |
> |
DelayQueue q = populatedQueue(SIZE); |
688 |
> |
Object o[] = q.toArray(null); |
689 |
> |
shouldThrow(); |
690 |
> |
} catch (NullPointerException success) {} |
691 |
> |
} |
692 |
> |
|
693 |
> |
/** |
694 |
> |
* toArray with incompatible array type throws CCE |
695 |
> |
*/ |
696 |
> |
public void testToArray1_BadArg() { |
697 |
> |
try { |
698 |
> |
DelayQueue q = populatedQueue(SIZE); |
699 |
> |
Object o[] = q.toArray(new String[10] ); |
700 |
> |
shouldThrow(); |
701 |
> |
} catch (ArrayStoreException success) {} |
702 |
|
} |
703 |
< |
|
703 |
> |
|
704 |
|
/** |
705 |
< |
* |
705 |
> |
* iterator iterates through all elements |
706 |
|
*/ |
707 |
|
public void testIterator() { |
708 |
|
DelayQueue q = populatedQueue(SIZE); |
709 |
|
int i = 0; |
710 |
< |
Iterator it = q.iterator(); |
711 |
< |
while(it.hasNext()) { |
710 |
> |
Iterator it = q.iterator(); |
711 |
> |
while (it.hasNext()) { |
712 |
|
assertTrue(q.contains(it.next())); |
713 |
|
++i; |
714 |
|
} |
716 |
|
} |
717 |
|
|
718 |
|
/** |
719 |
< |
* |
719 |
> |
* iterator.remove removes current element |
720 |
|
*/ |
721 |
|
public void testIteratorRemove () { |
742 |
– |
|
722 |
|
final DelayQueue q = new DelayQueue(); |
744 |
– |
|
723 |
|
q.add(new PDelay(2)); |
724 |
|
q.add(new PDelay(1)); |
725 |
|
q.add(new PDelay(3)); |
748 |
– |
|
726 |
|
Iterator it = q.iterator(); |
727 |
|
it.next(); |
728 |
|
it.remove(); |
752 |
– |
|
729 |
|
it = q.iterator(); |
730 |
|
assertEquals(it.next(), new PDelay(2)); |
731 |
|
assertEquals(it.next(), new PDelay(3)); |
734 |
|
|
735 |
|
|
736 |
|
/** |
737 |
< |
* |
737 |
> |
* toString contains toStrings of elements |
738 |
|
*/ |
739 |
|
public void testToString() { |
740 |
|
DelayQueue q = populatedQueue(SIZE); |
742 |
|
for (int i = 0; i < SIZE; ++i) { |
743 |
|
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
744 |
|
} |
745 |
< |
} |
745 |
> |
} |
746 |
|
|
747 |
|
/** |
748 |
< |
* |
748 |
> |
* offer transfers elements across Executor tasks |
749 |
|
*/ |
750 |
|
public void testPollInExecutor() { |
775 |
– |
|
751 |
|
final DelayQueue q = new DelayQueue(); |
777 |
– |
|
752 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
753 |
< |
|
754 |
< |
executor.execute(new Runnable() { |
781 |
< |
public void run() { |
753 |
> |
executor.execute(new CheckedRunnable() { |
754 |
> |
public void realRun() throws InterruptedException { |
755 |
|
threadAssertNull(q.poll()); |
756 |
< |
try { |
757 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
758 |
< |
threadAssertTrue(q.isEmpty()); |
759 |
< |
} |
760 |
< |
catch (InterruptedException e) { |
761 |
< |
threadUnexpectedException(); |
762 |
< |
} |
763 |
< |
} |
764 |
< |
}); |
756 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
757 |
> |
threadAssertTrue(q.isEmpty()); |
758 |
> |
}}); |
759 |
> |
|
760 |
> |
executor.execute(new CheckedRunnable() { |
761 |
> |
public void realRun() throws InterruptedException { |
762 |
> |
Thread.sleep(SHORT_DELAY_MS); |
763 |
> |
q.put(new PDelay(1)); |
764 |
> |
}}); |
765 |
|
|
793 |
– |
executor.execute(new Runnable() { |
794 |
– |
public void run() { |
795 |
– |
try { |
796 |
– |
Thread.sleep(SHORT_DELAY_MS); |
797 |
– |
q.put(new PDelay(1)); |
798 |
– |
} |
799 |
– |
catch (InterruptedException e) { |
800 |
– |
threadUnexpectedException(); |
801 |
– |
} |
802 |
– |
} |
803 |
– |
}); |
804 |
– |
|
766 |
|
joinPool(executor); |
767 |
|
|
768 |
|
} |
769 |
|
|
770 |
|
|
771 |
|
/** |
772 |
< |
* |
772 |
> |
* Delayed actions do not occur until their delay elapses |
773 |
|
*/ |
774 |
< |
public void testDelay() { |
774 |
> |
public void testDelay() throws InterruptedException { |
775 |
|
DelayQueue q = new DelayQueue(); |
776 |
|
NanoDelay[] elements = new NanoDelay[SIZE]; |
777 |
|
for (int i = 0; i < SIZE; ++i) { |
781 |
|
q.add(elements[i]); |
782 |
|
} |
783 |
|
|
784 |
+ |
long last = 0; |
785 |
+ |
for (int i = 0; i < SIZE; ++i) { |
786 |
+ |
NanoDelay e = (NanoDelay)(q.take()); |
787 |
+ |
long tt = e.getTriggerTime(); |
788 |
+ |
assertTrue(tt <= System.nanoTime()); |
789 |
+ |
if (i != 0) |
790 |
+ |
assertTrue(tt >= last); |
791 |
+ |
last = tt; |
792 |
+ |
} |
793 |
+ |
} |
794 |
+ |
|
795 |
+ |
/** |
796 |
+ |
* peek of a non-empty queue returns non-null even if not expired |
797 |
+ |
*/ |
798 |
+ |
public void testPeekDelayed() { |
799 |
+ |
DelayQueue q = new DelayQueue(); |
800 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
801 |
+ |
assert(q.peek() != null); |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
|
805 |
+ |
/** |
806 |
+ |
* poll of a non-empty queue returns null if no expired elements. |
807 |
+ |
*/ |
808 |
+ |
public void testPollDelayed() { |
809 |
+ |
DelayQueue q = new DelayQueue(); |
810 |
+ |
q.add(new NanoDelay(Long.MAX_VALUE)); |
811 |
+ |
assertNull(q.poll()); |
812 |
+ |
} |
813 |
+ |
|
814 |
+ |
/** |
815 |
+ |
* timed poll of a non-empty queue returns null if no expired elements. |
816 |
+ |
*/ |
817 |
+ |
public void testTimedPollDelayed() throws InterruptedException { |
818 |
+ |
DelayQueue q = new DelayQueue(); |
819 |
+ |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
820 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
821 |
+ |
} |
822 |
+ |
|
823 |
+ |
/** |
824 |
+ |
* drainTo(null) throws NPE |
825 |
+ |
*/ |
826 |
+ |
public void testDrainToNull() { |
827 |
+ |
DelayQueue q = populatedQueue(SIZE); |
828 |
|
try { |
829 |
< |
long last = 0; |
830 |
< |
for (int i = 0; i < SIZE; ++i) { |
831 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
832 |
< |
long tt = e.getTriggerTime(); |
833 |
< |
assertTrue(tt <= System.nanoTime()); |
834 |
< |
if (i != 0) |
835 |
< |
assertTrue(tt >= last); |
836 |
< |
last = tt; |
837 |
< |
} |
829 |
> |
q.drainTo(null); |
830 |
> |
shouldThrow(); |
831 |
> |
} catch (NullPointerException success) {} |
832 |
> |
} |
833 |
> |
|
834 |
> |
/** |
835 |
> |
* drainTo(this) throws IAE |
836 |
> |
*/ |
837 |
> |
public void testDrainToSelf() { |
838 |
> |
DelayQueue q = populatedQueue(SIZE); |
839 |
> |
try { |
840 |
> |
q.drainTo(q); |
841 |
> |
shouldThrow(); |
842 |
> |
} catch (IllegalArgumentException success) {} |
843 |
> |
} |
844 |
> |
|
845 |
> |
/** |
846 |
> |
* drainTo(c) empties queue into another collection c |
847 |
> |
*/ |
848 |
> |
public void testDrainTo() { |
849 |
> |
DelayQueue q = new DelayQueue(); |
850 |
> |
PDelay[] elems = new PDelay[SIZE]; |
851 |
> |
for (int i = 0; i < SIZE; ++i) { |
852 |
> |
elems[i] = new PDelay(i); |
853 |
> |
q.add(elems[i]); |
854 |
|
} |
855 |
< |
catch(InterruptedException ie) { |
856 |
< |
unexpectedException(); |
855 |
> |
ArrayList l = new ArrayList(); |
856 |
> |
q.drainTo(l); |
857 |
> |
assertEquals(q.size(), 0); |
858 |
> |
for (int i = 0; i < SIZE; ++i) |
859 |
> |
assertEquals(l.get(i), elems[i]); |
860 |
> |
q.add(elems[0]); |
861 |
> |
q.add(elems[1]); |
862 |
> |
assertFalse(q.isEmpty()); |
863 |
> |
assertTrue(q.contains(elems[0])); |
864 |
> |
assertTrue(q.contains(elems[1])); |
865 |
> |
l.clear(); |
866 |
> |
q.drainTo(l); |
867 |
> |
assertEquals(q.size(), 0); |
868 |
> |
assertEquals(l.size(), 2); |
869 |
> |
for (int i = 0; i < 2; ++i) |
870 |
> |
assertEquals(l.get(i), elems[i]); |
871 |
> |
} |
872 |
> |
|
873 |
> |
/** |
874 |
> |
* drainTo empties queue |
875 |
> |
*/ |
876 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
877 |
> |
final DelayQueue q = populatedQueue(SIZE); |
878 |
> |
Thread t = new Thread(new CheckedRunnable() { |
879 |
> |
public void realRun() { |
880 |
> |
q.put(new PDelay(SIZE+1)); |
881 |
> |
}}); |
882 |
> |
|
883 |
> |
t.start(); |
884 |
> |
ArrayList l = new ArrayList(); |
885 |
> |
q.drainTo(l); |
886 |
> |
assertTrue(l.size() >= SIZE); |
887 |
> |
t.join(); |
888 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
889 |
> |
} |
890 |
> |
|
891 |
> |
/** |
892 |
> |
* drainTo(null, n) throws NPE |
893 |
> |
*/ |
894 |
> |
public void testDrainToNullN() { |
895 |
> |
DelayQueue q = populatedQueue(SIZE); |
896 |
> |
try { |
897 |
> |
q.drainTo(null, 0); |
898 |
> |
shouldThrow(); |
899 |
> |
} catch (NullPointerException success) {} |
900 |
> |
} |
901 |
> |
|
902 |
> |
/** |
903 |
> |
* drainTo(this, n) throws IAE |
904 |
> |
*/ |
905 |
> |
public void testDrainToSelfN() { |
906 |
> |
DelayQueue q = populatedQueue(SIZE); |
907 |
> |
try { |
908 |
> |
q.drainTo(q, 0); |
909 |
> |
shouldThrow(); |
910 |
> |
} catch (IllegalArgumentException success) {} |
911 |
> |
} |
912 |
> |
|
913 |
> |
/** |
914 |
> |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
915 |
> |
*/ |
916 |
> |
public void testDrainToN() { |
917 |
> |
for (int i = 0; i < SIZE + 2; ++i) { |
918 |
> |
DelayQueue q = populatedQueue(SIZE); |
919 |
> |
ArrayList l = new ArrayList(); |
920 |
> |
q.drainTo(l, i); |
921 |
> |
int k = (i < SIZE)? i : SIZE; |
922 |
> |
assertEquals(q.size(), SIZE-k); |
923 |
> |
assertEquals(l.size(), k); |
924 |
|
} |
925 |
|
} |
926 |
|
|
927 |
+ |
|
928 |
|
} |