ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.45
Committed: Fri May 6 11:22:07 2011 UTC (13 years ago) by dl
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.44: +7 -7 lines
Log Message:
Add/use delay() instead of Thread.sleep to ensure sleeps are long enough

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