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)); |
621 |
|
* iterator.remove removes current element |
622 |
|
*/ |
623 |
|
public void testIteratorRemove() { |
624 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
624 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
625 |
|
q.add(new PDelay(2)); |
626 |
|
q.add(new PDelay(1)); |
627 |
|
q.add(new PDelay(3)); |
648 |
|
* timed poll transfers elements across Executor tasks |
649 |
|
*/ |
650 |
|
public void testPollInExecutor() { |
651 |
< |
final DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
651 |
> |
final DelayQueue<PDelay> q = new DelayQueue<>(); |
652 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
653 |
|
final ExecutorService executor = Executors.newFixedThreadPool(2); |
654 |
|
try (PoolCleaner cleaner = cleaner(executor)) { |
692 |
|
* peek of a non-empty queue returns non-null even if not expired |
693 |
|
*/ |
694 |
|
public void testPeekDelayed() { |
695 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
695 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
696 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
697 |
|
assertNotNull(q.peek()); |
698 |
|
} |
701 |
|
* poll of a non-empty queue returns null if no expired elements. |
702 |
|
*/ |
703 |
|
public void testPollDelayed() { |
704 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
704 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
705 |
|
q.add(new NanoDelay(Long.MAX_VALUE)); |
706 |
|
assertNull(q.poll()); |
707 |
|
} |
710 |
|
* timed poll of a non-empty queue returns null if no expired elements. |
711 |
|
*/ |
712 |
|
public void testTimedPollDelayed() throws InterruptedException { |
713 |
< |
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>(); |
713 |
> |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
714 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
715 |
|
long startTime = System.nanoTime(); |
716 |
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
721 |
|
* drainTo(c) empties queue into another collection c |
722 |
|
*/ |
723 |
|
public void testDrainTo() { |
724 |
< |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
724 |
> |
DelayQueue<PDelay> q = new DelayQueue<>(); |
725 |
|
PDelay[] elems = new PDelay[SIZE]; |
726 |
|
for (int i = 0; i < SIZE; ++i) { |
727 |
|
elems[i] = new PDelay(i); |
728 |
|
q.add(elems[i]); |
729 |
|
} |
730 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
730 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
731 |
|
q.drainTo(l); |
732 |
|
mustEqual(0, q.size()); |
733 |
|
for (int i = 0; i < SIZE; ++i) |
756 |
|
}}); |
757 |
|
|
758 |
|
t.start(); |
759 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
759 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
760 |
|
q.drainTo(l); |
761 |
|
assertTrue(l.size() >= SIZE); |
762 |
|
t.join(); |
769 |
|
public void testDrainToN() { |
770 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
771 |
|
DelayQueue<PDelay> q = populatedQueue(SIZE); |
772 |
< |
ArrayList<PDelay> l = new ArrayList<PDelay>(); |
772 |
> |
ArrayList<PDelay> l = new ArrayList<>(); |
773 |
|
q.drainTo(l, i); |
774 |
|
int k = (i < SIZE) ? i : SIZE; |
775 |
|
mustEqual(SIZE - k, q.size()); |