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