ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.59
Committed: Wed Dec 31 19:21:20 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.58: +1 -1 lines
Log Message:
prefer do {} while (...); to while (...);

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