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 |
|
|
65 |
|
return (other instanceof PDelay) && |
66 |
|
this.pseudodelay == ((PDelay)other).pseudodelay; |
67 |
|
} |
68 |
+ |
// suppress [overrides] javac warning |
69 |
+ |
public int hashCode() { return pseudodelay; } |
70 |
|
public long getDelay(TimeUnit ignore) { |
71 |
|
return Integer.MIN_VALUE + pseudodelay; |
72 |
|
} |
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) { |
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 |
|
|
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 |
|
} |