173 |
|
public void testConstructor7() { |
174 |
|
Item[] items = defaultItems; |
175 |
|
Collection<Item> elements = Arrays.asList(items); |
176 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE, true, elements); |
176 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE, true, elements); |
177 |
|
for (int i = 0; i < SIZE; ++i) |
178 |
|
mustEqual(items[i], q.poll()); |
179 |
|
} |
217 |
|
* Offer succeeds if not full; fails if full |
218 |
|
*/ |
219 |
|
public void testOffer() { |
220 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(1); |
220 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(1); |
221 |
|
assertTrue(q.offer(zero)); |
222 |
|
assertFalse(q.offer(one)); |
223 |
|
} |
226 |
|
* add succeeds if not full; throws IllegalStateException if full |
227 |
|
*/ |
228 |
|
public void testAdd() { |
229 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE); |
229 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE); |
230 |
|
for (int i = 0; i < SIZE; i++) assertTrue(q.add(itemFor(i))); |
231 |
|
mustEqual(0, q.remainingCapacity()); |
232 |
|
try { |
251 |
|
* possibly adding some elements |
252 |
|
*/ |
253 |
|
public void testAddAll3() { |
254 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE); |
254 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE); |
255 |
|
Item[] items = new Item[2]; items[0] = zero; |
256 |
|
try { |
257 |
|
q.addAll(Arrays.asList(items)); |
283 |
|
public void testAddAll5() { |
284 |
|
Item[] empty = new Item[0]; |
285 |
|
Item[] items = defaultItems; |
286 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE); |
286 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE); |
287 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
288 |
|
assertTrue(q.addAll(Arrays.asList(items))); |
289 |
|
for (int i = 0; i < SIZE; ++i) |
294 |
|
* all elements successfully put are contained |
295 |
|
*/ |
296 |
|
public void testPut() throws InterruptedException { |
297 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE); |
297 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE); |
298 |
|
for (int i = 0; i < SIZE; ++i) { |
299 |
|
Item x = itemFor(i); |
300 |
|
q.put(x); |
307 |
|
* put blocks interruptibly if full |
308 |
|
*/ |
309 |
|
public void testBlockingPut() throws InterruptedException { |
310 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE); |
310 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE); |
311 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
312 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
313 |
|
public void realRun() throws InterruptedException { |
344 |
|
*/ |
345 |
|
public void testPutWithTake() throws InterruptedException { |
346 |
|
final int capacity = 2; |
347 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(capacity); |
347 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(capacity); |
348 |
|
final CountDownLatch pleaseTake = new CountDownLatch(1); |
349 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
350 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
384 |
|
* timed offer times out if full and elements not taken |
385 |
|
*/ |
386 |
|
public void testTimedOffer() { |
387 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(2); |
387 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(2); |
388 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
389 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
390 |
|
public void realRun() throws InterruptedException { |
609 |
|
*/ |
610 |
|
public void testContainsAll() { |
611 |
|
ArrayBlockingQueue<Item> q = populatedQueue(SIZE); |
612 |
< |
ArrayBlockingQueue<Item> p = new ArrayBlockingQueue<Item>(SIZE); |
612 |
> |
ArrayBlockingQueue<Item> p = new ArrayBlockingQueue<>(SIZE); |
613 |
|
for (int i = 0; i < SIZE; ++i) { |
614 |
|
assertTrue(q.containsAll(p)); |
615 |
|
assertFalse(p.containsAll(q)); |
712 |
|
/** |
713 |
|
* toArray(incompatible array type) throws ArrayStoreException |
714 |
|
*/ |
715 |
+ |
@SuppressWarnings("CollectionToArraySafeParameter") |
716 |
|
public void testToArray_incompatibleArrayType() { |
717 |
|
ArrayBlockingQueue<Item> q = populatedQueue(SIZE); |
718 |
|
try { |
748 |
|
* iterator of empty collection has no elements |
749 |
|
*/ |
750 |
|
public void testEmptyIterator() { |
751 |
< |
assertIteratorExhausted(new ArrayBlockingQueue<Item>(SIZE).iterator()); |
751 |
> |
assertIteratorExhausted(new ArrayBlockingQueue<>(SIZE).iterator()); |
752 |
|
} |
753 |
|
|
754 |
|
/** |
755 |
|
* iterator.remove removes current element |
756 |
|
*/ |
757 |
|
public void testIteratorRemove() { |
758 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
758 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
759 |
|
q.add(two); |
760 |
|
q.add(one); |
761 |
|
q.add(three); |
774 |
|
* iterator ordering is FIFO |
775 |
|
*/ |
776 |
|
public void testIteratorOrdering() { |
777 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
777 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
778 |
|
q.add(one); |
779 |
|
q.add(two); |
780 |
|
q.add(three); |
792 |
|
* Modifications do not cause iterators to fail |
793 |
|
*/ |
794 |
|
public void testWeaklyConsistentIteration() { |
795 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
795 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
796 |
|
q.add(one); |
797 |
|
q.add(two); |
798 |
|
q.add(three); |
818 |
|
* offer transfers elements across Executor tasks |
819 |
|
*/ |
820 |
|
public void testOfferInExecutor() { |
821 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(2); |
821 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(2); |
822 |
|
q.add(one); |
823 |
|
q.add(two); |
824 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
845 |
|
* timed poll retrieves elements across Executor threads |
846 |
|
*/ |
847 |
|
public void testPollInExecutor() { |
848 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(2); |
848 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(2); |
849 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
850 |
|
final ExecutorService executor = Executors.newFixedThreadPool(2); |
851 |
|
try (PoolCleaner cleaner = cleaner(executor)) { |
888 |
|
*/ |
889 |
|
public void testDrainTo() { |
890 |
|
ArrayBlockingQueue<Item> q = populatedQueue(SIZE); |
891 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
891 |
> |
ArrayList<Item> l = new ArrayList<>(); |
892 |
|
q.drainTo(l); |
893 |
|
mustEqual(0, q.size()); |
894 |
|
mustEqual(SIZE, l.size()); |
918 |
|
}}); |
919 |
|
|
920 |
|
t.start(); |
921 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
921 |
> |
ArrayList<Item> l = new ArrayList<>(); |
922 |
|
q.drainTo(l); |
923 |
|
assertTrue(l.size() >= SIZE); |
924 |
|
for (int i = 0; i < SIZE; ++i) |
931 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
932 |
|
*/ |
933 |
|
public void testDrainToN() { |
934 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE * 2); |
934 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE * 2); |
935 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
936 |
|
for (int j = 0; j < SIZE; j++) |
937 |
|
mustOffer(q, j); |
938 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
938 |
> |
ArrayList<Item> l = new ArrayList<>(); |
939 |
|
q.drainTo(l, i); |
940 |
|
int k = (i < SIZE) ? i : SIZE; |
941 |
|
mustEqual(k, l.size()); |