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; |
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; |
58 |
< |
if (i > j) return 1; |
59 |
< |
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 |
|
} |
61 |
– |
|
61 |
|
public int compareTo(Delayed y) { |
62 |
|
return compareTo((PDelay)y); |
63 |
|
} |
65 |
– |
|
64 |
|
public boolean equals(Object other) { |
65 |
< |
return equals((PDelay)other); |
66 |
< |
} |
69 |
< |
public boolean equals(PDelay other) { |
70 |
< |
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; |
75 |
< |
} |
76 |
< |
public int intValue() { |
77 |
< |
return pseudodelay; |
71 |
> |
return Integer.MIN_VALUE + pseudodelay; |
72 |
|
} |
79 |
– |
|
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) { |
127 |
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
128 |
|
assertTrue(q.isEmpty()); |
129 |
< |
for (int i = n-1; i >= 0; i-=2) |
129 |
> |
for (int i = n-1; i >= 0; i -= 2) |
130 |
|
assertTrue(q.offer(new PDelay(i))); |
131 |
< |
for (int i = (n & 1); i < n; i+=2) |
131 |
> |
for (int i = (n & 1); i < n; i += 2) |
132 |
|
assertTrue(q.offer(new PDelay(i))); |
133 |
|
assertFalse(q.isEmpty()); |
134 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
148 |
|
*/ |
149 |
|
public void testConstructor3() { |
150 |
|
try { |
151 |
< |
DelayQueue q = new DelayQueue(null); |
151 |
> |
new DelayQueue(null); |
152 |
|
shouldThrow(); |
153 |
|
} catch (NullPointerException success) {} |
154 |
|
} |
159 |
|
public void testConstructor4() { |
160 |
|
try { |
161 |
|
PDelay[] ints = new PDelay[SIZE]; |
162 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
162 |
> |
new DelayQueue(Arrays.asList(ints)); |
163 |
|
shouldThrow(); |
164 |
|
} catch (NullPointerException success) {} |
165 |
|
} |
172 |
|
PDelay[] ints = new PDelay[SIZE]; |
173 |
|
for (int i = 0; i < SIZE-1; ++i) |
174 |
|
ints[i] = new PDelay(i); |
175 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
175 |
> |
new DelayQueue(Arrays.asList(ints)); |
176 |
|
shouldThrow(); |
177 |
|
} catch (NullPointerException success) {} |
178 |
|
} |
289 |
|
public void testPut() { |
290 |
|
DelayQueue q = new DelayQueue(); |
291 |
|
for (int i = 0; i < SIZE; ++i) { |
292 |
< |
PDelay I = new PDelay(i); |
293 |
< |
q.put(I); |
294 |
< |
assertTrue(q.contains(I)); |
292 |
> |
PDelay x = new PDelay(i); |
293 |
> |
q.put(x); |
294 |
> |
assertTrue(q.contains(x)); |
295 |
|
} |
296 |
|
assertEquals(SIZE, q.size()); |
297 |
|
} |
335 |
|
public void testTake() throws InterruptedException { |
336 |
|
DelayQueue q = populatedQueue(SIZE); |
337 |
|
for (int i = 0; i < SIZE; ++i) { |
338 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
338 |
> |
assertEquals(new PDelay(i), q.take()); |
339 |
|
} |
340 |
|
} |
341 |
|
|
378 |
|
public void testPoll() { |
379 |
|
DelayQueue q = populatedQueue(SIZE); |
380 |
|
for (int i = 0; i < SIZE; ++i) { |
381 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
381 |
> |
assertEquals(new PDelay(i), q.poll()); |
382 |
|
} |
383 |
|
assertNull(q.poll()); |
384 |
|
} |
389 |
|
public void testTimedPoll0() throws InterruptedException { |
390 |
|
DelayQueue q = populatedQueue(SIZE); |
391 |
|
for (int i = 0; i < SIZE; ++i) { |
392 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
392 |
> |
assertEquals(new PDelay(i), q.poll(0, MILLISECONDS)); |
393 |
|
} |
394 |
|
assertNull(q.poll(0, MILLISECONDS)); |
395 |
|
} |
401 |
|
DelayQueue q = populatedQueue(SIZE); |
402 |
|
for (int i = 0; i < SIZE; ++i) { |
403 |
|
long startTime = System.nanoTime(); |
404 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
404 |
> |
assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); |
405 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
406 |
|
} |
407 |
|
long startTime = System.nanoTime(); |
450 |
|
public void testPeek() { |
451 |
|
DelayQueue q = populatedQueue(SIZE); |
452 |
|
for (int i = 0; i < SIZE; ++i) { |
453 |
< |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
454 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
453 |
> |
assertEquals(new PDelay(i), q.peek()); |
454 |
> |
assertEquals(new PDelay(i), q.poll()); |
455 |
|
if (q.isEmpty()) |
456 |
|
assertNull(q.peek()); |
457 |
|
else |
466 |
|
public void testElement() { |
467 |
|
DelayQueue q = populatedQueue(SIZE); |
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
< |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
469 |
> |
assertEquals(new PDelay(i), q.element()); |
470 |
|
q.poll(); |
471 |
|
} |
472 |
|
try { |
481 |
|
public void testRemove() { |
482 |
|
DelayQueue q = populatedQueue(SIZE); |
483 |
|
for (int i = 0; i < SIZE; ++i) { |
484 |
< |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
484 |
> |
assertEquals(new PDelay(i), q.remove()); |
485 |
|
} |
486 |
|
try { |
487 |
|
q.remove(); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
497 |
– |
* remove(x) removes x and returns true if present |
498 |
– |
*/ |
499 |
– |
public void testRemoveElement() { |
500 |
– |
DelayQueue q = populatedQueue(SIZE); |
501 |
– |
for (int i = 1; i < SIZE; i+=2) { |
502 |
– |
assertTrue(q.remove(new PDelay(i))); |
503 |
– |
} |
504 |
– |
for (int i = 0; i < SIZE; i+=2) { |
505 |
– |
assertTrue(q.remove(new PDelay(i))); |
506 |
– |
assertFalse(q.remove(new PDelay(i+1))); |
507 |
– |
} |
508 |
– |
assertTrue(q.isEmpty()); |
509 |
– |
} |
510 |
– |
|
511 |
– |
/** |
493 |
|
* contains(x) reports true when elements added but not yet removed |
494 |
|
*/ |
495 |
|
public void testContains() { |
561 |
|
assertTrue(q.removeAll(p)); |
562 |
|
assertEquals(SIZE-i, q.size()); |
563 |
|
for (int j = 0; j < i; ++j) { |
564 |
< |
PDelay I = (PDelay)(p.remove()); |
565 |
< |
assertFalse(q.contains(I)); |
564 |
> |
PDelay x = (PDelay)(p.remove()); |
565 |
> |
assertFalse(q.contains(x)); |
566 |
|
} |
567 |
|
} |
568 |
|
} |
614 |
|
++i; |
615 |
|
} |
616 |
|
assertEquals(i, SIZE); |
617 |
+ |
assertIteratorExhausted(it); |
618 |
+ |
} |
619 |
+ |
|
620 |
+ |
/** |
621 |
+ |
* iterator of empty collection has no elements |
622 |
+ |
*/ |
623 |
+ |
public void testEmptyIterator() { |
624 |
+ |
assertIteratorExhausted(new DelayQueue().iterator()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
636 |
|
it.next(); |
637 |
|
it.remove(); |
638 |
|
it = q.iterator(); |
639 |
< |
assertEquals(it.next(), new PDelay(2)); |
640 |
< |
assertEquals(it.next(), new PDelay(3)); |
639 |
> |
assertEquals(new PDelay(2), it.next()); |
640 |
> |
assertEquals(new PDelay(3), it.next()); |
641 |
|
assertFalse(it.hasNext()); |
642 |
|
} |
643 |
|
|
647 |
|
public void testToString() { |
648 |
|
DelayQueue q = populatedQueue(SIZE); |
649 |
|
String s = q.toString(); |
650 |
< |
for (int i = 0; i < SIZE; ++i) { |
651 |
< |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
663 |
< |
} |
650 |
> |
for (Object e : q) |
651 |
> |
assertTrue(s.contains(e.toString())); |
652 |
|
} |
653 |
|
|
654 |
|
/** |
662 |
|
public void realRun() throws InterruptedException { |
663 |
|
assertNull(q.poll()); |
664 |
|
threadsStarted.await(); |
665 |
< |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
665 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
666 |
|
checkEmpty(q); |
667 |
|
}}); |
668 |
|
|
734 |
|
} |
735 |
|
ArrayList l = new ArrayList(); |
736 |
|
q.drainTo(l); |
737 |
< |
assertEquals(q.size(), 0); |
737 |
> |
assertEquals(0, q.size()); |
738 |
|
for (int i = 0; i < SIZE; ++i) |
739 |
< |
assertEquals(l.get(i), elems[i]); |
739 |
> |
assertEquals(elems[i], l.get(i)); |
740 |
|
q.add(elems[0]); |
741 |
|
q.add(elems[1]); |
742 |
|
assertFalse(q.isEmpty()); |
744 |
|
assertTrue(q.contains(elems[1])); |
745 |
|
l.clear(); |
746 |
|
q.drainTo(l); |
747 |
< |
assertEquals(q.size(), 0); |
748 |
< |
assertEquals(l.size(), 2); |
747 |
> |
assertEquals(0, q.size()); |
748 |
> |
assertEquals(2, l.size()); |
749 |
|
for (int i = 0; i < 2; ++i) |
750 |
< |
assertEquals(l.get(i), elems[i]); |
750 |
> |
assertEquals(elems[i], l.get(i)); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
777 |
|
ArrayList l = new ArrayList(); |
778 |
|
q.drainTo(l, i); |
779 |
|
int k = (i < SIZE) ? i : SIZE; |
780 |
< |
assertEquals(q.size(), SIZE-k); |
781 |
< |
assertEquals(l.size(), k); |
780 |
> |
assertEquals(SIZE-k, q.size()); |
781 |
> |
assertEquals(k, l.size()); |
782 |
|
} |
783 |
|
} |
784 |
|
|
785 |
+ |
/** |
786 |
+ |
* remove(null), contains(null) always return false |
787 |
+ |
*/ |
788 |
+ |
public void testNeverContainsNull() { |
789 |
+ |
Collection<?> q = populatedQueue(SIZE); |
790 |
+ |
assertFalse(q.contains(null)); |
791 |
+ |
assertFalse(q.remove(null)); |
792 |
+ |
} |
793 |
|
} |