ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.76
Committed: Sun Nov 6 02:40:38 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.75: +18 -16 lines
Log Message:
whitespace

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