ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +72 -67 lines
Log Message:
improve tck javadocs; rename and add a few tests

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.5 * Offer succeeds if not full; fails if full
156 dl 1.4 */
157     public void testOffer() {
158 dl 1.1 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
159 dl 1.3 assertTrue(q.offer(zero));
160     assertFalse(q.offer(one));
161 dl 1.1 }
162    
163 dl 1.4 /**
164 dl 1.5 * add succeeds if not full; throws ISE if full
165 dl 1.4 */
166     public void testAdd() {
167 dl 1.1 try {
168 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
169     for (int i = 0; i < SIZE; ++i) {
170 dl 1.1 assertTrue(q.add(new Integer(i)));
171     }
172     assertEquals(0, q.remainingCapacity());
173 dl 1.3 q.add(new Integer(SIZE));
174 dl 1.1 } catch (IllegalStateException success){
175     }
176     }
177    
178 dl 1.4 /**
179 dl 1.5 * addAll(null) throws NPE
180 dl 1.4 */
181     public void testAddAll1() {
182 dl 1.1 try {
183     LinkedBlockingQueue q = new LinkedBlockingQueue(1);
184     q.addAll(null);
185 dl 1.4 shouldThrow();
186 dl 1.1 }
187     catch (NullPointerException success) {}
188     }
189 dl 1.4 /**
190 dl 1.5 * addAll of a collection with null elements throws NPE
191 dl 1.4 */
192     public void testAddAll2() {
193 dl 1.1 try {
194 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
195     Integer[] ints = new Integer[SIZE];
196 dl 1.1 q.addAll(Arrays.asList(ints));
197 dl 1.4 shouldThrow();
198 dl 1.1 }
199     catch (NullPointerException success) {}
200     }
201 dl 1.4 /**
202 dl 1.5 * addAll of a collection with any null elements throws NPE after
203     * possibly adding some elements
204 dl 1.4 */
205     public void testAddAll3() {
206 dl 1.1 try {
207 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
208     Integer[] ints = new Integer[SIZE];
209     for (int i = 0; i < SIZE-1; ++i)
210 dl 1.1 ints[i] = new Integer(i);
211     q.addAll(Arrays.asList(ints));
212 dl 1.4 shouldThrow();
213 dl 1.1 }
214     catch (NullPointerException success) {}
215     }
216 dl 1.4 /**
217 dl 1.5 * addAll throws ISE if not enough room
218 dl 1.4 */
219     public void testAddAll4() {
220 dl 1.1 try {
221     LinkedBlockingQueue q = new LinkedBlockingQueue(1);
222 dl 1.3 Integer[] ints = new Integer[SIZE];
223     for (int i = 0; i < SIZE; ++i)
224 dl 1.1 ints[i] = new Integer(i);
225     q.addAll(Arrays.asList(ints));
226 dl 1.4 shouldThrow();
227 dl 1.1 }
228     catch (IllegalStateException success) {}
229     }
230 dl 1.4 /**
231 dl 1.5 * Queue contains all elements, in traversal order, of successful addAll
232 dl 1.4 */
233     public void testAddAll5() {
234 dl 1.1 try {
235     Integer[] empty = new Integer[0];
236 dl 1.3 Integer[] ints = new Integer[SIZE];
237     for (int i = 0; i < SIZE; ++i)
238 dl 1.1 ints[i] = new Integer(i);
239 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
240 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
241     assertTrue(q.addAll(Arrays.asList(ints)));
242 dl 1.3 for (int i = 0; i < SIZE; ++i)
243 dl 1.1 assertEquals(ints[i], q.poll());
244     }
245     finally {}
246     }
247    
248 dl 1.4 /**
249 dl 1.5 * put(null) throws NPE
250 dl 1.4 */
251 dl 1.1 public void testPutNull() {
252     try {
253 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
254 dl 1.1 q.put(null);
255 dl 1.4 shouldThrow();
256 dl 1.1 }
257     catch (NullPointerException success){
258     }
259     catch (InterruptedException ie) {
260 dl 1.4 unexpectedException();
261 dl 1.1 }
262     }
263    
264 dl 1.4 /**
265 dl 1.5 * all elements successfully put are contained
266 dl 1.4 */
267 dl 1.1 public void testPut() {
268     try {
269 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
270     for (int i = 0; i < SIZE; ++i) {
271 dl 1.1 Integer I = new Integer(i);
272     q.put(I);
273     assertTrue(q.contains(I));
274     }
275     assertEquals(0, q.remainingCapacity());
276     }
277     catch (InterruptedException ie) {
278 dl 1.4 unexpectedException();
279 dl 1.1 }
280     }
281    
282 dl 1.4 /**
283 dl 1.5 * put blocks interruptibly if full
284 dl 1.4 */
285     public void testBlockingPut() {
286 dl 1.1 Thread t = new Thread(new Runnable() {
287     public void run() {
288     int added = 0;
289     try {
290 dl 1.3 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
291     for (int i = 0; i < SIZE; ++i) {
292 dl 1.1 q.put(new Integer(i));
293     ++added;
294     }
295 dl 1.3 q.put(new Integer(SIZE));
296 dl 1.4 threadShouldThrow();
297 dl 1.1 } catch (InterruptedException ie){
298 dl 1.3 threadAssertEquals(added, SIZE);
299 dl 1.1 }
300     }});
301     t.start();
302     try {
303     Thread.sleep(SHORT_DELAY_MS);
304     t.interrupt();
305     t.join();
306     }
307     catch (InterruptedException ie) {
308 dl 1.4 unexpectedException();
309 dl 1.1 }
310     }
311    
312 dl 1.4 /**
313 dl 1.5 * put blocks waiting for take when full
314 dl 1.4 */
315 dl 1.1 public void testPutWithTake() {
316     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
317     Thread t = new Thread(new Runnable() {
318 dl 1.4 public void run() {
319 dl 1.1 int added = 0;
320     try {
321     q.put(new Object());
322     ++added;
323     q.put(new Object());
324     ++added;
325     q.put(new Object());
326     ++added;
327     q.put(new Object());
328     ++added;
329 dl 1.4 threadShouldThrow();
330 dl 1.1 } catch (InterruptedException e){
331 dl 1.3 threadAssertTrue(added >= 2);
332 dl 1.1 }
333     }
334     });
335     try {
336     t.start();
337     Thread.sleep(SHORT_DELAY_MS);
338     q.take();
339     t.interrupt();
340     t.join();
341     } catch (Exception e){
342 dl 1.4 unexpectedException();
343 dl 1.1 }
344     }
345    
346 dl 1.4 /**
347 dl 1.5 * timed offer times out if full and elements not taken
348 dl 1.4 */
349 dl 1.1 public void testTimedOffer() {
350     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
351     Thread t = new Thread(new Runnable() {
352 dl 1.4 public void run() {
353 dl 1.1 try {
354     q.put(new Object());
355     q.put(new Object());
356 dl 1.3 threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
357 dl 1.1 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
358 dl 1.4 threadShouldThrow();
359 dl 1.1 } catch (InterruptedException success){}
360     }
361     });
362    
363     try {
364     t.start();
365 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
366 dl 1.1 t.interrupt();
367     t.join();
368     } catch (Exception e){
369 dl 1.4 unexpectedException();
370 dl 1.1 }
371     }
372    
373 dl 1.4 /**
374 dl 1.5 * take retrieves elements in FIFO order
375 dl 1.4 */
376     public void testTake() {
377 dl 1.1 try {
378 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
379     for (int i = 0; i < SIZE; ++i) {
380 dl 1.1 assertEquals(i, ((Integer)q.take()).intValue());
381     }
382     } catch (InterruptedException e){
383 dl 1.4 unexpectedException();
384 dl 1.1 }
385     }
386    
387 dl 1.4 /**
388 dl 1.5 * take blocks interruptibly when empty
389 dl 1.4 */
390 dl 1.1 public void testTakeFromEmpty() {
391     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
392     Thread t = new Thread(new Runnable() {
393 dl 1.4 public void run() {
394 dl 1.1 try {
395     q.take();
396 dl 1.4 threadShouldThrow();
397 dl 1.1 } catch (InterruptedException success){ }
398     }
399     });
400     try {
401     t.start();
402     Thread.sleep(SHORT_DELAY_MS);
403     t.interrupt();
404     t.join();
405     } catch (Exception e){
406 dl 1.4 unexpectedException();
407 dl 1.1 }
408     }
409    
410 dl 1.4 /**
411 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
412 dl 1.4 */
413     public void testBlockingTake() {
414 dl 1.1 Thread t = new Thread(new Runnable() {
415     public void run() {
416     try {
417 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
418     for (int i = 0; i < SIZE; ++i) {
419 dl 1.1 assertEquals(i, ((Integer)q.take()).intValue());
420     }
421     q.take();
422 dl 1.4 threadShouldThrow();
423 dl 1.1 } catch (InterruptedException success){
424     }
425     }});
426     t.start();
427     try {
428     Thread.sleep(SHORT_DELAY_MS);
429     t.interrupt();
430     t.join();
431     }
432     catch (InterruptedException ie) {
433 dl 1.4 unexpectedException();
434 dl 1.1 }
435     }
436    
437    
438 dl 1.4 /**
439 dl 1.5 * poll succeeds unless empty
440 dl 1.4 */
441     public void testPoll() {
442 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
443     for (int i = 0; i < SIZE; ++i) {
444 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
445     }
446     assertNull(q.poll());
447     }
448    
449 dl 1.4 /**
450 dl 1.5 * timed pool with zero timeout succeeds when non-empty, else times out
451 dl 1.4 */
452 dl 1.1 public void testTimedPoll0() {
453     try {
454 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
455     for (int i = 0; i < SIZE; ++i) {
456 dl 1.1 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
457     }
458     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
459     } catch (InterruptedException e){
460 dl 1.4 unexpectedException();
461 dl 1.1 }
462     }
463    
464 dl 1.4 /**
465 dl 1.5 * timed pool with nonzero timeout succeeds when non-empty, else times out
466 dl 1.4 */
467 dl 1.1 public void testTimedPoll() {
468     try {
469 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
470     for (int i = 0; i < SIZE; ++i) {
471 dl 1.1 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
472     }
473     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
474     } catch (InterruptedException e){
475 dl 1.4 unexpectedException();
476 dl 1.1 }
477     }
478    
479 dl 1.4 /**
480 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
481     * returning timeout status
482 dl 1.4 */
483     public void testInterruptedTimedPoll() {
484 dl 1.1 Thread t = new Thread(new Runnable() {
485     public void run() {
486     try {
487 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
488     for (int i = 0; i < SIZE; ++i) {
489     threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
490 dl 1.1 }
491 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
492 dl 1.1 } catch (InterruptedException success){
493     }
494     }});
495     t.start();
496     try {
497     Thread.sleep(SHORT_DELAY_MS);
498     t.interrupt();
499     t.join();
500     }
501     catch (InterruptedException ie) {
502 dl 1.4 unexpectedException();
503 dl 1.1 }
504     }
505    
506 dl 1.4 /**
507 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
508     * on interruption throws
509 dl 1.4 */
510     public void testTimedPollWithOffer() {
511 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
512     Thread t = new Thread(new Runnable() {
513 dl 1.4 public void run() {
514 dl 1.1 try {
515 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
517     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
518 dl 1.4 threadShouldThrow();
519 dl 1.1 } catch (InterruptedException success) { }
520     }
521     });
522     try {
523     t.start();
524 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
525     assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526 dl 1.1 t.interrupt();
527     t.join();
528     } catch (Exception e){
529 dl 1.4 unexpectedException();
530 dl 1.1 }
531     }
532    
533 dl 1.4 /**
534 dl 1.5 * peek returns next element, or null if empty
535 dl 1.4 */
536     public void testPeek() {
537 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
538     for (int i = 0; i < SIZE; ++i) {
539 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
540     q.poll();
541     assertTrue(q.peek() == null ||
542     i != ((Integer)q.peek()).intValue());
543     }
544     assertNull(q.peek());
545     }
546    
547 dl 1.4 /**
548 dl 1.5 * element returns next element, or throws NSEE if empty
549 dl 1.4 */
550     public void testElement() {
551 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
552     for (int i = 0; i < SIZE; ++i) {
553 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
554     q.poll();
555     }
556     try {
557     q.element();
558 dl 1.4 shouldThrow();
559 dl 1.1 }
560     catch (NoSuchElementException success) {}
561     }
562    
563 dl 1.4 /**
564 dl 1.5 * remove removes next element, or throws NSEE if empty
565 dl 1.4 */
566     public void testRemove() {
567 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
568     for (int i = 0; i < SIZE; ++i) {
569 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
570     }
571     try {
572     q.remove();
573 dl 1.4 shouldThrow();
574 dl 1.1 } catch (NoSuchElementException success){
575     }
576     }
577    
578 dl 1.4 /**
579 dl 1.5 * remove(x) removes x and returns true if present
580 dl 1.4 */
581     public void testRemoveElement() {
582 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
583     for (int i = 1; i < SIZE; i+=2) {
584 dl 1.1 assertTrue(q.remove(new Integer(i)));
585     }
586 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
587 dl 1.1 assertTrue(q.remove(new Integer(i)));
588     assertFalse(q.remove(new Integer(i+1)));
589     }
590 dl 1.2 assertTrue(q.isEmpty());
591 dl 1.1 }
592    
593 dl 1.4 /**
594 dl 1.5 * contains(x) reports true when elements added but not yet removed
595 dl 1.4 */
596     public void testContains() {
597 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
598     for (int i = 0; i < SIZE; ++i) {
599 dl 1.1 assertTrue(q.contains(new Integer(i)));
600     q.poll();
601     assertFalse(q.contains(new Integer(i)));
602     }
603     }
604    
605 dl 1.4 /**
606 dl 1.5 * clear removes all elements
607 dl 1.4 */
608     public void testClear() {
609 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
610 dl 1.1 q.clear();
611     assertTrue(q.isEmpty());
612     assertEquals(0, q.size());
613 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
614     q.add(one);
615 dl 1.1 assertFalse(q.isEmpty());
616     q.clear();
617     assertTrue(q.isEmpty());
618     }
619    
620 dl 1.4 /**
621 dl 1.5 * containsAll(c) is true when c contains a subset of elements
622 dl 1.4 */
623     public void testContainsAll() {
624 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
625     LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
626     for (int i = 0; i < SIZE; ++i) {
627 dl 1.1 assertTrue(q.containsAll(p));
628     assertFalse(p.containsAll(q));
629     p.add(new Integer(i));
630     }
631     assertTrue(p.containsAll(q));
632     }
633    
634 dl 1.4 /**
635 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
636 dl 1.4 */
637     public void testRetainAll() {
638 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
639     LinkedBlockingQueue p = populatedQueue(SIZE);
640     for (int i = 0; i < SIZE; ++i) {
641 dl 1.1 boolean changed = q.retainAll(p);
642     if (i == 0)
643     assertFalse(changed);
644     else
645     assertTrue(changed);
646    
647     assertTrue(q.containsAll(p));
648 dl 1.3 assertEquals(SIZE-i, q.size());
649 dl 1.1 p.remove();
650     }
651     }
652    
653 dl 1.4 /**
654 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
655 dl 1.4 */
656     public void testRemoveAll() {
657 dl 1.3 for (int i = 1; i < SIZE; ++i) {
658     LinkedBlockingQueue q = populatedQueue(SIZE);
659     LinkedBlockingQueue p = populatedQueue(i);
660 dl 1.1 assertTrue(q.removeAll(p));
661 dl 1.3 assertEquals(SIZE-i, q.size());
662 dl 1.1 for (int j = 0; j < i; ++j) {
663     Integer I = (Integer)(p.remove());
664     assertFalse(q.contains(I));
665     }
666     }
667     }
668    
669 dl 1.4 /**
670 dl 1.5 * toArray contains all elements
671 dl 1.4 */
672     public void testToArray() {
673 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
674 dl 1.1 Object[] o = q.toArray();
675     try {
676     for(int i = 0; i < o.length; i++)
677     assertEquals(o[i], q.take());
678     } catch (InterruptedException e){
679 dl 1.4 unexpectedException();
680 dl 1.1 }
681     }
682    
683 dl 1.4 /**
684 dl 1.5 * toArray(a) contains all elements
685 dl 1.4 */
686     public void testToArray2() {
687 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
688     Integer[] ints = new Integer[SIZE];
689 dl 1.1 ints = (Integer[])q.toArray(ints);
690     try {
691     for(int i = 0; i < ints.length; i++)
692     assertEquals(ints[i], q.take());
693     } catch (InterruptedException e){
694 dl 1.4 unexpectedException();
695 dl 1.1 }
696     }
697    
698 dl 1.4 /**
699 dl 1.5 * iterator iterates through all elements
700 dl 1.4 */
701     public void testIterator() {
702 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
703 dl 1.1 Iterator it = q.iterator();
704     try {
705     while(it.hasNext()){
706     assertEquals(it.next(), q.take());
707     }
708     } catch (InterruptedException e){
709 dl 1.4 unexpectedException();
710 dl 1.1 }
711     }
712    
713 dl 1.4 /**
714 dl 1.5 * iterator.remove removes current element
715     */
716     public void testIteratorRemove () {
717     final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
718     q.add(two);
719     q.add(one);
720     q.add(three);
721    
722     Iterator it = q.iterator();
723     it.next();
724     it.remove();
725    
726     it = q.iterator();
727     assertEquals(it.next(), one);
728     assertEquals(it.next(), three);
729     assertFalse(it.hasNext());
730     }
731    
732    
733     /**
734     * iterator ordering is FIFO
735 dl 1.4 */
736 dl 1.1 public void testIteratorOrdering() {
737     final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
738 dl 1.3 q.add(one);
739     q.add(two);
740     q.add(three);
741 dl 1.4 assertEquals(0, q.remainingCapacity());
742 dl 1.1 int k = 0;
743     for (Iterator it = q.iterator(); it.hasNext();) {
744     int i = ((Integer)(it.next())).intValue();
745 dl 1.4 assertEquals(++k, i);
746 dl 1.1 }
747 dl 1.4 assertEquals(3, k);
748 dl 1.1 }
749    
750 dl 1.4 /**
751 dl 1.5 * Modifications do not cause iterators to fail
752 dl 1.4 */
753 dl 1.1 public void testWeaklyConsistentIteration () {
754     final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
755 dl 1.3 q.add(one);
756     q.add(two);
757     q.add(three);
758 dl 1.1 try {
759     for (Iterator it = q.iterator(); it.hasNext();) {
760     q.remove();
761     it.next();
762     }
763     }
764     catch (ConcurrentModificationException e) {
765 dl 1.4 unexpectedException();
766 dl 1.1 }
767 dl 1.4 assertEquals(0, q.size());
768 dl 1.1 }
769    
770    
771 dl 1.4 /**
772 dl 1.5 * toString contains toStrings of elements
773 dl 1.4 */
774     public void testToString() {
775 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
776 dl 1.1 String s = q.toString();
777 dl 1.3 for (int i = 0; i < SIZE; ++i) {
778 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
779     }
780     }
781    
782    
783 dl 1.4 /**
784 dl 1.5 * offer transfers elements across Executor tasks
785 dl 1.4 */
786 dl 1.1 public void testOfferInExecutor() {
787     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
788 dl 1.3 q.add(one);
789     q.add(two);
790 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
791     executor.execute(new Runnable() {
792     public void run() {
793 dl 1.3 threadAssertFalse(q.offer(three));
794 dl 1.1 try {
795 dl 1.3 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
796     threadAssertEquals(0, q.remainingCapacity());
797 dl 1.1 }
798     catch (InterruptedException e) {
799 dl 1.4 threadUnexpectedException();
800 dl 1.1 }
801     }
802     });
803    
804     executor.execute(new Runnable() {
805     public void run() {
806     try {
807 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
808     threadAssertEquals(one, q.take());
809 dl 1.1 }
810     catch (InterruptedException e) {
811 dl 1.4 threadUnexpectedException();
812 dl 1.1 }
813     }
814     });
815    
816 dl 1.3 joinPool(executor);
817 dl 1.1 }
818    
819 dl 1.4 /**
820 dl 1.5 * poll retrieves elements across Executor threads
821 dl 1.4 */
822 dl 1.1 public void testPollInExecutor() {
823     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
824     ExecutorService executor = Executors.newFixedThreadPool(2);
825     executor.execute(new Runnable() {
826     public void run() {
827 dl 1.3 threadAssertNull(q.poll());
828 dl 1.1 try {
829 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
830     threadAssertTrue(q.isEmpty());
831 dl 1.1 }
832     catch (InterruptedException e) {
833 dl 1.4 threadUnexpectedException();
834 dl 1.1 }
835     }
836     });
837    
838     executor.execute(new Runnable() {
839     public void run() {
840     try {
841 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
842     q.put(one);
843 dl 1.1 }
844     catch (InterruptedException e) {
845 dl 1.4 threadUnexpectedException();
846 dl 1.1 }
847     }
848     });
849    
850 dl 1.3 joinPool(executor);
851 dl 1.2 }
852    
853 dl 1.4 /**
854 dl 1.5 * A deserialized serialized queue has same elements in same order
855 dl 1.4 */
856 dl 1.2 public void testSerialization() {
857 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
858 dl 1.2
859     try {
860     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
861     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
862     out.writeObject(q);
863     out.close();
864    
865     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
866     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
867     LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
868     assertEquals(q.size(), r.size());
869     while (!q.isEmpty())
870     assertEquals(q.remove(), r.remove());
871     } catch(Exception e){
872 dl 1.4 unexpectedException();
873 dl 1.2 }
874 dl 1.1 }
875    
876     }