ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.12
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +48 -48 lines
Log Message:
whitespace

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