ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.74
Committed: Sun Oct 30 21:07:27 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.73: +42 -69 lines
Log Message:
more thorough toArray tests

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.7 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.43 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.12 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.58 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10    
11     import java.util.ArrayList;
12 jsr166 1.48 import java.util.Arrays;
13     import java.util.Collection;
14     import java.util.Iterator;
15     import java.util.NoSuchElementException;
16 jsr166 1.49 import java.util.Queue;
17 jsr166 1.48 import java.util.concurrent.ArrayBlockingQueue;
18     import java.util.concurrent.BlockingQueue;
19     import java.util.concurrent.CountDownLatch;
20     import java.util.concurrent.Executors;
21     import java.util.concurrent.ExecutorService;
22 jsr166 1.74 import java.util.concurrent.ThreadLocalRandom;
23 jsr166 1.58
24     import junit.framework.Test;
25 dl 1.1
26 dl 1.3 public class ArrayBlockingQueueTest extends JSR166TestCase {
27 jsr166 1.32
28     public static class Fair extends BlockingQueueTest {
29     protected BlockingQueue emptyCollection() {
30 jsr166 1.50 return new ArrayBlockingQueue(SIZE, true);
31 jsr166 1.32 }
32     }
33    
34     public static class NonFair extends BlockingQueueTest {
35     protected BlockingQueue emptyCollection() {
36 jsr166 1.50 return new ArrayBlockingQueue(SIZE, false);
37 jsr166 1.32 }
38     }
39    
40 dl 1.1 public static void main(String[] args) {
41 jsr166 1.65 main(suite(), args);
42 dl 1.1 }
43 jsr166 1.32
44 dl 1.1 public static Test suite() {
45 jsr166 1.73 class Implementation implements CollectionImplementation {
46     public Class<?> klazz() { return ArrayBlockingQueue.class; }
47     public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); }
48     public Object makeElement(int i) { return i; }
49     public boolean isConcurrent() { return true; }
50     public boolean permitsNulls() { return false; }
51     }
52 jsr166 1.32 return newTestSuite(ArrayBlockingQueueTest.class,
53     new Fair().testSuite(),
54 jsr166 1.73 new NonFair().testSuite(),
55     CollectionTest.testSuite(new Implementation()));
56 dl 1.1 }
57    
58     /**
59 jsr166 1.54 * Returns a new queue of given size containing consecutive
60 jsr166 1.72 * Integers 0 ... n - 1.
61 dl 1.1 */
62 jsr166 1.41 private ArrayBlockingQueue<Integer> populatedQueue(int n) {
63     ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
64 dl 1.1 assertTrue(q.isEmpty());
65 jsr166 1.15 for (int i = 0; i < n; i++)
66     assertTrue(q.offer(new Integer(i)));
67 dl 1.1 assertFalse(q.isEmpty());
68     assertEquals(0, q.remainingCapacity());
69 jsr166 1.15 assertEquals(n, q.size());
70 jsr166 1.72 assertEquals((Integer) 0, q.peek());
71 dl 1.1 return q;
72     }
73 jsr166 1.12
74 dl 1.4 /**
75 dl 1.5 * A new queue has the indicated capacity
76 dl 1.4 */
77     public void testConstructor1() {
78 dl 1.3 assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
79 dl 1.1 }
80    
81 dl 1.4 /**
82 jsr166 1.18 * Constructor throws IAE if capacity argument nonpositive
83 dl 1.4 */
84     public void testConstructor2() {
85 dl 1.1 try {
86 jsr166 1.48 new ArrayBlockingQueue(0);
87 dl 1.4 shouldThrow();
88 jsr166 1.17 } catch (IllegalArgumentException success) {}
89 dl 1.1 }
90    
91 dl 1.4 /**
92 dl 1.5 * Initializing from null Collection throws NPE
93 dl 1.4 */
94     public void testConstructor3() {
95 dl 1.1 try {
96 jsr166 1.48 new ArrayBlockingQueue(1, true, null);
97 dl 1.4 shouldThrow();
98 jsr166 1.17 } catch (NullPointerException success) {}
99 dl 1.1 }
100    
101 dl 1.4 /**
102 dl 1.5 * Initializing from Collection of null elements throws NPE
103 dl 1.4 */
104     public void testConstructor4() {
105 jsr166 1.48 Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
106 dl 1.1 try {
107 jsr166 1.48 new ArrayBlockingQueue(SIZE, false, elements);
108 dl 1.4 shouldThrow();
109 jsr166 1.17 } catch (NullPointerException success) {}
110 dl 1.1 }
111    
112 dl 1.4 /**
113 dl 1.5 * Initializing from Collection with some null elements throws NPE
114 dl 1.4 */
115     public void testConstructor5() {
116 jsr166 1.48 Integer[] ints = new Integer[SIZE];
117 jsr166 1.66 for (int i = 0; i < SIZE - 1; ++i)
118 jsr166 1.48 ints[i] = i;
119     Collection<Integer> elements = Arrays.asList(ints);
120 dl 1.1 try {
121 jsr166 1.62 new ArrayBlockingQueue(SIZE, false, elements);
122 dl 1.4 shouldThrow();
123 jsr166 1.17 } catch (NullPointerException success) {}
124 dl 1.1 }
125    
126 dl 1.4 /**
127 dl 1.5 * Initializing from too large collection throws IAE
128 dl 1.4 */
129     public void testConstructor6() {
130 jsr166 1.48 Integer[] ints = new Integer[SIZE];
131     for (int i = 0; i < SIZE; ++i)
132     ints[i] = i;
133     Collection<Integer> elements = Arrays.asList(ints);
134 dl 1.1 try {
135 jsr166 1.48 new ArrayBlockingQueue(SIZE - 1, false, elements);
136 dl 1.4 shouldThrow();
137 jsr166 1.17 } catch (IllegalArgumentException success) {}
138 dl 1.1 }
139    
140 dl 1.4 /**
141 dl 1.5 * Queue contains all elements of collection used to initialize
142 dl 1.4 */
143     public void testConstructor7() {
144 jsr166 1.19 Integer[] ints = new Integer[SIZE];
145     for (int i = 0; i < SIZE; ++i)
146 jsr166 1.48 ints[i] = i;
147     Collection<Integer> elements = Arrays.asList(ints);
148     ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
149 jsr166 1.19 for (int i = 0; i < SIZE; ++i)
150     assertEquals(ints[i], q.poll());
151 dl 1.1 }
152    
153 dl 1.4 /**
154 dl 1.5 * Queue transitions from empty to full when elements added
155 dl 1.4 */
156 dl 1.1 public void testEmptyFull() {
157     ArrayBlockingQueue q = new ArrayBlockingQueue(2);
158     assertTrue(q.isEmpty());
159 dl 1.4 assertEquals(2, q.remainingCapacity());
160 dl 1.3 q.add(one);
161 dl 1.1 assertFalse(q.isEmpty());
162 dl 1.3 q.add(two);
163 dl 1.1 assertFalse(q.isEmpty());
164 dl 1.4 assertEquals(0, q.remainingCapacity());
165     assertFalse(q.offer(three));
166 dl 1.1 }
167    
168 dl 1.4 /**
169 dl 1.5 * remainingCapacity decreases on add, increases on remove
170 dl 1.4 */
171     public void testRemainingCapacity() {
172 jsr166 1.64 BlockingQueue q = populatedQueue(SIZE);
173 dl 1.3 for (int i = 0; i < SIZE; ++i) {
174 dl 1.1 assertEquals(i, q.remainingCapacity());
175 jsr166 1.64 assertEquals(SIZE, q.size() + q.remainingCapacity());
176     assertEquals(i, q.remove());
177 dl 1.1 }
178 dl 1.3 for (int i = 0; i < SIZE; ++i) {
179 jsr166 1.66 assertEquals(SIZE - i, q.remainingCapacity());
180 jsr166 1.64 assertEquals(SIZE, q.size() + q.remainingCapacity());
181     assertTrue(q.add(i));
182 dl 1.1 }
183     }
184    
185 dl 1.4 /**
186 dl 1.5 * Offer succeeds if not full; fails if full
187 dl 1.4 */
188     public void testOffer() {
189 dl 1.1 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
190 dl 1.3 assertTrue(q.offer(zero));
191     assertFalse(q.offer(one));
192 dl 1.1 }
193    
194 dl 1.4 /**
195 dl 1.5 * add succeeds if not full; throws ISE if full
196 dl 1.4 */
197     public void testAdd() {
198 jsr166 1.63 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
199     for (int i = 0; i < SIZE; ++i) {
200     assertTrue(q.add(new Integer(i)));
201     }
202     assertEquals(0, q.remainingCapacity());
203 jsr166 1.15 try {
204 dl 1.3 q.add(new Integer(SIZE));
205 jsr166 1.17 shouldThrow();
206     } catch (IllegalStateException success) {}
207 dl 1.1 }
208    
209 dl 1.4 /**
210 dl 1.6 * addAll(this) throws IAE
211     */
212     public void testAddAllSelf() {
213 jsr166 1.63 ArrayBlockingQueue q = populatedQueue(SIZE);
214 dl 1.6 try {
215     q.addAll(q);
216     shouldThrow();
217 jsr166 1.17 } catch (IllegalArgumentException success) {}
218 dl 1.6 }
219    
220 dl 1.4 /**
221 dl 1.5 * addAll of a collection with any null elements throws NPE after
222     * possibly adding some elements
223 dl 1.4 */
224     public void testAddAll3() {
225 jsr166 1.63 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
226     Integer[] ints = new Integer[SIZE];
227 jsr166 1.66 for (int i = 0; i < SIZE - 1; ++i)
228 jsr166 1.63 ints[i] = new Integer(i);
229 dl 1.1 try {
230     q.addAll(Arrays.asList(ints));
231 dl 1.4 shouldThrow();
232 jsr166 1.17 } catch (NullPointerException success) {}
233 dl 1.1 }
234 jsr166 1.30
235 dl 1.4 /**
236 dl 1.5 * addAll throws ISE if not enough room
237 dl 1.4 */
238     public void testAddAll4() {
239 jsr166 1.63 ArrayBlockingQueue q = new ArrayBlockingQueue(1);
240     Integer[] ints = new Integer[SIZE];
241     for (int i = 0; i < SIZE; ++i)
242     ints[i] = new Integer(i);
243 dl 1.1 try {
244     q.addAll(Arrays.asList(ints));
245 dl 1.4 shouldThrow();
246 jsr166 1.17 } catch (IllegalStateException success) {}
247 dl 1.1 }
248 jsr166 1.30
249 dl 1.4 /**
250 dl 1.5 * Queue contains all elements, in traversal order, of successful addAll
251 dl 1.4 */
252     public void testAddAll5() {
253 jsr166 1.17 Integer[] empty = new Integer[0];
254     Integer[] ints = new Integer[SIZE];
255     for (int i = 0; i < SIZE; ++i)
256     ints[i] = new Integer(i);
257     ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
258     assertFalse(q.addAll(Arrays.asList(empty)));
259     assertTrue(q.addAll(Arrays.asList(ints)));
260     for (int i = 0; i < SIZE; ++i)
261     assertEquals(ints[i], q.poll());
262 dl 1.1 }
263    
264 dl 1.4 /**
265 dl 1.5 * all elements successfully put are contained
266 dl 1.4 */
267 jsr166 1.17 public void testPut() throws InterruptedException {
268     ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
269     for (int i = 0; i < SIZE; ++i) {
270 jsr166 1.60 Integer x = new Integer(i);
271     q.put(x);
272     assertTrue(q.contains(x));
273 dl 1.1 }
274 jsr166 1.17 assertEquals(0, q.remainingCapacity());
275 dl 1.1 }
276    
277 dl 1.4 /**
278 dl 1.5 * put blocks interruptibly if full
279 dl 1.4 */
280 jsr166 1.17 public void testBlockingPut() throws InterruptedException {
281 dl 1.10 final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
282 jsr166 1.47 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
283     Thread t = newStartedThread(new CheckedRunnable() {
284 jsr166 1.25 public void realRun() throws InterruptedException {
285     for (int i = 0; i < SIZE; ++i)
286     q.put(i);
287     assertEquals(SIZE, q.size());
288     assertEquals(0, q.remainingCapacity());
289 jsr166 1.47
290     Thread.currentThread().interrupt();
291     try {
292     q.put(99);
293     shouldThrow();
294     } catch (InterruptedException success) {}
295     assertFalse(Thread.interrupted());
296    
297     pleaseInterrupt.countDown();
298 jsr166 1.17 try {
299 jsr166 1.25 q.put(99);
300     shouldThrow();
301     } catch (InterruptedException success) {}
302 jsr166 1.47 assertFalse(Thread.interrupted());
303 jsr166 1.25 }});
304 jsr166 1.17
305 jsr166 1.47 await(pleaseInterrupt);
306     assertThreadStaysAlive(t);
307 jsr166 1.17 t.interrupt();
308 jsr166 1.47 awaitTermination(t);
309 jsr166 1.25 assertEquals(SIZE, q.size());
310     assertEquals(0, q.remainingCapacity());
311 dl 1.1 }
312    
313 dl 1.4 /**
314 jsr166 1.47 * put blocks interruptibly waiting for take when full
315 dl 1.4 */
316 jsr166 1.17 public void testPutWithTake() throws InterruptedException {
317 jsr166 1.27 final int capacity = 2;
318     final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
319 jsr166 1.47 final CountDownLatch pleaseTake = new CountDownLatch(1);
320     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
321     Thread t = newStartedThread(new CheckedRunnable() {
322 jsr166 1.27 public void realRun() throws InterruptedException {
323 jsr166 1.47 for (int i = 0; i < capacity; i++)
324 jsr166 1.27 q.put(i);
325 jsr166 1.47 pleaseTake.countDown();
326     q.put(86);
327    
328     pleaseInterrupt.countDown();
329 jsr166 1.17 try {
330 jsr166 1.27 q.put(99);
331     shouldThrow();
332     } catch (InterruptedException success) {}
333 jsr166 1.47 assertFalse(Thread.interrupted());
334 jsr166 1.17 }});
335    
336 jsr166 1.47 await(pleaseTake);
337 jsr166 1.51 assertEquals(0, q.remainingCapacity());
338 jsr166 1.27 assertEquals(0, q.take());
339 jsr166 1.47
340     await(pleaseInterrupt);
341     assertThreadStaysAlive(t);
342 jsr166 1.17 t.interrupt();
343 jsr166 1.47 awaitTermination(t);
344 jsr166 1.51 assertEquals(0, q.remainingCapacity());
345 dl 1.1 }
346    
347 dl 1.4 /**
348 dl 1.5 * timed offer times out if full and elements not taken
349 dl 1.4 */
350 jsr166 1.17 public void testTimedOffer() throws InterruptedException {
351 dl 1.1 final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
352 jsr166 1.46 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
353     Thread t = newStartedThread(new CheckedRunnable() {
354 jsr166 1.17 public void realRun() throws InterruptedException {
355     q.put(new Object());
356     q.put(new Object());
357 jsr166 1.46 long startTime = System.nanoTime();
358     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
359     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
360     pleaseInterrupt.countDown();
361 jsr166 1.24 try {
362 jsr166 1.46 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
363 jsr166 1.24 shouldThrow();
364     } catch (InterruptedException success) {}
365     }});
366 jsr166 1.17
367 jsr166 1.46 await(pleaseInterrupt);
368 jsr166 1.47 assertThreadStaysAlive(t);
369 jsr166 1.17 t.interrupt();
370 jsr166 1.46 awaitTermination(t);
371 dl 1.1 }
372    
373 dl 1.4 /**
374 dl 1.5 * take retrieves elements in FIFO order
375 dl 1.4 */
376 jsr166 1.17 public void testTake() throws InterruptedException {
377     ArrayBlockingQueue q = populatedQueue(SIZE);
378     for (int i = 0; i < SIZE; ++i) {
379 jsr166 1.25 assertEquals(i, q.take());
380 jsr166 1.15 }
381 dl 1.1 }
382    
383 dl 1.4 /**
384 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
385 dl 1.4 */
386 jsr166 1.17 public void testBlockingTake() throws InterruptedException {
387 jsr166 1.25 final ArrayBlockingQueue q = populatedQueue(SIZE);
388 jsr166 1.47 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
389     Thread t = newStartedThread(new CheckedRunnable() {
390 jsr166 1.17 public void realRun() throws InterruptedException {
391     for (int i = 0; i < SIZE; ++i) {
392 jsr166 1.25 assertEquals(i, q.take());
393 jsr166 1.17 }
394 jsr166 1.47
395     Thread.currentThread().interrupt();
396     try {
397     q.take();
398     shouldThrow();
399     } catch (InterruptedException success) {}
400     assertFalse(Thread.interrupted());
401    
402     pleaseInterrupt.countDown();
403 jsr166 1.25 try {
404     q.take();
405     shouldThrow();
406     } catch (InterruptedException success) {}
407 jsr166 1.47 assertFalse(Thread.interrupted());
408 jsr166 1.25 }});
409 jsr166 1.17
410 jsr166 1.47 await(pleaseInterrupt);
411     assertThreadStaysAlive(t);
412 jsr166 1.25 t.interrupt();
413 jsr166 1.47 awaitTermination(t);
414 dl 1.1 }
415    
416 dl 1.4 /**
417 dl 1.5 * poll succeeds unless empty
418 dl 1.4 */
419     public void testPoll() {
420 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
421     for (int i = 0; i < SIZE; ++i) {
422 jsr166 1.25 assertEquals(i, q.poll());
423 dl 1.1 }
424 jsr166 1.15 assertNull(q.poll());
425 dl 1.1 }
426    
427 dl 1.4 /**
428 jsr166 1.37 * timed poll with zero timeout succeeds when non-empty, else times out
429 dl 1.4 */
430 jsr166 1.17 public void testTimedPoll0() throws InterruptedException {
431     ArrayBlockingQueue q = populatedQueue(SIZE);
432     for (int i = 0; i < SIZE; ++i) {
433 jsr166 1.25 assertEquals(i, q.poll(0, MILLISECONDS));
434 jsr166 1.15 }
435 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
436 jsr166 1.47 checkEmpty(q);
437 dl 1.1 }
438    
439 dl 1.4 /**
440 jsr166 1.37 * timed poll with nonzero timeout succeeds when non-empty, else times out
441 dl 1.4 */
442 jsr166 1.17 public void testTimedPoll() throws InterruptedException {
443     ArrayBlockingQueue q = populatedQueue(SIZE);
444     for (int i = 0; i < SIZE; ++i) {
445 jsr166 1.47 long startTime = System.nanoTime();
446     assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
447     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
448     }
449     long startTime = System.nanoTime();
450     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
451     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
452     checkEmpty(q);
453 dl 1.1 }
454    
455 dl 1.4 /**
456 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
457     * returning timeout status
458 dl 1.4 */
459 jsr166 1.17 public void testInterruptedTimedPoll() throws InterruptedException {
460 jsr166 1.42 final BlockingQueue<Integer> q = populatedQueue(SIZE);
461     final CountDownLatch aboutToWait = new CountDownLatch(1);
462     Thread t = newStartedThread(new CheckedRunnable() {
463 jsr166 1.17 public void realRun() throws InterruptedException {
464 jsr166 1.70 long startTime = System.nanoTime();
465 jsr166 1.17 for (int i = 0; i < SIZE; ++i) {
466 jsr166 1.70 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
467 jsr166 1.17 }
468 jsr166 1.42 aboutToWait.countDown();
469 jsr166 1.21 try {
470 jsr166 1.70 q.poll(LONG_DELAY_MS, MILLISECONDS);
471 jsr166 1.22 shouldThrow();
472 jsr166 1.42 } catch (InterruptedException success) {
473 jsr166 1.69 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
474 jsr166 1.42 }
475 jsr166 1.21 }});
476 jsr166 1.17
477 jsr166 1.69 await(aboutToWait);
478 jsr166 1.71 waitForThreadToEnterWaitState(t);
479 jsr166 1.17 t.interrupt();
480 jsr166 1.70 awaitTermination(t);
481 jsr166 1.42 checkEmpty(q);
482 dl 1.1 }
483    
484 dl 1.4 /**
485 dl 1.5 * peek returns next element, or null if empty
486 dl 1.4 */
487     public void testPeek() {
488 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
489     for (int i = 0; i < SIZE; ++i) {
490 jsr166 1.26 assertEquals(i, q.peek());
491     assertEquals(i, q.poll());
492 dl 1.1 assertTrue(q.peek() == null ||
493 jsr166 1.26 !q.peek().equals(i));
494 dl 1.1 }
495 jsr166 1.15 assertNull(q.peek());
496 dl 1.1 }
497    
498 dl 1.4 /**
499 dl 1.5 * element returns next element, or throws NSEE if empty
500 dl 1.4 */
501     public void testElement() {
502 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
503     for (int i = 0; i < SIZE; ++i) {
504 jsr166 1.26 assertEquals(i, q.element());
505     assertEquals(i, q.poll());
506 dl 1.1 }
507     try {
508     q.element();
509 dl 1.4 shouldThrow();
510 jsr166 1.17 } catch (NoSuchElementException success) {}
511 dl 1.1 }
512    
513 dl 1.4 /**
514 dl 1.5 * remove removes next element, or throws NSEE if empty
515 dl 1.4 */
516     public void testRemove() {
517 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
518     for (int i = 0; i < SIZE; ++i) {
519 jsr166 1.26 assertEquals(i, q.remove());
520 dl 1.1 }
521     try {
522     q.remove();
523 dl 1.4 shouldThrow();
524 jsr166 1.17 } catch (NoSuchElementException success) {}
525 dl 1.1 }
526    
527 dl 1.4 /**
528 dl 1.5 * contains(x) reports true when elements added but not yet removed
529 dl 1.4 */
530     public void testContains() {
531 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
532     for (int i = 0; i < SIZE; ++i) {
533 dl 1.1 assertTrue(q.contains(new Integer(i)));
534 jsr166 1.26 assertEquals(i, q.poll());
535 dl 1.1 assertFalse(q.contains(new Integer(i)));
536     }
537     }
538    
539 dl 1.4 /**
540 dl 1.5 * clear removes all elements
541 dl 1.4 */
542     public void testClear() {
543 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
544 dl 1.1 q.clear();
545     assertTrue(q.isEmpty());
546     assertEquals(0, q.size());
547 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
548     q.add(one);
549 dl 1.1 assertFalse(q.isEmpty());
550 dl 1.11 assertTrue(q.contains(one));
551 dl 1.1 q.clear();
552     assertTrue(q.isEmpty());
553     }
554    
555 dl 1.4 /**
556 dl 1.5 * containsAll(c) is true when c contains a subset of elements
557 dl 1.4 */
558     public void testContainsAll() {
559 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
560     ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
561     for (int i = 0; i < SIZE; ++i) {
562 dl 1.1 assertTrue(q.containsAll(p));
563     assertFalse(p.containsAll(q));
564     p.add(new Integer(i));
565     }
566     assertTrue(p.containsAll(q));
567     }
568    
569 dl 1.4 /**
570 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
571 dl 1.4 */
572     public void testRetainAll() {
573 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
574     ArrayBlockingQueue p = populatedQueue(SIZE);
575     for (int i = 0; i < SIZE; ++i) {
576 dl 1.1 boolean changed = q.retainAll(p);
577     if (i == 0)
578     assertFalse(changed);
579     else
580     assertTrue(changed);
581    
582     assertTrue(q.containsAll(p));
583 jsr166 1.66 assertEquals(SIZE - i, q.size());
584 dl 1.1 p.remove();
585     }
586     }
587    
588 dl 1.4 /**
589 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
590 dl 1.4 */
591     public void testRemoveAll() {
592 dl 1.3 for (int i = 1; i < SIZE; ++i) {
593     ArrayBlockingQueue q = populatedQueue(SIZE);
594     ArrayBlockingQueue p = populatedQueue(i);
595 dl 1.1 assertTrue(q.removeAll(p));
596 jsr166 1.66 assertEquals(SIZE - i, q.size());
597 dl 1.1 for (int j = 0; j < i; ++j) {
598 jsr166 1.60 Integer x = (Integer)(p.remove());
599     assertFalse(q.contains(x));
600 dl 1.1 }
601     }
602     }
603    
604 jsr166 1.74 void checkToArray(ArrayBlockingQueue<Integer> q) {
605 jsr166 1.55 int size = q.size();
606 jsr166 1.74 Object[] a1 = q.toArray();
607     assertEquals(size, a1.length);
608     Integer[] a2 = q.toArray(new Integer[0]);
609     assertEquals(size, a2.length);
610     Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
611     assertEquals(size, a3.length);
612     Integer[] a4 = new Integer[size];
613     assertSame(a4, q.toArray(a4));
614     Integer[] a5 = new Integer[size + 1];
615     Arrays.fill(a5, 42);
616     assertSame(a5, q.toArray(a5));
617     Integer[] a6 = new Integer[size + 2];
618     Arrays.fill(a6, 42);
619     assertSame(a6, q.toArray(a6));
620     Object[][] as = { a1, a2, a3, a4, a5, a6 };
621     for (Object[] a : as) {
622     if (a.length > size) assertNull(a[size]);
623     if (a.length > size + 1) assertEquals(42, a[size + 1]);
624     }
625 jsr166 1.55 Iterator it = q.iterator();
626 jsr166 1.74 Integer s = q.peek();
627 jsr166 1.55 for (int i = 0; i < size; i++) {
628     Integer x = (Integer) it.next();
629 jsr166 1.74 assertEquals(s + i, (int) x);
630     for (Object[] a : as)
631     assertSame(a1[i], x);
632 jsr166 1.55 }
633     }
634    
635 dl 1.4 /**
636 jsr166 1.74 * toArray() and toArray(a) contain all elements in FIFO order
637 dl 1.4 */
638 jsr166 1.40 public void testToArray() {
639 jsr166 1.74 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
640     final int size = rnd.nextInt(6);
641     final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
642     ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
643     for (int i = 0; i < size; i++) {
644 jsr166 1.55 checkToArray(q);
645     q.add(i);
646     }
647     // Provoke wraparound
648 jsr166 1.74 int added = size * 2;
649     for (int i = 0; i < added; i++) {
650 jsr166 1.55 checkToArray(q);
651 jsr166 1.74 assertEquals((Integer) i, q.poll());
652     q.add(size + i);
653 jsr166 1.55 }
654 jsr166 1.74 for (int i = 0; i < size; i++) {
655 jsr166 1.55 checkToArray(q);
656 jsr166 1.74 assertEquals((Integer) (added + i), q.poll());
657 jsr166 1.55 }
658 dl 1.1 }
659 dl 1.6
660     /**
661 jsr166 1.38 * toArray(incompatible array type) throws ArrayStoreException
662 dl 1.6 */
663 jsr166 1.74 public void testToArray_incompatibleArrayType() {
664 jsr166 1.26 ArrayBlockingQueue q = populatedQueue(SIZE);
665 jsr166 1.15 try {
666 jsr166 1.38 q.toArray(new String[10]);
667 jsr166 1.15 shouldThrow();
668 jsr166 1.18 } catch (ArrayStoreException success) {}
669 jsr166 1.74 try {
670     q.toArray(new String[0]);
671     shouldThrow();
672     } catch (ArrayStoreException success) {}
673 dl 1.6 }
674    
675 dl 1.4 /**
676 dl 1.5 * iterator iterates through all elements
677 dl 1.4 */
678 jsr166 1.17 public void testIterator() throws InterruptedException {
679 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
680 jsr166 1.15 Iterator it = q.iterator();
681 jsr166 1.61 int i;
682     for (i = 0; it.hasNext(); i++)
683     assertTrue(q.contains(it.next()));
684     assertEquals(i, SIZE);
685     assertIteratorExhausted(it);
686    
687     it = q.iterator();
688     for (i = 0; it.hasNext(); i++)
689 jsr166 1.17 assertEquals(it.next(), q.take());
690 jsr166 1.61 assertEquals(i, SIZE);
691     assertIteratorExhausted(it);
692     }
693    
694     /**
695     * iterator of empty collection has no elements
696     */
697     public void testEmptyIterator() {
698     assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator());
699 dl 1.1 }
700    
701 dl 1.4 /**
702 dl 1.5 * iterator.remove removes current element
703     */
704 jsr166 1.29 public void testIteratorRemove() {
705 dl 1.5 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
706     q.add(two);
707     q.add(one);
708     q.add(three);
709    
710     Iterator it = q.iterator();
711     it.next();
712     it.remove();
713 jsr166 1.12
714 dl 1.5 it = q.iterator();
715 jsr166 1.28 assertSame(it.next(), one);
716     assertSame(it.next(), three);
717 dl 1.5 assertFalse(it.hasNext());
718     }
719    
720     /**
721     * iterator ordering is FIFO
722 dl 1.4 */
723 dl 1.1 public void testIteratorOrdering() {
724     final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
725 dl 1.3 q.add(one);
726     q.add(two);
727     q.add(three);
728 dl 1.1
729     assertEquals("queue should be full", 0, q.remainingCapacity());
730    
731     int k = 0;
732     for (Iterator it = q.iterator(); it.hasNext();) {
733 jsr166 1.26 assertEquals(++k, it.next());
734 dl 1.1 }
735 dl 1.4 assertEquals(3, k);
736 dl 1.1 }
737    
738 dl 1.4 /**
739 dl 1.5 * Modifications do not cause iterators to fail
740 dl 1.4 */
741 jsr166 1.29 public void testWeaklyConsistentIteration() {
742 dl 1.1 final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
743 dl 1.3 q.add(one);
744     q.add(two);
745     q.add(three);
746 jsr166 1.17 for (Iterator it = q.iterator(); it.hasNext();) {
747     q.remove();
748     it.next();
749 dl 1.1 }
750 dl 1.4 assertEquals(0, q.size());
751 dl 1.1 }
752    
753 dl 1.4 /**
754 dl 1.5 * toString contains toStrings of elements
755 dl 1.4 */
756     public void testToString() {
757 dl 1.3 ArrayBlockingQueue q = populatedQueue(SIZE);
758 dl 1.1 String s = q.toString();
759 dl 1.3 for (int i = 0; i < SIZE; ++i) {
760 jsr166 1.47 assertTrue(s.contains(String.valueOf(i)));
761 dl 1.1 }
762 jsr166 1.12 }
763 dl 1.1
764 dl 1.4 /**
765 dl 1.5 * offer transfers elements across Executor tasks
766 dl 1.4 */
767 dl 1.1 public void testOfferInExecutor() {
768     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
769 dl 1.3 q.add(one);
770     q.add(two);
771 jsr166 1.47 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
772 jsr166 1.68 final ExecutorService executor = Executors.newFixedThreadPool(2);
773     try (PoolCleaner cleaner = cleaner(executor)) {
774     executor.execute(new CheckedRunnable() {
775     public void realRun() throws InterruptedException {
776     assertFalse(q.offer(three));
777     threadsStarted.await();
778     assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
779     assertEquals(0, q.remainingCapacity());
780     }});
781    
782     executor.execute(new CheckedRunnable() {
783     public void realRun() throws InterruptedException {
784     threadsStarted.await();
785     assertEquals(0, q.remainingCapacity());
786     assertSame(one, q.take());
787     }});
788     }
789 dl 1.1 }
790    
791 dl 1.4 /**
792 jsr166 1.47 * timed poll retrieves elements across Executor threads
793 dl 1.4 */
794 dl 1.1 public void testPollInExecutor() {
795     final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
796 jsr166 1.47 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
797 jsr166 1.68 final ExecutorService executor = Executors.newFixedThreadPool(2);
798     try (PoolCleaner cleaner = cleaner(executor)) {
799     executor.execute(new CheckedRunnable() {
800     public void realRun() throws InterruptedException {
801     assertNull(q.poll());
802     threadsStarted.await();
803     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
804     checkEmpty(q);
805     }});
806    
807     executor.execute(new CheckedRunnable() {
808     public void realRun() throws InterruptedException {
809     threadsStarted.await();
810     q.put(one);
811     }});
812     }
813 dl 1.1 }
814 dl 1.2
815 dl 1.4 /**
816 dl 1.5 * A deserialized serialized queue has same elements in same order
817 dl 1.4 */
818 jsr166 1.17 public void testSerialization() throws Exception {
819 jsr166 1.49 Queue x = populatedQueue(SIZE);
820     Queue y = serialClone(x);
821 dl 1.2
822 jsr166 1.56 assertNotSame(x, y);
823 jsr166 1.49 assertEquals(x.size(), y.size());
824     assertEquals(x.toString(), y.toString());
825     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
826     while (!x.isEmpty()) {
827     assertFalse(y.isEmpty());
828     assertEquals(x.remove(), y.remove());
829     }
830     assertTrue(y.isEmpty());
831 dl 1.6 }
832    
833     /**
834     * drainTo(c) empties queue into another collection c
835 jsr166 1.12 */
836 dl 1.6 public void testDrainTo() {
837     ArrayBlockingQueue q = populatedQueue(SIZE);
838     ArrayList l = new ArrayList();
839     q.drainTo(l);
840 jsr166 1.51 assertEquals(0, q.size());
841     assertEquals(SIZE, l.size());
842 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
843 dl 1.6 assertEquals(l.get(i), new Integer(i));
844 dl 1.11 q.add(zero);
845     q.add(one);
846     assertFalse(q.isEmpty());
847     assertTrue(q.contains(zero));
848     assertTrue(q.contains(one));
849     l.clear();
850     q.drainTo(l);
851 jsr166 1.51 assertEquals(0, q.size());
852     assertEquals(2, l.size());
853 jsr166 1.12 for (int i = 0; i < 2; ++i)
854 dl 1.11 assertEquals(l.get(i), new Integer(i));
855 dl 1.6 }
856    
857     /**
858     * drainTo empties full queue, unblocking a waiting put.
859 jsr166 1.12 */
860 jsr166 1.17 public void testDrainToWithActivePut() throws InterruptedException {
861 dl 1.6 final ArrayBlockingQueue q = populatedQueue(SIZE);
862 jsr166 1.17 Thread t = new Thread(new CheckedRunnable() {
863     public void realRun() throws InterruptedException {
864 jsr166 1.66 q.put(new Integer(SIZE + 1));
865 jsr166 1.17 }});
866    
867     t.start();
868     ArrayList l = new ArrayList();
869     q.drainTo(l);
870     assertTrue(l.size() >= SIZE);
871     for (int i = 0; i < SIZE; ++i)
872     assertEquals(l.get(i), new Integer(i));
873     t.join();
874     assertTrue(q.size() + l.size() >= SIZE);
875 dl 1.6 }
876    
877     /**
878 jsr166 1.34 * drainTo(c, n) empties first min(n, size) elements of queue into c
879 jsr166 1.12 */
880 dl 1.6 public void testDrainToN() {
881 jsr166 1.66 ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
882 dl 1.6 for (int i = 0; i < SIZE + 2; ++i) {
883 jsr166 1.13 for (int j = 0; j < SIZE; j++)
884 dl 1.11 assertTrue(q.offer(new Integer(j)));
885 dl 1.6 ArrayList l = new ArrayList();
886     q.drainTo(l, i);
887 jsr166 1.35 int k = (i < SIZE) ? i : SIZE;
888 jsr166 1.52 assertEquals(k, l.size());
889 jsr166 1.66 assertEquals(SIZE - k, q.size());
890 jsr166 1.12 for (int j = 0; j < k; ++j)
891 dl 1.6 assertEquals(l.get(j), new Integer(j));
892 jsr166 1.59 do {} while (q.poll() != null);
893 dl 1.2 }
894     }
895    
896 jsr166 1.57 /**
897     * remove(null), contains(null) always return false
898     */
899     public void testNeverContainsNull() {
900     Collection<?>[] qs = {
901     new ArrayBlockingQueue<Object>(10),
902     populatedQueue(2),
903     };
904    
905     for (Collection<?> q : qs) {
906     assertFalse(q.contains(null));
907     assertFalse(q.remove(null));
908     }
909     }
910 dl 1.1 }