176 |
|
PDelay[] items = new PDelay[SIZE]; |
177 |
|
for (int i = 0; i < SIZE; ++i) |
178 |
|
items[i] = new PDelay(i); |
179 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(Arrays.asList(items)); |
179 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(Arrays.asList(items)); |
180 |
|
for (int i = 0; i < SIZE; ++i) |
181 |
|
mustEqual(items[i], q.poll()); |
182 |
|
} |
185 |
|
* isEmpty is true before add, false after |
186 |
|
*/ |
187 |
|
public void testEmpty() { |
188 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
188 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
189 |
|
assertTrue(q.isEmpty()); |
190 |
|
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
191 |
|
q.add(new PDelay(1)); |
217 |
|
* offer non-null succeeds |
218 |
|
*/ |
219 |
|
public void testOffer() { |
220 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
220 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
221 |
|
assertTrue(q.offer(new PDelay(0))); |
222 |
|
assertTrue(q.offer(new PDelay(1))); |
223 |
|
} |
226 |
|
* add succeeds |
227 |
|
*/ |
228 |
|
public void testAdd() { |
229 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
229 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
230 |
|
for (int i = 0; i < SIZE; ++i) { |
231 |
|
mustEqual(i, q.size()); |
232 |
|
assertTrue(q.add(new PDelay(i))); |
249 |
|
* possibly adding some elements |
250 |
|
*/ |
251 |
|
public void testAddAll3() { |
252 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
252 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
253 |
|
PDelay[] a = new PDelay[SIZE]; |
254 |
|
for (int i = 0; i < SIZE - 1; ++i) |
255 |
|
a[i] = new PDelay(i); |
267 |
|
PDelay[] items = new PDelay[SIZE]; |
268 |
|
for (int i = SIZE - 1; i >= 0; --i) |
269 |
|
items[i] = new PDelay(i); |
270 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
270 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
271 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
|
assertTrue(q.addAll(Arrays.asList(items))); |
273 |
|
for (int i = 0; i < SIZE; ++i) |
278 |
|
* all elements successfully put are contained |
279 |
|
*/ |
280 |
|
public void testPut() { |
281 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
281 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
282 |
|
for (int i = 0; i < SIZE; ++i) { |
283 |
|
PDelay x = new PDelay(i); |
284 |
|
q.put(x); |
291 |
|
* put doesn't block waiting for take |
292 |
|
*/ |
293 |
|
public void testPutWithTake() throws InterruptedException { |
294 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
294 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
295 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
296 |
|
public void realRun() { |
297 |
|
q.put(new PDelay(0)); |
308 |
|
* Queue is unbounded, so timed offer never times out |
309 |
|
*/ |
310 |
|
public void testTimedOffer() throws InterruptedException { |
311 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
311 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
312 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
313 |
|
public void realRun() throws InterruptedException { |
314 |
|
q.put(new PDelay(0)); |
514 |
|
*/ |
515 |
|
public void testContainsAll() { |
516 |
|
DelayQueue<PDelay> q = populatedQueue(SIZE); |
517 |
< |
DelayQueue<PDelay> p = new DelayQueue<PDelay>(); |
517 |
> |
DelayQueue<PDelay> p = new DelayQueue<>(); |
518 |
|
for (int i = 0; i < SIZE; ++i) { |
519 |
|
assertTrue(q.containsAll(p)); |
520 |
|
assertFalse(p.containsAll(q)); |
587 |
|
/** |
588 |
|
* toArray(incompatible array type) throws ArrayStoreException |
589 |
|
*/ |
590 |
< |
public void testToArray1_BadArg() { |
590 |
> |
@SuppressWarnings("CollectionToArraySafeParameter") |
591 |
> |
public void testToArray_incompatibleArrayType() { |
592 |
|
DelayQueue<PDelay> q = populatedQueue(SIZE); |
593 |
|
try { |
594 |
|
q.toArray(new String[10]); |
622 |
|
* iterator.remove removes current element |
623 |
|
*/ |
624 |
|
public void testIteratorRemove() { |
625 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
625 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
626 |
|
q.add(new PDelay(2)); |
627 |
|
q.add(new PDelay(1)); |
628 |
|
q.add(new PDelay(3)); |
649 |
|
* timed poll transfers elements across Executor tasks |
650 |
|
*/ |
651 |
|
public void testPollInExecutor() { |
652 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
652 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
653 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
654 |
|
final ExecutorService executor = Executors.newFixedThreadPool(2); |
655 |
|
try (PoolCleaner cleaner = cleaner(executor)) { |
693 |
|
* peek of a non-empty queue returns non-null even if not expired |
694 |
|
*/ |
695 |
|
public void testPeekDelayed() { |
696 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
696 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
697 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
698 |
|
assertNotNull(q.peek()); |
699 |
|
} |
702 |
|
* poll of a non-empty queue returns null if no expired elements. |
703 |
|
*/ |
704 |
|
public void testPollDelayed() { |
705 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
705 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
706 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
707 |
|
assertNull(q.poll()); |
708 |
|
} |
711 |
|
* timed poll of a non-empty queue returns null if no expired elements. |
712 |
|
*/ |
713 |
|
public void testTimedPollDelayed() throws InterruptedException { |
714 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
714 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
715 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
716 |
|
long startTime = System.nanoTime(); |
717 |
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
722 |
|
* drainTo(c) empties queue into another collection c |
723 |
|
*/ |
724 |
|
public void testDrainTo() { |
725 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
725 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
726 |
|
PDelay[] elems = new PDelay[SIZE]; |
727 |
|
for (int i = 0; i < SIZE; ++i) { |
728 |
|
elems[i] = new PDelay(i); |
729 |
|
q.add(elems[i]); |
730 |
|
} |
731 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
731 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
732 |
|
q.drainTo(l); |
733 |
|
mustEqual(0, q.size()); |
734 |
|
for (int i = 0; i < SIZE; ++i) |
757 |
|
}}); |
758 |
|
|
759 |
|
t.start(); |
760 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
760 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
761 |
|
q.drainTo(l); |
762 |
|
assertTrue(l.size() >= SIZE); |
763 |
|
t.join(); |
770 |
|
public void testDrainToN() { |
771 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
772 |
|
DelayQueue<PDelay> q = populatedQueue(SIZE); |
773 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
773 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
774 |
|
q.drainTo(l, i); |
775 |
|
int k = (i < SIZE) ? i : SIZE; |
776 |
|
mustEqual(SIZE - k, q.size()); |