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 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.Delayed; |
18 |
> |
import java.util.concurrent.DelayQueue; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.TimeUnit; |
22 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
– |
import java.util.concurrent.*; |
23 |
|
|
24 |
|
public class DelayQueueTest extends JSR166TestCase { |
25 |
+ |
|
26 |
+ |
public static class Generic extends BlockingQueueTest { |
27 |
+ |
protected BlockingQueue emptyCollection() { |
28 |
+ |
return new DelayQueue(); |
29 |
+ |
} |
30 |
+ |
protected PDelay makeElement(int i) { |
31 |
+ |
return new PDelay(i); |
32 |
+ |
} |
33 |
+ |
} |
34 |
+ |
|
35 |
|
public static void main(String[] args) { |
36 |
|
junit.textui.TestRunner.run(suite()); |
37 |
|
} |
38 |
|
|
39 |
|
public static Test suite() { |
40 |
< |
return new TestSuite(DelayQueueTest.class); |
40 |
> |
return newTestSuite(DelayQueueTest.class, |
41 |
> |
new Generic().testSuite()); |
42 |
|
} |
43 |
|
|
44 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
70 |
|
return other.pseudodelay == pseudodelay; |
71 |
|
} |
72 |
|
|
52 |
– |
|
73 |
|
public long getDelay(TimeUnit ignore) { |
74 |
|
return pseudodelay; |
75 |
|
} |
82 |
|
} |
83 |
|
} |
84 |
|
|
65 |
– |
|
85 |
|
/** |
86 |
|
* Delayed implementation that actually delays |
87 |
|
*/ |
123 |
|
} |
124 |
|
} |
125 |
|
|
107 |
– |
|
126 |
|
/** |
127 |
|
* Create a queue of given size containing consecutive |
128 |
|
* PDelays 0 ... n. |
227 |
|
} |
228 |
|
|
229 |
|
/** |
212 |
– |
* offer(null) throws NPE |
213 |
– |
*/ |
214 |
– |
public void testOfferNull() { |
215 |
– |
try { |
216 |
– |
DelayQueue q = new DelayQueue(); |
217 |
– |
q.offer(null); |
218 |
– |
shouldThrow(); |
219 |
– |
} catch (NullPointerException success) {} |
220 |
– |
} |
221 |
– |
|
222 |
– |
/** |
223 |
– |
* add(null) throws NPE |
224 |
– |
*/ |
225 |
– |
public void testAddNull() { |
226 |
– |
try { |
227 |
– |
DelayQueue q = new DelayQueue(); |
228 |
– |
q.add(null); |
229 |
– |
shouldThrow(); |
230 |
– |
} catch (NullPointerException success) {} |
231 |
– |
} |
232 |
– |
|
233 |
– |
/** |
230 |
|
* offer non-null succeeds |
231 |
|
*/ |
232 |
|
public void testOffer() { |
247 |
|
} |
248 |
|
|
249 |
|
/** |
254 |
– |
* addAll(null) throws NPE |
255 |
– |
*/ |
256 |
– |
public void testAddAll1() { |
257 |
– |
try { |
258 |
– |
DelayQueue q = new DelayQueue(); |
259 |
– |
q.addAll(null); |
260 |
– |
shouldThrow(); |
261 |
– |
} catch (NullPointerException success) {} |
262 |
– |
} |
263 |
– |
|
264 |
– |
|
265 |
– |
/** |
250 |
|
* addAll(this) throws IAE |
251 |
|
*/ |
252 |
|
public void testAddAllSelf() { |
258 |
|
} |
259 |
|
|
260 |
|
/** |
277 |
– |
* addAll of a collection with null elements throws NPE |
278 |
– |
*/ |
279 |
– |
public void testAddAll2() { |
280 |
– |
try { |
281 |
– |
DelayQueue q = new DelayQueue(); |
282 |
– |
PDelay[] ints = new PDelay[SIZE]; |
283 |
– |
q.addAll(Arrays.asList(ints)); |
284 |
– |
shouldThrow(); |
285 |
– |
} catch (NullPointerException success) {} |
286 |
– |
} |
287 |
– |
|
288 |
– |
/** |
261 |
|
* addAll of a collection with any null elements throws NPE after |
262 |
|
* possibly adding some elements |
263 |
|
*/ |
288 |
|
} |
289 |
|
|
290 |
|
/** |
319 |
– |
* put(null) throws NPE |
320 |
– |
*/ |
321 |
– |
public void testPutNull() { |
322 |
– |
try { |
323 |
– |
DelayQueue q = new DelayQueue(); |
324 |
– |
q.put(null); |
325 |
– |
shouldThrow(); |
326 |
– |
} catch (NullPointerException success) {} |
327 |
– |
} |
328 |
– |
|
329 |
– |
/** |
291 |
|
* all elements successfully put are contained |
292 |
|
*/ |
293 |
< |
public void testPut() { |
294 |
< |
DelayQueue q = new DelayQueue(); |
295 |
< |
for (int i = 0; i < SIZE; ++i) { |
296 |
< |
PDelay I = new PDelay(i); |
297 |
< |
q.put(I); |
298 |
< |
assertTrue(q.contains(I)); |
299 |
< |
} |
300 |
< |
assertEquals(SIZE, q.size()); |
293 |
> |
public void testPut() { |
294 |
> |
DelayQueue q = new DelayQueue(); |
295 |
> |
for (int i = 0; i < SIZE; ++i) { |
296 |
> |
PDelay I = new PDelay(i); |
297 |
> |
q.put(I); |
298 |
> |
assertTrue(q.contains(I)); |
299 |
> |
} |
300 |
> |
assertEquals(SIZE, q.size()); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
305 |
|
*/ |
306 |
|
public void testPutWithTake() throws InterruptedException { |
307 |
|
final DelayQueue q = new DelayQueue(); |
308 |
< |
Thread t = new Thread(new CheckedRunnable() { |
308 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
309 |
|
public void realRun() { |
310 |
|
q.put(new PDelay(0)); |
311 |
|
q.put(new PDelay(0)); |
313 |
|
q.put(new PDelay(0)); |
314 |
|
}}); |
315 |
|
|
316 |
< |
t.start(); |
317 |
< |
Thread.sleep(SHORT_DELAY_MS); |
357 |
< |
q.take(); |
358 |
< |
t.interrupt(); |
359 |
< |
t.join(); |
316 |
> |
awaitTermination(t); |
317 |
> |
assertEquals(4, q.size()); |
318 |
|
} |
319 |
|
|
320 |
|
/** |
322 |
|
*/ |
323 |
|
public void testTimedOffer() throws InterruptedException { |
324 |
|
final DelayQueue q = new DelayQueue(); |
325 |
< |
Thread t = new Thread(new CheckedRunnable() { |
325 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
326 |
|
public void realRun() throws InterruptedException { |
327 |
|
q.put(new PDelay(0)); |
328 |
|
q.put(new PDelay(0)); |
330 |
|
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
331 |
|
}}); |
332 |
|
|
333 |
< |
t.start(); |
376 |
< |
Thread.sleep(SMALL_DELAY_MS); |
377 |
< |
t.interrupt(); |
378 |
< |
t.join(); |
333 |
> |
awaitTermination(t); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
344 |
|
} |
345 |
|
|
346 |
|
/** |
392 |
– |
* take blocks interruptibly when empty |
393 |
– |
*/ |
394 |
– |
public void testTakeFromEmptyBlocksInterruptibly() |
395 |
– |
throws InterruptedException { |
396 |
– |
final BlockingQueue q = new DelayQueue(); |
397 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
398 |
– |
Thread t = newStartedThread(new CheckedRunnable() { |
399 |
– |
public void realRun() { |
400 |
– |
long t0 = System.nanoTime(); |
401 |
– |
threadStarted.countDown(); |
402 |
– |
try { |
403 |
– |
q.take(); |
404 |
– |
shouldThrow(); |
405 |
– |
} catch (InterruptedException expected) {} |
406 |
– |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
407 |
– |
}}); |
408 |
– |
threadStarted.await(); |
409 |
– |
Thread.sleep(SHORT_DELAY_MS); |
410 |
– |
assertTrue(t.isAlive()); |
411 |
– |
t.interrupt(); |
412 |
– |
awaitTermination(t, MEDIUM_DELAY_MS); |
413 |
– |
assertFalse(t.isAlive()); |
414 |
– |
} |
415 |
– |
|
416 |
– |
/** |
347 |
|
* Take removes existing elements until empty, then blocks interruptibly |
348 |
|
*/ |
349 |
|
public void testBlockingTake() throws InterruptedException { |
350 |
|
final DelayQueue q = populatedQueue(SIZE); |
351 |
< |
Thread t = new Thread(new CheckedRunnable() { |
351 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
352 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
353 |
|
public void realRun() throws InterruptedException { |
354 |
|
for (int i = 0; i < SIZE; ++i) { |
355 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
356 |
|
} |
357 |
+ |
|
358 |
+ |
Thread.currentThread().interrupt(); |
359 |
+ |
try { |
360 |
+ |
q.take(); |
361 |
+ |
shouldThrow(); |
362 |
+ |
} catch (InterruptedException success) {} |
363 |
+ |
assertFalse(Thread.interrupted()); |
364 |
+ |
|
365 |
+ |
pleaseInterrupt.countDown(); |
366 |
|
try { |
367 |
|
q.take(); |
368 |
|
shouldThrow(); |
369 |
|
} catch (InterruptedException success) {} |
370 |
+ |
assertFalse(Thread.interrupted()); |
371 |
|
}}); |
372 |
|
|
373 |
< |
t.start(); |
374 |
< |
Thread.sleep(SHORT_DELAY_MS); |
373 |
> |
await(pleaseInterrupt); |
374 |
> |
assertThreadStaysAlive(t); |
375 |
|
t.interrupt(); |
376 |
< |
t.join(); |
376 |
> |
awaitTermination(t); |
377 |
|
} |
378 |
|
|
438 |
– |
|
379 |
|
/** |
380 |
|
* poll succeeds unless empty |
381 |
|
*/ |
404 |
|
public void testTimedPoll() throws InterruptedException { |
405 |
|
DelayQueue q = populatedQueue(SIZE); |
406 |
|
for (int i = 0; i < SIZE; ++i) { |
407 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
408 |
< |
} |
409 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
407 |
> |
long startTime = System.nanoTime(); |
408 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
409 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
410 |
> |
} |
411 |
> |
long startTime = System.nanoTime(); |
412 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
413 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
414 |
> |
checkEmpty(q); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
419 |
|
* returning timeout status |
420 |
|
*/ |
421 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
422 |
< |
Thread t = new Thread(new CheckedRunnable() { |
422 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
423 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
424 |
|
public void realRun() throws InterruptedException { |
425 |
|
DelayQueue q = populatedQueue(SIZE); |
426 |
|
for (int i = 0; i < SIZE; ++i) { |
427 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
428 |
|
} |
483 |
– |
try { |
484 |
– |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
485 |
– |
shouldThrow(); |
486 |
– |
} catch (InterruptedException success) {} |
487 |
– |
}}); |
488 |
– |
|
489 |
– |
t.start(); |
490 |
– |
Thread.sleep(SHORT_DELAY_MS); |
491 |
– |
t.interrupt(); |
492 |
– |
t.join(); |
493 |
– |
} |
494 |
– |
|
495 |
– |
/** |
496 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
497 |
– |
* on interruption throws |
498 |
– |
*/ |
499 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
500 |
– |
final DelayQueue q = new DelayQueue(); |
501 |
– |
final PDelay pdelay = new PDelay(0); |
502 |
– |
final CheckedBarrier barrier = new CheckedBarrier(2); |
503 |
– |
Thread t = new Thread(new CheckedRunnable() { |
504 |
– |
public void realRun() throws InterruptedException { |
505 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
506 |
– |
|
507 |
– |
barrier.await(); |
508 |
– |
assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
429 |
|
|
430 |
|
Thread.currentThread().interrupt(); |
431 |
|
try { |
432 |
< |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
432 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
433 |
|
shouldThrow(); |
434 |
|
} catch (InterruptedException success) {} |
435 |
+ |
assertFalse(Thread.interrupted()); |
436 |
|
|
437 |
< |
barrier.await(); |
437 |
> |
pleaseInterrupt.countDown(); |
438 |
|
try { |
439 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
439 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
440 |
|
shouldThrow(); |
441 |
|
} catch (InterruptedException success) {} |
442 |
+ |
assertFalse(Thread.interrupted()); |
443 |
|
}}); |
444 |
|
|
445 |
< |
t.start(); |
446 |
< |
barrier.await(); |
525 |
< |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
526 |
< |
barrier.await(); |
527 |
< |
sleep(SHORT_DELAY_MS); |
445 |
> |
await(pleaseInterrupt); |
446 |
> |
assertThreadStaysAlive(t); |
447 |
|
t.interrupt(); |
448 |
< |
t.join(); |
448 |
> |
awaitTermination(t); |
449 |
|
} |
450 |
|
|
532 |
– |
|
451 |
|
/** |
452 |
|
* peek returns next element, or null if empty |
453 |
|
*/ |
610 |
|
assertSame(ints[i], q.remove()); |
611 |
|
} |
612 |
|
|
695 |
– |
|
696 |
– |
/** |
697 |
– |
* toArray(null) throws NullPointerException |
698 |
– |
*/ |
699 |
– |
public void testToArray_NullArg() { |
700 |
– |
DelayQueue q = populatedQueue(SIZE); |
701 |
– |
try { |
702 |
– |
q.toArray(null); |
703 |
– |
shouldThrow(); |
704 |
– |
} catch (NullPointerException success) {} |
705 |
– |
} |
706 |
– |
|
613 |
|
/** |
614 |
|
* toArray(incompatible array type) throws ArrayStoreException |
615 |
|
*/ |
652 |
|
assertFalse(it.hasNext()); |
653 |
|
} |
654 |
|
|
749 |
– |
|
655 |
|
/** |
656 |
|
* toString contains toStrings of elements |
657 |
|
*/ |
659 |
|
DelayQueue q = populatedQueue(SIZE); |
660 |
|
String s = q.toString(); |
661 |
|
for (int i = 0; i < SIZE; ++i) { |
662 |
< |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
662 |
> |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
663 |
|
} |
664 |
|
} |
665 |
|
|
666 |
|
/** |
667 |
< |
* offer transfers elements across Executor tasks |
667 |
> |
* timed poll transfers elements across Executor tasks |
668 |
|
*/ |
669 |
|
public void testPollInExecutor() { |
670 |
|
final DelayQueue q = new DelayQueue(); |
671 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
672 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
673 |
|
executor.execute(new CheckedRunnable() { |
674 |
|
public void realRun() throws InterruptedException { |
675 |
|
assertNull(q.poll()); |
676 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
677 |
< |
assertTrue(q.isEmpty()); |
676 |
> |
threadsStarted.await(); |
677 |
> |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
678 |
> |
checkEmpty(q); |
679 |
|
}}); |
680 |
|
|
681 |
|
executor.execute(new CheckedRunnable() { |
682 |
|
public void realRun() throws InterruptedException { |
683 |
< |
Thread.sleep(SHORT_DELAY_MS); |
683 |
> |
threadsStarted.await(); |
684 |
|
q.put(new PDelay(1)); |
685 |
|
}}); |
686 |
|
|
687 |
|
joinPool(executor); |
688 |
|
} |
689 |
|
|
783 |
– |
|
690 |
|
/** |
691 |
|
* Delayed actions do not occur until their delay elapses |
692 |
|
*/ |
716 |
|
assertNotNull(q.peek()); |
717 |
|
} |
718 |
|
|
813 |
– |
|
719 |
|
/** |
720 |
|
* poll of a non-empty queue returns null if no expired elements. |
721 |
|
*/ |
731 |
|
public void testTimedPollDelayed() throws InterruptedException { |
732 |
|
DelayQueue q = new DelayQueue(); |
733 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
734 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
830 |
< |
} |
831 |
< |
|
832 |
< |
/** |
833 |
< |
* drainTo(null) throws NPE |
834 |
< |
*/ |
835 |
< |
public void testDrainToNull() { |
836 |
< |
DelayQueue q = populatedQueue(SIZE); |
837 |
< |
try { |
838 |
< |
q.drainTo(null); |
839 |
< |
shouldThrow(); |
840 |
< |
} catch (NullPointerException success) {} |
841 |
< |
} |
842 |
< |
|
843 |
< |
/** |
844 |
< |
* drainTo(this) throws IAE |
845 |
< |
*/ |
846 |
< |
public void testDrainToSelf() { |
847 |
< |
DelayQueue q = populatedQueue(SIZE); |
848 |
< |
try { |
849 |
< |
q.drainTo(q); |
850 |
< |
shouldThrow(); |
851 |
< |
} catch (IllegalArgumentException success) {} |
734 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
781 |
|
} |
782 |
|
|
783 |
|
/** |
901 |
– |
* drainTo(null, n) throws NPE |
902 |
– |
*/ |
903 |
– |
public void testDrainToNullN() { |
904 |
– |
DelayQueue q = populatedQueue(SIZE); |
905 |
– |
try { |
906 |
– |
q.drainTo(null, 0); |
907 |
– |
shouldThrow(); |
908 |
– |
} catch (NullPointerException success) {} |
909 |
– |
} |
910 |
– |
|
911 |
– |
/** |
912 |
– |
* drainTo(this, n) throws IAE |
913 |
– |
*/ |
914 |
– |
public void testDrainToSelfN() { |
915 |
– |
DelayQueue q = populatedQueue(SIZE); |
916 |
– |
try { |
917 |
– |
q.drainTo(q, 0); |
918 |
– |
shouldThrow(); |
919 |
– |
} catch (IllegalArgumentException success) {} |
920 |
– |
} |
921 |
– |
|
922 |
– |
/** |
784 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
785 |
|
*/ |
786 |
|
public void testDrainToN() { |
794 |
|
} |
795 |
|
} |
796 |
|
|
936 |
– |
|
797 |
|
} |