6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.Arrays; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Arrays; |
13 |
+ |
import java.util.Collection; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.concurrent.BlockingQueue; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.TimeUnit; |
23 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
23 |
> |
|
24 |
> |
import junit.framework.Test; |
25 |
|
|
26 |
|
public class DelayQueueTest extends JSR166TestCase { |
27 |
|
|
52 |
|
*/ |
53 |
|
static class PDelay implements Delayed { |
54 |
|
int pseudodelay; |
55 |
< |
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
56 |
< |
public int compareTo(PDelay y) { |
57 |
< |
int i = pseudodelay; |
58 |
< |
int j = y.pseudodelay; |
59 |
< |
if (i < j) return -1; |
57 |
< |
if (i > j) return 1; |
58 |
< |
return 0; |
55 |
> |
PDelay(int i) { pseudodelay = i; } |
56 |
> |
public int compareTo(PDelay other) { |
57 |
> |
int a = this.pseudodelay; |
58 |
> |
int b = other.pseudodelay; |
59 |
> |
return (a < b) ? -1 : (a > b) ? 1 : 0; |
60 |
|
} |
60 |
– |
|
61 |
|
public int compareTo(Delayed y) { |
62 |
|
return compareTo((PDelay)y); |
63 |
|
} |
64 |
– |
|
64 |
|
public boolean equals(Object other) { |
65 |
< |
return equals((PDelay)other); |
66 |
< |
} |
68 |
< |
public boolean equals(PDelay other) { |
69 |
< |
return other.pseudodelay == pseudodelay; |
65 |
> |
return (other instanceof PDelay) && |
66 |
> |
this.pseudodelay == ((PDelay)other).pseudodelay; |
67 |
|
} |
68 |
< |
|
68 |
> |
// suppress [overrides] javac warning |
69 |
> |
public int hashCode() { return pseudodelay; } |
70 |
|
public long getDelay(TimeUnit ignore) { |
71 |
< |
return pseudodelay; |
74 |
< |
} |
75 |
< |
public int intValue() { |
76 |
< |
return pseudodelay; |
71 |
> |
return Integer.MIN_VALUE + pseudodelay; |
72 |
|
} |
78 |
– |
|
73 |
|
public String toString() { |
74 |
|
return String.valueOf(pseudodelay); |
75 |
|
} |
102 |
|
return other.trigger == trigger; |
103 |
|
} |
104 |
|
|
105 |
+ |
// suppress [overrides] javac warning |
106 |
+ |
public int hashCode() { return (int) trigger; } |
107 |
+ |
|
108 |
|
public long getDelay(TimeUnit unit) { |
109 |
|
long n = trigger - System.nanoTime(); |
110 |
|
return unit.convert(n, TimeUnit.NANOSECONDS); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* Create a queue of given size containing consecutive |
123 |
> |
* Returns a new queue of given size containing consecutive |
124 |
|
* PDelays 0 ... n. |
125 |
|
*/ |
126 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
490 |
|
} |
491 |
|
|
492 |
|
/** |
496 |
– |
* remove(x) removes x and returns true if present |
497 |
– |
*/ |
498 |
– |
public void testRemoveElement() { |
499 |
– |
DelayQueue q = populatedQueue(SIZE); |
500 |
– |
for (int i = 1; i < SIZE; i+=2) { |
501 |
– |
assertTrue(q.remove(new PDelay(i))); |
502 |
– |
} |
503 |
– |
for (int i = 0; i < SIZE; i+=2) { |
504 |
– |
assertTrue(q.remove(new PDelay(i))); |
505 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
506 |
– |
} |
507 |
– |
assertTrue(q.isEmpty()); |
508 |
– |
} |
509 |
– |
|
510 |
– |
/** |
493 |
|
* contains(x) reports true when elements added but not yet removed |
494 |
|
*/ |
495 |
|
public void testContains() { |
628 |
|
it.next(); |
629 |
|
it.remove(); |
630 |
|
it = q.iterator(); |
631 |
< |
assertEquals(it.next(), new PDelay(2)); |
632 |
< |
assertEquals(it.next(), new PDelay(3)); |
631 |
> |
assertEquals(new PDelay(2), it.next()); |
632 |
> |
assertEquals(new PDelay(3), it.next()); |
633 |
|
assertFalse(it.hasNext()); |
634 |
|
} |
635 |
|
|
639 |
|
public void testToString() { |
640 |
|
DelayQueue q = populatedQueue(SIZE); |
641 |
|
String s = q.toString(); |
642 |
< |
for (int i = 0; i < SIZE; ++i) { |
643 |
< |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
662 |
< |
} |
642 |
> |
for (Object e : q) |
643 |
> |
assertTrue(s.contains(e.toString())); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
654 |
|
public void realRun() throws InterruptedException { |
655 |
|
assertNull(q.poll()); |
656 |
|
threadsStarted.await(); |
657 |
< |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
657 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
658 |
|
checkEmpty(q); |
659 |
|
}}); |
660 |
|
|
726 |
|
} |
727 |
|
ArrayList l = new ArrayList(); |
728 |
|
q.drainTo(l); |
729 |
< |
assertEquals(q.size(), 0); |
729 |
> |
assertEquals(0, q.size()); |
730 |
|
for (int i = 0; i < SIZE; ++i) |
731 |
< |
assertEquals(l.get(i), elems[i]); |
731 |
> |
assertEquals(elems[i], l.get(i)); |
732 |
|
q.add(elems[0]); |
733 |
|
q.add(elems[1]); |
734 |
|
assertFalse(q.isEmpty()); |
736 |
|
assertTrue(q.contains(elems[1])); |
737 |
|
l.clear(); |
738 |
|
q.drainTo(l); |
739 |
< |
assertEquals(q.size(), 0); |
740 |
< |
assertEquals(l.size(), 2); |
739 |
> |
assertEquals(0, q.size()); |
740 |
> |
assertEquals(2, l.size()); |
741 |
|
for (int i = 0; i < 2; ++i) |
742 |
< |
assertEquals(l.get(i), elems[i]); |
742 |
> |
assertEquals(elems[i], l.get(i)); |
743 |
|
} |
744 |
|
|
745 |
|
/** |
769 |
|
ArrayList l = new ArrayList(); |
770 |
|
q.drainTo(l, i); |
771 |
|
int k = (i < SIZE) ? i : SIZE; |
772 |
< |
assertEquals(q.size(), SIZE-k); |
773 |
< |
assertEquals(l.size(), k); |
772 |
> |
assertEquals(SIZE-k, q.size()); |
773 |
> |
assertEquals(k, l.size()); |
774 |
|
} |
775 |
|
} |
776 |
|
|
777 |
+ |
/** |
778 |
+ |
* remove(null), contains(null) always return false |
779 |
+ |
*/ |
780 |
+ |
public void testNeverContainsNull() { |
781 |
+ |
Collection<?> q = populatedQueue(SIZE); |
782 |
+ |
assertFalse(q.contains(null)); |
783 |
+ |
assertFalse(q.remove(null)); |
784 |
+ |
} |
785 |
|
} |