ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.6
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.5: +152 -0 lines
Log Message:
Added tests and documentation

File Contents

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