ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.75
Committed: Sat Nov 5 23:38:21 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.74: +32 -11 lines
Log Message:
introduce random wrap and random spare capacity

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