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 |
|
*/ |
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() { |
24 |
|
|
25 |
|
/** |
26 |
|
* A delayed implementation for testing. |
27 |
< |
* Most tests use Pseudodelays, where delays are all elapsed |
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 { |
32 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
33 |
|
public int compareTo(PDelay y) { |
34 |
|
int i = pseudodelay; |
35 |
< |
int j = ((PDelay)y).pseudodelay; |
35 |
> |
int j = y.pseudodelay; |
36 |
|
if (i < j) return -1; |
37 |
|
if (i > j) return 1; |
38 |
|
return 0; |
39 |
|
} |
40 |
|
|
41 |
|
public int compareTo(Delayed y) { |
42 |
< |
int i = pseudodelay; |
43 |
< |
int j = ((PDelay)y).pseudodelay; |
44 |
< |
if (i < j) return -1; |
45 |
< |
if (i > j) return 1; |
46 |
< |
return 0; |
42 |
> |
return compareTo((PDelay)y); |
43 |
|
} |
44 |
|
|
45 |
|
public boolean equals(Object other) { |
46 |
< |
return ((PDelay)other).pseudodelay == pseudodelay; |
46 |
> |
return equals((PDelay)other); |
47 |
|
} |
48 |
|
public boolean equals(PDelay other) { |
49 |
< |
return ((PDelay)other).pseudodelay == pseudodelay; |
49 |
> |
return other.pseudodelay == pseudodelay; |
50 |
|
} |
51 |
|
|
56 |
– |
|
52 |
|
public long getDelay(TimeUnit ignore) { |
53 |
|
return pseudodelay; |
54 |
|
} |
61 |
|
} |
62 |
|
} |
63 |
|
|
69 |
– |
|
64 |
|
/** |
65 |
|
* Delayed implementation that actually delays |
66 |
|
*/ |
71 |
|
} |
72 |
|
public int compareTo(NanoDelay y) { |
73 |
|
long i = trigger; |
74 |
< |
long j = ((NanoDelay)y).trigger; |
74 |
> |
long j = y.trigger; |
75 |
|
if (i < j) return -1; |
76 |
|
if (i > j) return 1; |
77 |
|
return 0; |
78 |
|
} |
79 |
|
|
80 |
|
public int compareTo(Delayed y) { |
81 |
< |
long i = trigger; |
88 |
< |
long j = ((NanoDelay)y).trigger; |
89 |
< |
if (i < j) return -1; |
90 |
< |
if (i > j) return 1; |
91 |
< |
return 0; |
81 |
> |
return compareTo((NanoDelay)y); |
82 |
|
} |
83 |
|
|
84 |
|
public boolean equals(Object other) { |
85 |
< |
return ((NanoDelay)other).trigger == trigger; |
85 |
> |
return equals((NanoDelay)other); |
86 |
|
} |
87 |
|
public boolean equals(NanoDelay other) { |
88 |
< |
return ((NanoDelay)other).trigger == trigger; |
88 |
> |
return other.trigger == trigger; |
89 |
|
} |
90 |
|
|
91 |
|
public long getDelay(TimeUnit unit) { |
102 |
|
} |
103 |
|
} |
104 |
|
|
115 |
– |
|
105 |
|
/** |
106 |
|
* Create a queue of given size containing consecutive |
107 |
|
* PDelays 0 ... n. |
108 |
|
*/ |
109 |
< |
private DelayQueue populatedQueue(int n) { |
110 |
< |
DelayQueue q = new DelayQueue(); |
109 |
> |
private DelayQueue<PDelay> populatedQueue(int n) { |
110 |
> |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
111 |
|
assertTrue(q.isEmpty()); |
112 |
|
for (int i = n-1; i >= 0; i-=2) |
113 |
|
assertTrue(q.offer(new PDelay(i))); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* remainingCapacity does not change when elementa added or removed, |
191 |
> |
* remainingCapacity does not change when elements added or removed, |
192 |
|
* but size does |
193 |
|
*/ |
194 |
|
public void testRemainingCapacity() { |
258 |
|
} catch (NullPointerException success) {} |
259 |
|
} |
260 |
|
|
272 |
– |
|
261 |
|
/** |
262 |
|
* addAll(this) throws IAE |
263 |
|
*/ |
280 |
|
shouldThrow(); |
281 |
|
} catch (NullPointerException success) {} |
282 |
|
} |
283 |
+ |
|
284 |
|
/** |
285 |
|
* addAll of a collection with any null elements throws NPE after |
286 |
|
* possibly adding some elements |
314 |
|
/** |
315 |
|
* put(null) throws NPE |
316 |
|
*/ |
317 |
< |
public void testPutNull() { |
317 |
> |
public void testPutNull() { |
318 |
|
try { |
319 |
|
DelayQueue q = new DelayQueue(); |
320 |
|
q.put(null); |
321 |
|
shouldThrow(); |
322 |
|
} catch (NullPointerException success) {} |
323 |
< |
} |
323 |
> |
} |
324 |
|
|
325 |
|
/** |
326 |
|
* all elements successfully put are contained |
327 |
|
*/ |
328 |
< |
public void testPut() { |
329 |
< |
DelayQueue q = new DelayQueue(); |
330 |
< |
for (int i = 0; i < SIZE; ++i) { |
331 |
< |
PDelay I = new PDelay(i); |
332 |
< |
q.put(I); |
333 |
< |
assertTrue(q.contains(I)); |
334 |
< |
} |
335 |
< |
assertEquals(SIZE, q.size()); |
328 |
> |
public void testPut() { |
329 |
> |
DelayQueue q = new DelayQueue(); |
330 |
> |
for (int i = 0; i < SIZE; ++i) { |
331 |
> |
PDelay I = new PDelay(i); |
332 |
> |
q.put(I); |
333 |
> |
assertTrue(q.contains(I)); |
334 |
> |
} |
335 |
> |
assertEquals(SIZE, q.size()); |
336 |
|
} |
337 |
|
|
338 |
|
/** |
340 |
|
*/ |
341 |
|
public void testPutWithTake() throws InterruptedException { |
342 |
|
final DelayQueue q = new DelayQueue(); |
343 |
< |
Thread t = new Thread(new CheckedRunnable() { |
343 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
|
public void realRun() { |
345 |
|
q.put(new PDelay(0)); |
346 |
|
q.put(new PDelay(0)); |
348 |
|
q.put(new PDelay(0)); |
349 |
|
}}); |
350 |
|
|
351 |
< |
t.start(); |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
< |
q.take(); |
365 |
< |
t.interrupt(); |
366 |
< |
t.join(); |
351 |
> |
awaitTermination(t); |
352 |
> |
assertEquals(4, q.size()); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
357 |
|
*/ |
358 |
|
public void testTimedOffer() throws InterruptedException { |
359 |
|
final DelayQueue q = new DelayQueue(); |
360 |
< |
Thread t = new Thread(new CheckedRunnable() { |
360 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
361 |
|
public void realRun() throws InterruptedException { |
362 |
|
q.put(new PDelay(0)); |
363 |
|
q.put(new PDelay(0)); |
364 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
365 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
364 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
365 |
> |
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
366 |
|
}}); |
367 |
|
|
368 |
< |
t.start(); |
383 |
< |
Thread.sleep(SMALL_DELAY_MS); |
384 |
< |
t.interrupt(); |
385 |
< |
t.join(); |
368 |
> |
awaitTermination(t); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
381 |
|
/** |
382 |
|
* take blocks interruptibly when empty |
383 |
|
*/ |
384 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
385 |
< |
final DelayQueue q = new DelayQueue(); |
386 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
387 |
< |
public void realRun() throws InterruptedException { |
388 |
< |
q.take(); |
389 |
< |
}}; |
384 |
> |
public void testTakeFromEmptyBlocksInterruptibly() |
385 |
> |
throws InterruptedException { |
386 |
> |
final BlockingQueue q = new DelayQueue(); |
387 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
389 |
> |
public void realRun() { |
390 |
> |
threadStarted.countDown(); |
391 |
> |
try { |
392 |
> |
q.take(); |
393 |
> |
shouldThrow(); |
394 |
> |
} catch (InterruptedException success) {} |
395 |
> |
assertFalse(Thread.interrupted()); |
396 |
> |
}}); |
397 |
|
|
398 |
< |
t.start(); |
399 |
< |
Thread.sleep(SHORT_DELAY_MS); |
398 |
> |
await(threadStarted); |
399 |
> |
assertThreadStaysAlive(t); |
400 |
|
t.interrupt(); |
401 |
< |
t.join(); |
401 |
> |
awaitTermination(t); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* Take removes existing elements until empty, then blocks interruptibly |
406 |
|
*/ |
407 |
|
public void testBlockingTake() throws InterruptedException { |
408 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
408 |
> |
final DelayQueue q = populatedQueue(SIZE); |
409 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
410 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
411 |
|
public void realRun() throws InterruptedException { |
420 |
– |
DelayQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
413 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
414 |
|
} |
424 |
– |
q.take(); |
425 |
– |
}}; |
415 |
|
|
416 |
< |
t.start(); |
417 |
< |
Thread.sleep(SHORT_DELAY_MS); |
416 |
> |
Thread.currentThread().interrupt(); |
417 |
> |
try { |
418 |
> |
q.take(); |
419 |
> |
shouldThrow(); |
420 |
> |
} catch (InterruptedException success) {} |
421 |
> |
assertFalse(Thread.interrupted()); |
422 |
> |
|
423 |
> |
pleaseInterrupt.countDown(); |
424 |
> |
try { |
425 |
> |
q.take(); |
426 |
> |
shouldThrow(); |
427 |
> |
} catch (InterruptedException success) {} |
428 |
> |
assertFalse(Thread.interrupted()); |
429 |
> |
}}); |
430 |
> |
|
431 |
> |
await(pleaseInterrupt); |
432 |
> |
assertThreadStaysAlive(t); |
433 |
|
t.interrupt(); |
434 |
< |
t.join(); |
434 |
> |
awaitTermination(t); |
435 |
|
} |
436 |
|
|
433 |
– |
|
437 |
|
/** |
438 |
|
* poll succeeds unless empty |
439 |
|
*/ |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
449 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
450 |
|
*/ |
451 |
|
public void testTimedPoll0() throws InterruptedException { |
452 |
|
DelayQueue q = populatedQueue(SIZE); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
460 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
460 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
461 |
|
*/ |
462 |
|
public void testTimedPoll() throws InterruptedException { |
463 |
|
DelayQueue q = populatedQueue(SIZE); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
465 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
466 |
< |
} |
467 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
465 |
> |
long startTime = System.nanoTime(); |
466 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
467 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
468 |
> |
} |
469 |
> |
long startTime = System.nanoTime(); |
470 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
471 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
472 |
> |
checkEmpty(q); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
477 |
|
* returning timeout status |
478 |
|
*/ |
479 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
480 |
< |
Thread t = new Thread(new CheckedRunnable() { |
480 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
481 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
482 |
|
public void realRun() throws InterruptedException { |
483 |
|
DelayQueue q = populatedQueue(SIZE); |
484 |
|
for (int i = 0; i < SIZE; ++i) { |
485 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
486 |
|
} |
487 |
+ |
|
488 |
+ |
Thread.currentThread().interrupt(); |
489 |
|
try { |
490 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
490 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
491 |
> |
shouldThrow(); |
492 |
> |
} catch (InterruptedException success) {} |
493 |
> |
assertFalse(Thread.interrupted()); |
494 |
> |
|
495 |
> |
pleaseInterrupt.countDown(); |
496 |
> |
try { |
497 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
498 |
|
shouldThrow(); |
499 |
|
} catch (InterruptedException success) {} |
500 |
+ |
assertFalse(Thread.interrupted()); |
501 |
|
}}); |
502 |
|
|
503 |
< |
t.start(); |
504 |
< |
Thread.sleep(SHORT_DELAY_MS); |
503 |
> |
await(pleaseInterrupt); |
504 |
> |
assertThreadStaysAlive(t); |
505 |
|
t.interrupt(); |
506 |
< |
t.join(); |
506 |
> |
awaitTermination(t); |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
511 |
< |
* on interruption throws |
510 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
511 |
> |
* on interruption throws |
512 |
|
*/ |
513 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
514 |
|
final DelayQueue q = new DelayQueue(); |
515 |
< |
Thread t = new Thread(new CheckedRunnable() { |
515 |
> |
final PDelay pdelay = new PDelay(0); |
516 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
517 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
518 |
|
public void realRun() throws InterruptedException { |
519 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
520 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
519 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
520 |
> |
|
521 |
> |
barrier.await(); |
522 |
> |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
523 |
> |
|
524 |
> |
Thread.currentThread().interrupt(); |
525 |
|
try { |
526 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
527 |
|
shouldThrow(); |
528 |
|
} catch (InterruptedException success) {} |
529 |
+ |
assertFalse(Thread.interrupted()); |
530 |
+ |
|
531 |
+ |
barrier.await(); |
532 |
+ |
try { |
533 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
534 |
+ |
shouldThrow(); |
535 |
+ |
} catch (InterruptedException success) {} |
536 |
+ |
assertFalse(Thread.interrupted()); |
537 |
|
}}); |
538 |
|
|
539 |
< |
t.start(); |
540 |
< |
Thread.sleep(SMALL_DELAY_MS); |
541 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
539 |
> |
barrier.await(); |
540 |
> |
long startTime = System.nanoTime(); |
541 |
> |
assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); |
542 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
543 |
> |
|
544 |
> |
barrier.await(); |
545 |
> |
assertThreadStaysAlive(t); |
546 |
|
t.interrupt(); |
547 |
< |
t.join(); |
547 |
> |
awaitTermination(t); |
548 |
|
} |
549 |
|
|
513 |
– |
|
550 |
|
/** |
551 |
|
* peek returns next element, or null if empty |
552 |
|
*/ |
554 |
|
DelayQueue q = populatedQueue(SIZE); |
555 |
|
for (int i = 0; i < SIZE; ++i) { |
556 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
557 |
< |
q.poll(); |
557 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
558 |
|
if (q.isEmpty()) |
559 |
|
assertNull(q.peek()); |
560 |
|
else |
561 |
< |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
561 |
> |
assertFalse(new PDelay(i).equals(q.peek())); |
562 |
|
} |
563 |
|
assertNull(q.peek()); |
564 |
|
} |
693 |
|
Object[] o = q.toArray(); |
694 |
|
Arrays.sort(o); |
695 |
|
for (int i = 0; i < o.length; i++) |
696 |
< |
assertEquals(o[i], q.take()); |
696 |
> |
assertSame(o[i], q.take()); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
|
* toArray(a) contains all elements |
701 |
|
*/ |
702 |
< |
public void testToArray2() throws InterruptedException { |
703 |
< |
DelayQueue q = populatedQueue(SIZE); |
702 |
> |
public void testToArray2() { |
703 |
> |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
704 |
|
PDelay[] ints = new PDelay[SIZE]; |
705 |
< |
ints = (PDelay[])q.toArray(ints); |
705 |
> |
PDelay[] array = q.toArray(ints); |
706 |
> |
assertSame(ints, array); |
707 |
|
Arrays.sort(ints); |
708 |
|
for (int i = 0; i < ints.length; i++) |
709 |
< |
assertEquals(ints[i], q.take()); |
709 |
> |
assertSame(ints[i], q.remove()); |
710 |
|
} |
711 |
|
|
675 |
– |
|
712 |
|
/** |
713 |
< |
* toArray(null) throws NPE |
713 |
> |
* toArray(null) throws NullPointerException |
714 |
|
*/ |
715 |
< |
public void testToArray_BadArg() { |
715 |
> |
public void testToArray_NullArg() { |
716 |
> |
DelayQueue q = populatedQueue(SIZE); |
717 |
|
try { |
718 |
< |
DelayQueue q = populatedQueue(SIZE); |
682 |
< |
Object o[] = q.toArray(null); |
718 |
> |
q.toArray(null); |
719 |
|
shouldThrow(); |
720 |
|
} catch (NullPointerException success) {} |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* toArray with incompatible array type throws CCE |
724 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
725 |
|
*/ |
726 |
|
public void testToArray1_BadArg() { |
727 |
+ |
DelayQueue q = populatedQueue(SIZE); |
728 |
|
try { |
729 |
< |
DelayQueue q = populatedQueue(SIZE); |
693 |
< |
Object o[] = q.toArray(new String[10] ); |
729 |
> |
q.toArray(new String[10]); |
730 |
|
shouldThrow(); |
731 |
|
} catch (ArrayStoreException success) {} |
732 |
|
} |
748 |
|
/** |
749 |
|
* iterator.remove removes current element |
750 |
|
*/ |
751 |
< |
public void testIteratorRemove () { |
751 |
> |
public void testIteratorRemove() { |
752 |
|
final DelayQueue q = new DelayQueue(); |
753 |
|
q.add(new PDelay(2)); |
754 |
|
q.add(new PDelay(1)); |
762 |
|
assertFalse(it.hasNext()); |
763 |
|
} |
764 |
|
|
729 |
– |
|
765 |
|
/** |
766 |
|
* toString contains toStrings of elements |
767 |
|
*/ |
769 |
|
DelayQueue q = populatedQueue(SIZE); |
770 |
|
String s = q.toString(); |
771 |
|
for (int i = 0; i < SIZE; ++i) { |
772 |
< |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
772 |
> |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
773 |
|
} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
< |
* offer transfers elements across Executor tasks |
777 |
> |
* timed poll transfers elements across Executor tasks |
778 |
|
*/ |
779 |
|
public void testPollInExecutor() { |
780 |
|
final DelayQueue q = new DelayQueue(); |
781 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
|
executor.execute(new CheckedRunnable() { |
784 |
|
public void realRun() throws InterruptedException { |
785 |
< |
threadAssertNull(q.poll()); |
786 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
< |
threadAssertTrue(q.isEmpty()); |
785 |
> |
assertNull(q.poll()); |
786 |
> |
threadsStarted.await(); |
787 |
> |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
788 |
> |
checkEmpty(q); |
789 |
|
}}); |
790 |
|
|
791 |
|
executor.execute(new CheckedRunnable() { |
792 |
|
public void realRun() throws InterruptedException { |
793 |
< |
Thread.sleep(SHORT_DELAY_MS); |
793 |
> |
threadsStarted.await(); |
794 |
|
q.put(new PDelay(1)); |
795 |
|
}}); |
796 |
|
|
797 |
|
joinPool(executor); |
761 |
– |
|
798 |
|
} |
799 |
|
|
764 |
– |
|
800 |
|
/** |
801 |
|
* Delayed actions do not occur until their delay elapses |
802 |
|
*/ |
803 |
|
public void testDelay() throws InterruptedException { |
804 |
< |
DelayQueue q = new DelayQueue(); |
805 |
< |
NanoDelay[] elements = new NanoDelay[SIZE]; |
806 |
< |
for (int i = 0; i < SIZE; ++i) { |
772 |
< |
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
773 |
< |
} |
774 |
< |
for (int i = 0; i < SIZE; ++i) { |
775 |
< |
q.add(elements[i]); |
776 |
< |
} |
804 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
805 |
> |
for (int i = 0; i < SIZE; ++i) |
806 |
> |
q.add(new NanoDelay(1000000L * (SIZE - i))); |
807 |
|
|
808 |
|
long last = 0; |
809 |
|
for (int i = 0; i < SIZE; ++i) { |
810 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
810 |
> |
NanoDelay e = q.take(); |
811 |
|
long tt = e.getTriggerTime(); |
812 |
< |
assertTrue(tt <= System.nanoTime()); |
812 |
> |
assertTrue(System.nanoTime() - tt >= 0); |
813 |
|
if (i != 0) |
814 |
|
assertTrue(tt >= last); |
815 |
|
last = tt; |
816 |
|
} |
817 |
+ |
assertTrue(q.isEmpty()); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
823 |
|
public void testPeekDelayed() { |
824 |
|
DelayQueue q = new DelayQueue(); |
825 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
826 |
< |
assert(q.peek() != null); |
826 |
> |
assertNotNull(q.peek()); |
827 |
|
} |
828 |
|
|
798 |
– |
|
829 |
|
/** |
830 |
|
* poll of a non-empty queue returns null if no expired elements. |
831 |
|
*/ |
841 |
|
public void testTimedPollDelayed() throws InterruptedException { |
842 |
|
DelayQueue q = new DelayQueue(); |
843 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
844 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
844 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
935 |
|
} |
936 |
|
|
937 |
|
/** |
938 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
938 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
939 |
|
*/ |
940 |
|
public void testDrainToN() { |
941 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
942 |
|
DelayQueue q = populatedQueue(SIZE); |
943 |
|
ArrayList l = new ArrayList(); |
944 |
|
q.drainTo(l, i); |
945 |
< |
int k = (i < SIZE)? i : SIZE; |
945 |
> |
int k = (i < SIZE) ? i : SIZE; |
946 |
|
assertEquals(q.size(), SIZE-k); |
947 |
|
assertEquals(l.size(), k); |
948 |
|
} |
949 |
|
} |
950 |
|
|
921 |
– |
|
951 |
|
} |