129 |
|
*/ |
130 |
|
public void testConstructor6() { |
131 |
|
Item[] items = defaultItems; |
132 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(Arrays.asList(items)); |
132 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(Arrays.asList(items)); |
133 |
|
for (int i = 0; i < SIZE; ++i) |
134 |
|
mustEqual(items[i], q.poll()); |
135 |
|
} |
138 |
|
* Queue transitions from empty to full when elements added |
139 |
|
*/ |
140 |
|
public void testEmptyFull() { |
141 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
141 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(2); |
142 |
|
assertTrue(q.isEmpty()); |
143 |
|
mustEqual(2, q.remainingCapacity()); |
144 |
|
q.add(one); |
170 |
|
* Offer succeeds if not full; fails if full |
171 |
|
*/ |
172 |
|
public void testOffer() { |
173 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(1); |
173 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(1); |
174 |
|
assertTrue(q.offer(zero)); |
175 |
|
assertFalse(q.offer(one)); |
176 |
|
} |
179 |
|
* add succeeds if not full; throws IllegalStateException if full |
180 |
|
*/ |
181 |
|
public void testAdd() { |
182 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
182 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE); |
183 |
|
for (int i = 0; i < SIZE; ++i) |
184 |
|
mustAdd(q, i); |
185 |
|
mustEqual(0, q.remainingCapacity()); |
205 |
|
* possibly adding some elements |
206 |
|
*/ |
207 |
|
public void testAddAll3() { |
208 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
208 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE); |
209 |
|
Item[] items = new Item[2]; items[0] = zero; |
210 |
|
Collection<Item> elements = Arrays.asList(items); |
211 |
|
try { |
218 |
|
* addAll throws IllegalStateException if not enough room |
219 |
|
*/ |
220 |
|
public void testAddAll4() { |
221 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE - 1); |
221 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE - 1); |
222 |
|
Item[] items = defaultItems; |
223 |
|
Collection<Item> elements = Arrays.asList(items); |
224 |
|
try { |
233 |
|
public void testAddAll5() { |
234 |
|
Item[] empty = new Item[0]; |
235 |
|
Item[] items = defaultItems; |
236 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
236 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE); |
237 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
238 |
|
assertTrue(q.addAll(Arrays.asList(items))); |
239 |
|
for (int i = 0; i < SIZE; ++i) |
244 |
|
* all elements successfully put are contained |
245 |
|
*/ |
246 |
|
public void testPut() throws InterruptedException { |
247 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
247 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE); |
248 |
|
for (int i = 0; i < SIZE; ++i) { |
249 |
|
Item x = itemFor(i); |
250 |
|
q.put(x); |
257 |
|
* put blocks interruptibly if full |
258 |
|
*/ |
259 |
|
public void testBlockingPut() throws InterruptedException { |
260 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(SIZE); |
260 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(SIZE); |
261 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
262 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
263 |
|
public void realRun() throws InterruptedException { |
294 |
|
*/ |
295 |
|
public void testPutWithTake() throws InterruptedException { |
296 |
|
final int capacity = 2; |
297 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
297 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(2); |
298 |
|
final CountDownLatch pleaseTake = new CountDownLatch(1); |
299 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
300 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
334 |
|
* timed offer times out if full and elements not taken |
335 |
|
*/ |
336 |
|
public void testTimedOffer() { |
337 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
337 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(2); |
338 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
339 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
340 |
|
public void realRun() throws InterruptedException { |
526 |
|
* An add following remove(x) succeeds |
527 |
|
*/ |
528 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
529 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(); |
529 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(); |
530 |
|
assertTrue(q.add(one)); |
531 |
|
assertTrue(q.add(two)); |
532 |
|
mustRemove(q, one); |
568 |
|
*/ |
569 |
|
public void testContainsAll() { |
570 |
|
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
571 |
< |
LinkedBlockingQueue<Item> p = new LinkedBlockingQueue<Item>(SIZE); |
571 |
> |
LinkedBlockingQueue<Item> p = new LinkedBlockingQueue<>(SIZE); |
572 |
|
for (int i = 0; i < SIZE; ++i) { |
573 |
|
assertTrue(q.containsAll(p)); |
574 |
|
assertFalse(p.containsAll(q)); |
677 |
|
* iterator.remove removes current element |
678 |
|
*/ |
679 |
|
public void testIteratorRemove() { |
680 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
680 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(3); |
681 |
|
q.add(two); |
682 |
|
q.add(one); |
683 |
|
q.add(three); |
696 |
|
* iterator ordering is FIFO |
697 |
|
*/ |
698 |
|
public void testIteratorOrdering() { |
699 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
699 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(3); |
700 |
|
q.add(one); |
701 |
|
q.add(two); |
702 |
|
q.add(three); |
712 |
|
* Modifications do not cause iterators to fail |
713 |
|
*/ |
714 |
|
public void testWeaklyConsistentIteration() { |
715 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(3); |
715 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(3); |
716 |
|
q.add(one); |
717 |
|
q.add(two); |
718 |
|
q.add(three); |
738 |
|
* offer transfers elements across Executor tasks |
739 |
|
*/ |
740 |
|
public void testOfferInExecutor() { |
741 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
741 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(2); |
742 |
|
q.add(one); |
743 |
|
q.add(two); |
744 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
764 |
|
* timed poll retrieves elements across Executor threads |
765 |
|
*/ |
766 |
|
public void testPollInExecutor() { |
767 |
< |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(2); |
767 |
> |
final LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(2); |
768 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
769 |
|
final ExecutorService executor = Executors.newFixedThreadPool(2); |
770 |
|
try (PoolCleaner cleaner = cleaner(executor)) { |
807 |
|
*/ |
808 |
|
public void testDrainTo() { |
809 |
|
LinkedBlockingQueue<Item> q = populatedQueue(SIZE); |
810 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
810 |
> |
ArrayList<Item> l = new ArrayList<>(); |
811 |
|
q.drainTo(l); |
812 |
|
mustEqual(0, q.size()); |
813 |
|
mustEqual(SIZE, l.size()); |
837 |
|
}}); |
838 |
|
|
839 |
|
t.start(); |
840 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
840 |
> |
ArrayList<Item> l = new ArrayList<>(); |
841 |
|
q.drainTo(l); |
842 |
|
assertTrue(l.size() >= SIZE); |
843 |
|
for (int i = 0; i < SIZE; ++i) |
850 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
851 |
|
*/ |
852 |
|
public void testDrainToN() { |
853 |
< |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<Item>(); |
853 |
> |
LinkedBlockingQueue<Item> q = new LinkedBlockingQueue<>(); |
854 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
855 |
|
for (int j = 0; j < SIZE; j++) |
856 |
|
mustOffer(q, j); |
857 |
< |
ArrayList<Item> l = new ArrayList<Item>(); |
857 |
> |
ArrayList<Item> l = new ArrayList<>(); |
858 |
|
q.drainTo(l, i); |
859 |
|
int k = (i < SIZE) ? i : SIZE; |
860 |
|
mustEqual(k, l.size()); |
870 |
|
*/ |
871 |
|
public void testNeverContainsNull() { |
872 |
|
Collection<?>[] qs = { |
873 |
< |
new LinkedBlockingQueue<Object>(), |
873 |
> |
new LinkedBlockingQueue<>(), |
874 |
|
populatedQueue(2), |
875 |
|
}; |
876 |
|
|