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; |
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 |
|
|
226 |
|
* offer(null) throws NPE |
227 |
|
*/ |
228 |
|
public void testOfferNull() { |
229 |
< |
try { |
229 |
> |
try { |
230 |
|
DelayQueue q = new DelayQueue(); |
231 |
|
q.offer(null); |
232 |
|
shouldThrow(); |
237 |
|
* add(null) throws NPE |
238 |
|
*/ |
239 |
|
public void testAddNull() { |
240 |
< |
try { |
240 |
> |
try { |
241 |
|
DelayQueue q = new DelayQueue(); |
242 |
|
q.add(null); |
243 |
|
shouldThrow(); |
339 |
|
* put(null) throws NPE |
340 |
|
*/ |
341 |
|
public void testPutNull() { |
342 |
< |
try { |
342 |
> |
try { |
343 |
|
DelayQueue q = new DelayQueue(); |
344 |
|
q.put(null); |
345 |
|
shouldThrow(); |
346 |
|
} |
347 |
< |
catch (NullPointerException success){ |
348 |
< |
} |
347 |
> |
catch (NullPointerException success) { |
348 |
> |
} |
349 |
|
} |
350 |
|
|
351 |
|
/** |
393 |
|
q.take(); |
394 |
|
t.interrupt(); |
395 |
|
t.join(); |
396 |
< |
} catch (Exception e){ |
396 |
> |
} catch (Exception e) { |
397 |
|
unexpectedException(); |
398 |
|
} |
399 |
|
} |
408 |
|
try { |
409 |
|
q.put(new PDelay(0)); |
410 |
|
q.put(new PDelay(0)); |
411 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
412 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
411 |
> |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
412 |
> |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
413 |
|
} finally { } |
414 |
|
} |
415 |
|
}); |
419 |
|
Thread.sleep(SMALL_DELAY_MS); |
420 |
|
t.interrupt(); |
421 |
|
t.join(); |
422 |
< |
} catch (Exception e){ |
422 |
> |
} catch (Exception e) { |
423 |
|
unexpectedException(); |
424 |
|
} |
425 |
|
} |
428 |
|
* take retrieves elements in priority order |
429 |
|
*/ |
430 |
|
public void testTake() { |
431 |
< |
try { |
431 |
> |
try { |
432 |
|
DelayQueue q = populatedQueue(SIZE); |
433 |
|
for (int i = 0; i < SIZE; ++i) { |
434 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
435 |
|
} |
436 |
< |
} catch (InterruptedException e){ |
437 |
< |
unexpectedException(); |
438 |
< |
} |
436 |
> |
} catch (InterruptedException e) { |
437 |
> |
unexpectedException(); |
438 |
> |
} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
447 |
|
public void run() { |
448 |
|
try { |
449 |
|
q.take(); |
450 |
< |
threadShouldThrow(); |
451 |
< |
} catch (InterruptedException success){ } |
450 |
> |
threadShouldThrow(); |
451 |
> |
} catch (InterruptedException success) { } |
452 |
|
} |
453 |
|
}); |
454 |
|
try { |
456 |
|
Thread.sleep(SHORT_DELAY_MS); |
457 |
|
t.interrupt(); |
458 |
|
t.join(); |
459 |
< |
} catch (Exception e){ |
459 |
> |
} catch (Exception e) { |
460 |
|
unexpectedException(); |
461 |
|
} |
462 |
|
} |
474 |
|
} |
475 |
|
q.take(); |
476 |
|
threadShouldThrow(); |
477 |
< |
} catch (InterruptedException success){ |
477 |
> |
} catch (InterruptedException success) { |
478 |
|
} |
479 |
|
}}); |
480 |
|
t.start(); |
484 |
|
t.join(); |
485 |
|
} |
486 |
|
catch (InterruptedException ie) { |
487 |
< |
unexpectedException(); |
487 |
> |
unexpectedException(); |
488 |
|
} |
489 |
|
} |
490 |
|
|
497 |
|
for (int i = 0; i < SIZE; ++i) { |
498 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
499 |
|
} |
500 |
< |
assertNull(q.poll()); |
500 |
> |
assertNull(q.poll()); |
501 |
|
} |
502 |
|
|
503 |
|
/** |
507 |
|
try { |
508 |
|
DelayQueue q = populatedQueue(SIZE); |
509 |
|
for (int i = 0; i < SIZE; ++i) { |
510 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
510 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
511 |
|
} |
512 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
513 |
< |
} catch (InterruptedException e){ |
514 |
< |
unexpectedException(); |
515 |
< |
} |
512 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
513 |
> |
} catch (InterruptedException e) { |
514 |
> |
unexpectedException(); |
515 |
> |
} |
516 |
|
} |
517 |
|
|
518 |
|
/** |
522 |
|
try { |
523 |
|
DelayQueue q = populatedQueue(SIZE); |
524 |
|
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
525 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
526 |
|
} |
527 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
528 |
< |
} catch (InterruptedException e){ |
529 |
< |
unexpectedException(); |
530 |
< |
} |
527 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
528 |
> |
} catch (InterruptedException e) { |
529 |
> |
unexpectedException(); |
530 |
> |
} |
531 |
|
} |
532 |
|
|
533 |
|
/** |
540 |
|
try { |
541 |
|
DelayQueue q = populatedQueue(SIZE); |
542 |
|
for (int i = 0; i < SIZE; ++i) { |
543 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
543 |
> |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
544 |
|
} |
545 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
546 |
< |
} catch (InterruptedException success){ |
545 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
546 |
> |
} catch (InterruptedException success) { |
547 |
|
} |
548 |
|
}}); |
549 |
|
t.start(); |
553 |
|
t.join(); |
554 |
|
} |
555 |
|
catch (InterruptedException ie) { |
556 |
< |
unexpectedException(); |
556 |
> |
unexpectedException(); |
557 |
|
} |
558 |
|
} |
559 |
|
|
566 |
|
Thread t = new Thread(new Runnable() { |
567 |
|
public void run() { |
568 |
|
try { |
569 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
570 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
571 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
572 |
< |
threadFail("Should block"); |
569 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
570 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
571 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
572 |
> |
threadFail("Should block"); |
573 |
|
} catch (InterruptedException success) { } |
574 |
|
} |
575 |
|
}); |
576 |
|
try { |
577 |
|
t.start(); |
578 |
|
Thread.sleep(SMALL_DELAY_MS); |
579 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
579 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
580 |
|
t.interrupt(); |
581 |
|
t.join(); |
582 |
< |
} catch (Exception e){ |
582 |
> |
} catch (Exception e) { |
583 |
|
unexpectedException(); |
584 |
|
} |
585 |
|
} |
598 |
|
else |
599 |
|
assertTrue(i != ((PDelay)q.peek()).intValue()); |
600 |
|
} |
601 |
< |
assertNull(q.peek()); |
601 |
> |
assertNull(q.peek()); |
602 |
|
} |
603 |
|
|
604 |
|
/** |
628 |
|
try { |
629 |
|
q.remove(); |
630 |
|
shouldThrow(); |
631 |
< |
} catch (NoSuchElementException success){ |
632 |
< |
} |
631 |
> |
} catch (NoSuchElementException success) { |
632 |
> |
} |
633 |
|
} |
634 |
|
|
635 |
|
/** |
730 |
|
*/ |
731 |
|
public void testToArray() { |
732 |
|
DelayQueue q = populatedQueue(SIZE); |
733 |
< |
Object[] o = q.toArray(); |
733 |
> |
Object[] o = q.toArray(); |
734 |
|
Arrays.sort(o); |
735 |
< |
try { |
736 |
< |
for (int i = 0; i < o.length; i++) |
737 |
< |
assertEquals(o[i], q.take()); |
738 |
< |
} catch (InterruptedException e){ |
739 |
< |
unexpectedException(); |
740 |
< |
} |
735 |
> |
try { |
736 |
> |
for (int i = 0; i < o.length; i++) |
737 |
> |
assertEquals(o[i], q.take()); |
738 |
> |
} catch (InterruptedException e) { |
739 |
> |
unexpectedException(); |
740 |
> |
} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
745 |
|
*/ |
746 |
|
public void testToArray2() { |
747 |
|
DelayQueue q = populatedQueue(SIZE); |
748 |
< |
PDelay[] ints = new PDelay[SIZE]; |
749 |
< |
ints = (PDelay[])q.toArray(ints); |
748 |
> |
PDelay[] ints = new PDelay[SIZE]; |
749 |
> |
ints = (PDelay[])q.toArray(ints); |
750 |
|
Arrays.sort(ints); |
751 |
< |
try { |
752 |
< |
for (int i = 0; i < ints.length; i++) |
753 |
< |
assertEquals(ints[i], q.take()); |
754 |
< |
} catch (InterruptedException e){ |
755 |
< |
unexpectedException(); |
756 |
< |
} |
751 |
> |
try { |
752 |
> |
for (int i = 0; i < ints.length; i++) |
753 |
> |
assertEquals(ints[i], q.take()); |
754 |
> |
} catch (InterruptedException e) { |
755 |
> |
unexpectedException(); |
756 |
> |
} |
757 |
|
} |
758 |
|
|
759 |
|
|
761 |
|
* toArray(null) throws NPE |
762 |
|
*/ |
763 |
|
public void testToArray_BadArg() { |
764 |
< |
try { |
764 |
> |
try { |
765 |
|
DelayQueue q = populatedQueue(SIZE); |
766 |
< |
Object o[] = q.toArray(null); |
767 |
< |
shouldThrow(); |
768 |
< |
} catch (NullPointerException success){} |
766 |
> |
Object o[] = q.toArray(null); |
767 |
> |
shouldThrow(); |
768 |
> |
} catch (NullPointerException success) {} |
769 |
|
} |
770 |
|
|
771 |
|
/** |
772 |
|
* toArray with incompatible array type throws CCE |
773 |
|
*/ |
774 |
|
public void testToArray1_BadArg() { |
775 |
< |
try { |
775 |
> |
try { |
776 |
|
DelayQueue q = populatedQueue(SIZE); |
777 |
< |
Object o[] = q.toArray(new String[10] ); |
778 |
< |
shouldThrow(); |
779 |
< |
} catch (ArrayStoreException success){} |
777 |
> |
Object o[] = q.toArray(new String[10] ); |
778 |
> |
shouldThrow(); |
779 |
> |
} catch (ArrayStoreException success) {} |
780 |
|
} |
781 |
|
|
782 |
|
/** |
785 |
|
public void testIterator() { |
786 |
|
DelayQueue q = populatedQueue(SIZE); |
787 |
|
int i = 0; |
788 |
< |
Iterator it = q.iterator(); |
788 |
> |
Iterator it = q.iterator(); |
789 |
|
while (it.hasNext()) { |
790 |
|
assertTrue(q.contains(it.next())); |
791 |
|
++i; |
832 |
|
public void run() { |
833 |
|
threadAssertNull(q.poll()); |
834 |
|
try { |
835 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
835 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
836 |
|
threadAssertTrue(q.isEmpty()); |
837 |
|
} |
838 |
|
catch (InterruptedException e) { |
912 |
|
DelayQueue q = new DelayQueue(); |
913 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
914 |
|
try { |
915 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
915 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
916 |
|
} catch (Exception ex) { |
917 |
|
unexpectedException(); |
918 |
|
} |
987 |
|
assertTrue(l.size() >= SIZE); |
988 |
|
t.join(); |
989 |
|
assertTrue(q.size() + l.size() >= SIZE); |
990 |
< |
} catch (Exception e){ |
990 |
> |
} catch (Exception e) { |
991 |
|
unexpectedException(); |
992 |
|
} |
993 |
|
} |