22 |
|
|
23 |
|
/** |
24 |
|
* A delayed implmentation for testing. |
25 |
< |
* Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
25 |
> |
* Most tests use Pseudodelays, where delays are all elapsed |
26 |
|
* (so, no blocking solely for delays) but are still ordered |
27 |
|
*/ |
28 |
|
static class PDelay implements Delayed { |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* |
132 |
> |
* A new queue has unbounded capacity |
133 |
|
*/ |
134 |
|
public void testConstructor1() { |
135 |
|
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* |
139 |
> |
* Initializing from null Collection throws NPE |
140 |
|
*/ |
141 |
|
public void testConstructor3() { |
142 |
|
try { |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* |
150 |
> |
* Initializing from Collection of null elements throws NPE |
151 |
|
*/ |
152 |
|
public void testConstructor4() { |
153 |
|
try { |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* |
162 |
> |
* Initializing from Collection with some null elements throws NPE |
163 |
|
*/ |
164 |
|
public void testConstructor5() { |
165 |
|
try { |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* |
176 |
> |
* Queue contains all elements of collection used to initialize |
177 |
|
*/ |
178 |
|
public void testConstructor6() { |
179 |
|
try { |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* |
191 |
> |
* isEmpty is true before add, false after |
192 |
|
*/ |
193 |
|
public void testEmpty() { |
194 |
|
DelayQueue q = new DelayQueue(); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* |
206 |
> |
* remainingCapacity does not change when elementa added or removed, |
207 |
> |
* but size does |
208 |
|
*/ |
209 |
|
public void testRemainingCapacity() { |
210 |
|
DelayQueue q = populatedQueue(SIZE); |
221 |
|
} |
222 |
|
|
223 |
|
/** |
224 |
< |
* |
224 |
> |
* offer(null) throws NPE |
225 |
|
*/ |
226 |
|
public void testOfferNull() { |
227 |
|
try { |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* |
235 |
> |
* offer non-null succeeds |
236 |
|
*/ |
237 |
|
public void testOffer() { |
238 |
|
DelayQueue q = new DelayQueue(); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
< |
* |
244 |
> |
* add succeeds |
245 |
|
*/ |
246 |
|
public void testAdd() { |
247 |
|
DelayQueue q = new DelayQueue(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* |
255 |
> |
* addAll(null) throws NPE |
256 |
|
*/ |
257 |
|
public void testAddAll1() { |
258 |
|
try { |
263 |
|
catch (NullPointerException success) {} |
264 |
|
} |
265 |
|
/** |
266 |
< |
* |
266 |
> |
* addAll of a collection with null elements throws NPE |
267 |
|
*/ |
268 |
|
public void testAddAll2() { |
269 |
|
try { |
275 |
|
catch (NullPointerException success) {} |
276 |
|
} |
277 |
|
/** |
278 |
< |
* |
278 |
> |
* addAll of a collection with any null elements throws NPE after |
279 |
> |
* possibly adding some elements |
280 |
|
*/ |
281 |
|
public void testAddAll3() { |
282 |
|
try { |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* |
294 |
> |
* Queue contains all elements of successful addAll |
295 |
|
*/ |
296 |
|
public void testAddAll5() { |
297 |
|
try { |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* |
312 |
> |
* put(null) throws NPE |
313 |
|
*/ |
314 |
|
public void testPutNull() { |
315 |
|
try { |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
< |
* |
325 |
> |
* all elements successfully put are contained |
326 |
|
*/ |
327 |
|
public void testPut() { |
328 |
|
try { |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
< |
* |
342 |
> |
* put doesn't block waiting for take |
343 |
|
*/ |
344 |
|
public void testPutWithTake() { |
345 |
|
final DelayQueue q = new DelayQueue(); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* |
375 |
> |
* timed offer does not time out |
376 |
|
*/ |
377 |
|
public void testTimedOffer() { |
378 |
|
final DelayQueue q = new DelayQueue(); |
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* |
401 |
> |
* take retrieves elements in priority order |
402 |
|
*/ |
403 |
|
public void testTake() { |
404 |
|
try { |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* |
415 |
> |
* take blocks interruptibly when empty |
416 |
|
*/ |
417 |
|
public void testTakeFromEmpty() { |
418 |
|
final DelayQueue q = new DelayQueue(); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* |
438 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
439 |
|
*/ |
440 |
|
public void testBlockingTake() { |
441 |
|
Thread t = new Thread(new Runnable() { |
463 |
|
|
464 |
|
|
465 |
|
/** |
466 |
< |
* |
466 |
> |
* poll succeeds unless empty |
467 |
|
*/ |
468 |
|
public void testPoll() { |
469 |
|
DelayQueue q = populatedQueue(SIZE); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
< |
* |
477 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
478 |
|
*/ |
479 |
|
public void testTimedPoll0() { |
480 |
|
try { |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* |
492 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
493 |
|
*/ |
494 |
|
public void testTimedPoll() { |
495 |
|
try { |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
< |
* |
507 |
> |
* Interrupted timed poll throws InterruptedException instead of |
508 |
> |
* returning timeout status |
509 |
|
*/ |
510 |
|
public void testInterruptedTimedPoll() { |
511 |
|
Thread t = new Thread(new Runnable() { |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* |
534 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
535 |
> |
* on interruption throws |
536 |
|
*/ |
537 |
|
public void testTimedPollWithOffer() { |
538 |
|
final DelayQueue q = new DelayQueue(); |
559 |
|
|
560 |
|
|
561 |
|
/** |
562 |
< |
* |
562 |
> |
* peek returns next element, or null if empty |
563 |
|
*/ |
564 |
|
public void testPeek() { |
565 |
|
DelayQueue q = populatedQueue(SIZE); |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* |
576 |
> |
* element returns next element, or throws NSEE if empty |
577 |
|
*/ |
578 |
|
public void testElement() { |
579 |
|
DelayQueue q = populatedQueue(SIZE); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
592 |
< |
* |
592 |
> |
* remove removes next element, or throws NSEE if empty |
593 |
|
*/ |
594 |
|
public void testRemove() { |
595 |
|
DelayQueue q = populatedQueue(SIZE); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* |
607 |
> |
* remove(x) removes x and returns true if present |
608 |
|
*/ |
609 |
|
public void testRemoveElement() { |
610 |
|
DelayQueue q = populatedQueue(SIZE); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
< |
* |
622 |
> |
* contains(x) reports true when elements added but not yet removed |
623 |
|
*/ |
624 |
|
public void testContains() { |
625 |
|
DelayQueue q = populatedQueue(SIZE); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
634 |
< |
* |
634 |
> |
* clear removes all elements |
635 |
|
*/ |
636 |
|
public void testClear() { |
637 |
|
DelayQueue q = populatedQueue(SIZE); |
646 |
|
} |
647 |
|
|
648 |
|
/** |
649 |
< |
* |
649 |
> |
* containsAll(c) is true when c contains a subset of elements |
650 |
|
*/ |
651 |
|
public void testContainsAll() { |
652 |
|
DelayQueue q = populatedQueue(SIZE); |
660 |
|
} |
661 |
|
|
662 |
|
/** |
663 |
< |
* |
663 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
664 |
|
*/ |
665 |
|
public void testRetainAll() { |
666 |
|
DelayQueue q = populatedQueue(SIZE); |
679 |
|
} |
680 |
|
|
681 |
|
/** |
682 |
< |
* |
682 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
683 |
|
*/ |
684 |
|
public void testRemoveAll() { |
685 |
|
for (int i = 1; i < SIZE; ++i) { |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
< |
* |
698 |
> |
* toArray contains all elements |
699 |
|
*/ |
700 |
|
public void testToArray() { |
701 |
|
DelayQueue q = populatedQueue(SIZE); |
710 |
|
} |
711 |
|
|
712 |
|
/** |
713 |
< |
* |
713 |
> |
* toArray(a) contains all elements |
714 |
|
*/ |
715 |
|
public void testToArray2() { |
716 |
|
DelayQueue q = populatedQueue(SIZE); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
< |
* |
729 |
> |
* iterator iterates through all elements |
730 |
|
*/ |
731 |
|
public void testIterator() { |
732 |
|
DelayQueue q = populatedQueue(SIZE); |
740 |
|
} |
741 |
|
|
742 |
|
/** |
743 |
< |
* |
743 |
> |
* iterator.remove removes current element |
744 |
|
*/ |
745 |
|
public void testIteratorRemove () { |
742 |
– |
|
746 |
|
final DelayQueue q = new DelayQueue(); |
744 |
– |
|
747 |
|
q.add(new PDelay(2)); |
748 |
|
q.add(new PDelay(1)); |
749 |
|
q.add(new PDelay(3)); |
748 |
– |
|
750 |
|
Iterator it = q.iterator(); |
751 |
|
it.next(); |
752 |
|
it.remove(); |
752 |
– |
|
753 |
|
it = q.iterator(); |
754 |
|
assertEquals(it.next(), new PDelay(2)); |
755 |
|
assertEquals(it.next(), new PDelay(3)); |
758 |
|
|
759 |
|
|
760 |
|
/** |
761 |
< |
* |
761 |
> |
* toString contains toStrings of elements |
762 |
|
*/ |
763 |
|
public void testToString() { |
764 |
|
DelayQueue q = populatedQueue(SIZE); |
769 |
|
} |
770 |
|
|
771 |
|
/** |
772 |
< |
* |
772 |
> |
* offer transfers elements across Executor tasks |
773 |
|
*/ |
774 |
|
public void testPollInExecutor() { |
775 |
– |
|
775 |
|
final DelayQueue q = new DelayQueue(); |
777 |
– |
|
776 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
779 |
– |
|
777 |
|
executor.execute(new Runnable() { |
778 |
|
public void run() { |
779 |
|
threadAssertNull(q.poll()); |
798 |
|
} |
799 |
|
} |
800 |
|
}); |
804 |
– |
|
801 |
|
joinPool(executor); |
802 |
|
|
803 |
|
} |
804 |
|
|
805 |
|
|
806 |
|
/** |
807 |
< |
* |
807 |
> |
* Dekayed actions do not occur until their delay elapses |
808 |
|
*/ |
809 |
|
public void testDelay() { |
810 |
|
DelayQueue q = new DelayQueue(); |