ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.47
Committed: Fri May 27 20:07:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +67 -31 lines
Log Message:
performance and robustness improvements to queue tests

File Contents

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