ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
Revision: 1.16
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +15 -14 lines
Log Message:
import static TimeUnit.MILLISECONDS

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