ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.32
Committed: Thu Oct 28 17:57:26 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +0 -16 lines
Log Message:
refactor testTakeFromEmpty into BlockingQueueTest

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