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) { |
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(); |
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 |
|
/** |
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 |
|
|
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 |
|
} |