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)); |
747 |
|
* iterator of empty collection has no elements |
748 |
|
*/ |
749 |
|
public void testEmptyIterator() { |
750 |
< |
assertIteratorExhausted(new ArrayBlockingQueue<Item>(SIZE).iterator()); |
750 |
> |
assertIteratorExhausted(new ArrayBlockingQueue<>(SIZE).iterator()); |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
|
* iterator.remove removes current element |
755 |
|
*/ |
756 |
|
public void testIteratorRemove() { |
757 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
757 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
758 |
|
q.add(two); |
759 |
|
q.add(one); |
760 |
|
q.add(three); |
773 |
|
* iterator ordering is FIFO |
774 |
|
*/ |
775 |
|
public void testIteratorOrdering() { |
776 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
776 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
777 |
|
q.add(one); |
778 |
|
q.add(two); |
779 |
|
q.add(three); |
791 |
|
* Modifications do not cause iterators to fail |
792 |
|
*/ |
793 |
|
public void testWeaklyConsistentIteration() { |
794 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(3); |
794 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(3); |
795 |
|
q.add(one); |
796 |
|
q.add(two); |
797 |
|
q.add(three); |
817 |
|
* offer transfers elements across Executor tasks |
818 |
|
*/ |
819 |
|
public void testOfferInExecutor() { |
820 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(2); |
820 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(2); |
821 |
|
q.add(one); |
822 |
|
q.add(two); |
823 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
844 |
|
* timed poll retrieves elements across Executor threads |
845 |
|
*/ |
846 |
|
public void testPollInExecutor() { |
847 |
< |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(2); |
847 |
> |
final ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(2); |
848 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
849 |
|
final ExecutorService executor = Executors.newFixedThreadPool(2); |
850 |
|
try (PoolCleaner cleaner = cleaner(executor)) { |
887 |
|
*/ |
888 |
|
public void testDrainTo() { |
889 |
|
ArrayBlockingQueue<Item> q = populatedQueue(SIZE); |
890 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
890 |
> |
ArrayList<Item> l = new ArrayList<>(); |
891 |
|
q.drainTo(l); |
892 |
|
mustEqual(0, q.size()); |
893 |
|
mustEqual(SIZE, l.size()); |
917 |
|
}}); |
918 |
|
|
919 |
|
t.start(); |
920 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
920 |
> |
ArrayList<Item> l = new ArrayList<>(); |
921 |
|
q.drainTo(l); |
922 |
|
assertTrue(l.size() >= SIZE); |
923 |
|
for (int i = 0; i < SIZE; ++i) |
930 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
931 |
|
*/ |
932 |
|
public void testDrainToN() { |
933 |
< |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<Item>(SIZE * 2); |
933 |
> |
ArrayBlockingQueue<Item> q = new ArrayBlockingQueue<>(SIZE * 2); |
934 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
935 |
|
for (int j = 0; j < SIZE; j++) |
936 |
|
mustOffer(q, j); |
937 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
937 |
> |
ArrayList<Item> l = new ArrayList<>(); |
938 |
|
q.drainTo(l, i); |
939 |
|
int k = (i < SIZE) ? i : SIZE; |
940 |
|
mustEqual(k, l.size()); |