ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.31
Committed: Tue Oct 19 00:43:49 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +1 -1 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 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 blocks interruptibly when empty
385 dl 1.4 */
386 jsr166 1.17 public void testTakeFromEmpty() throws InterruptedException {
387 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
388 jsr166 1.17 Thread t = new ThreadShouldThrow(InterruptedException.class) {
389     public void realRun() throws InterruptedException {
390     q.take();
391     }};
392    
393     t.start();
394     Thread.sleep(SHORT_DELAY_MS);
395     t.interrupt();
396     t.join();
397 dl 1.1 }
398    
399 dl 1.4 /**
400 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
401 dl 1.4 */
402 jsr166 1.17 public void testBlockingTake() throws InterruptedException {
403 jsr166 1.22 final LinkedBlockingQueue q = populatedQueue(SIZE);
404     Thread t = new Thread(new CheckedRunnable() {
405 jsr166 1.17 public void realRun() throws InterruptedException {
406     for (int i = 0; i < SIZE; ++i) {
407 jsr166 1.23 assertEquals(i, q.take());
408 jsr166 1.17 }
409 jsr166 1.22 try {
410     q.take();
411     shouldThrow();
412     } catch (InterruptedException success) {}
413     }});
414 jsr166 1.17
415 dl 1.1 t.start();
416 jsr166 1.17 Thread.sleep(SHORT_DELAY_MS);
417     t.interrupt();
418     t.join();
419 dl 1.1 }
420    
421 dl 1.4 /**
422 dl 1.5 * poll succeeds unless empty
423 dl 1.4 */
424     public void testPoll() {
425 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
426     for (int i = 0; i < SIZE; ++i) {
427 jsr166 1.23 assertEquals(i, q.poll());
428 dl 1.1 }
429 jsr166 1.15 assertNull(q.poll());
430 dl 1.1 }
431    
432 dl 1.4 /**
433 dl 1.5 * timed pool with zero timeout succeeds when non-empty, else times out
434 dl 1.4 */
435 jsr166 1.17 public void testTimedPoll0() throws InterruptedException {
436     LinkedBlockingQueue q = populatedQueue(SIZE);
437     for (int i = 0; i < SIZE; ++i) {
438 jsr166 1.23 assertEquals(i, q.poll(0, MILLISECONDS));
439 jsr166 1.15 }
440 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
441 dl 1.1 }
442    
443 dl 1.4 /**
444 dl 1.5 * timed pool with nonzero timeout succeeds when non-empty, else times out
445 dl 1.4 */
446 jsr166 1.17 public void testTimedPoll() throws InterruptedException {
447     LinkedBlockingQueue q = populatedQueue(SIZE);
448     for (int i = 0; i < SIZE; ++i) {
449 jsr166 1.23 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450 jsr166 1.15 }
451 jsr166 1.17 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
452 dl 1.1 }
453    
454 dl 1.4 /**
455 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
456     * returning timeout status
457 dl 1.4 */
458 jsr166 1.17 public void testInterruptedTimedPoll() throws InterruptedException {
459 jsr166 1.19 Thread t = new Thread(new CheckedRunnable() {
460 jsr166 1.17 public void realRun() throws InterruptedException {
461     LinkedBlockingQueue q = populatedQueue(SIZE);
462     for (int i = 0; i < SIZE; ++i) {
463 jsr166 1.23 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
464 jsr166 1.17 }
465 jsr166 1.19 try {
466     q.poll(SMALL_DELAY_MS, MILLISECONDS);
467     shouldThrow();
468     } catch (InterruptedException success) {}
469     }});
470 jsr166 1.17
471 dl 1.1 t.start();
472 jsr166 1.17 Thread.sleep(SHORT_DELAY_MS);
473     t.interrupt();
474     t.join();
475 dl 1.1 }
476    
477 dl 1.4 /**
478 dl 1.5 * peek returns next element, or null if empty
479 dl 1.4 */
480     public void testPeek() {
481 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
482     for (int i = 0; i < SIZE; ++i) {
483 jsr166 1.23 assertEquals(i, q.peek());
484     assertEquals(i, q.poll());
485 dl 1.1 assertTrue(q.peek() == null ||
486 jsr166 1.23 !q.peek().equals(i));
487 dl 1.1 }
488 jsr166 1.15 assertNull(q.peek());
489 dl 1.1 }
490    
491 dl 1.4 /**
492 dl 1.5 * element returns next element, or throws NSEE if empty
493 dl 1.4 */
494     public void testElement() {
495 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
496     for (int i = 0; i < SIZE; ++i) {
497 jsr166 1.23 assertEquals(i, q.element());
498     assertEquals(i, q.poll());
499 dl 1.1 }
500     try {
501     q.element();
502 dl 1.4 shouldThrow();
503 jsr166 1.17 } catch (NoSuchElementException success) {}
504 dl 1.1 }
505    
506 dl 1.4 /**
507 dl 1.5 * remove removes next element, or throws NSEE if empty
508 dl 1.4 */
509     public void testRemove() {
510 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
511     for (int i = 0; i < SIZE; ++i) {
512 jsr166 1.23 assertEquals(i, q.remove());
513 dl 1.1 }
514     try {
515     q.remove();
516 dl 1.4 shouldThrow();
517 jsr166 1.17 } catch (NoSuchElementException success) {}
518 dl 1.1 }
519    
520 dl 1.4 /**
521 dl 1.5 * remove(x) removes x and returns true if present
522 dl 1.4 */
523     public void testRemoveElement() {
524 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
525     for (int i = 1; i < SIZE; i+=2) {
526 dl 1.1 assertTrue(q.remove(new Integer(i)));
527     }
528 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
529 dl 1.1 assertTrue(q.remove(new Integer(i)));
530     assertFalse(q.remove(new Integer(i+1)));
531     }
532 dl 1.2 assertTrue(q.isEmpty());
533 dl 1.1 }
534 dl 1.11
535     /**
536     * An add following remove(x) succeeds
537     */
538 jsr166 1.17 public void testRemoveElementAndAdd() throws InterruptedException {
539     LinkedBlockingQueue q = new LinkedBlockingQueue();
540     assertTrue(q.add(new Integer(1)));
541     assertTrue(q.add(new Integer(2)));
542     assertTrue(q.remove(new Integer(1)));
543     assertTrue(q.remove(new Integer(2)));
544     assertTrue(q.add(new Integer(3)));
545     assertTrue(q.take() != null);
546 dl 1.11 }
547 jsr166 1.12
548 dl 1.4 /**
549 dl 1.5 * contains(x) reports true when elements added but not yet removed
550 dl 1.4 */
551     public void testContains() {
552 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
553     for (int i = 0; i < SIZE; ++i) {
554 dl 1.1 assertTrue(q.contains(new Integer(i)));
555     q.poll();
556     assertFalse(q.contains(new Integer(i)));
557     }
558     }
559    
560 dl 1.4 /**
561 dl 1.5 * clear removes all elements
562 dl 1.4 */
563     public void testClear() {
564 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
565 dl 1.1 q.clear();
566     assertTrue(q.isEmpty());
567     assertEquals(0, q.size());
568 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
569     q.add(one);
570 dl 1.1 assertFalse(q.isEmpty());
571 dl 1.10 assertTrue(q.contains(one));
572 dl 1.1 q.clear();
573     assertTrue(q.isEmpty());
574     }
575    
576 dl 1.4 /**
577 dl 1.5 * containsAll(c) is true when c contains a subset of elements
578 dl 1.4 */
579     public void testContainsAll() {
580 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
581     LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
582     for (int i = 0; i < SIZE; ++i) {
583 dl 1.1 assertTrue(q.containsAll(p));
584     assertFalse(p.containsAll(q));
585     p.add(new Integer(i));
586     }
587     assertTrue(p.containsAll(q));
588     }
589    
590 dl 1.4 /**
591 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
592 dl 1.4 */
593     public void testRetainAll() {
594 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
595     LinkedBlockingQueue p = populatedQueue(SIZE);
596     for (int i = 0; i < SIZE; ++i) {
597 dl 1.1 boolean changed = q.retainAll(p);
598     if (i == 0)
599     assertFalse(changed);
600     else
601     assertTrue(changed);
602    
603     assertTrue(q.containsAll(p));
604 dl 1.3 assertEquals(SIZE-i, q.size());
605 dl 1.1 p.remove();
606     }
607     }
608    
609 dl 1.4 /**
610 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
611 dl 1.4 */
612     public void testRemoveAll() {
613 dl 1.3 for (int i = 1; i < SIZE; ++i) {
614     LinkedBlockingQueue q = populatedQueue(SIZE);
615     LinkedBlockingQueue p = populatedQueue(i);
616 dl 1.1 assertTrue(q.removeAll(p));
617 dl 1.3 assertEquals(SIZE-i, q.size());
618 dl 1.1 for (int j = 0; j < i; ++j) {
619     Integer I = (Integer)(p.remove());
620     assertFalse(q.contains(I));
621     }
622     }
623     }
624    
625 dl 1.4 /**
626 dl 1.5 * toArray contains all elements
627 dl 1.4 */
628 jsr166 1.17 public void testToArray() throws InterruptedException {
629 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
630 jsr166 1.15 Object[] o = q.toArray();
631     for (int i = 0; i < o.length; i++)
632     assertEquals(o[i], q.take());
633 dl 1.1 }
634    
635 dl 1.4 /**
636 dl 1.5 * toArray(a) contains all elements
637 dl 1.4 */
638 jsr166 1.17 public void testToArray2() throws InterruptedException {
639 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
640 jsr166 1.15 Integer[] ints = new Integer[SIZE];
641     ints = (Integer[])q.toArray(ints);
642 jsr166 1.17 for (int i = 0; i < ints.length; i++)
643     assertEquals(ints[i], q.take());
644 dl 1.1 }
645 dl 1.6
646     /**
647     * toArray(null) throws NPE
648     */
649     public void testToArray_BadArg() {
650 jsr166 1.23 LinkedBlockingQueue q = populatedQueue(SIZE);
651 jsr166 1.15 try {
652     Object o[] = q.toArray(null);
653     shouldThrow();
654     } catch (NullPointerException success) {}
655 dl 1.6 }
656    
657     /**
658 dl 1.8 * toArray with incompatible array type throws CCE
659 dl 1.6 */
660     public void testToArray1_BadArg() {
661 jsr166 1.23 LinkedBlockingQueue q = populatedQueue(SIZE);
662 jsr166 1.15 try {
663 jsr166 1.23 Object o[] = q.toArray(new String[10]);
664 jsr166 1.15 shouldThrow();
665 jsr166 1.17 } catch (ArrayStoreException success) {}
666 dl 1.6 }
667    
668 jsr166 1.12
669 dl 1.4 /**
670 dl 1.5 * iterator iterates through all elements
671 dl 1.4 */
672 jsr166 1.17 public void testIterator() throws InterruptedException {
673 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
674 jsr166 1.15 Iterator it = q.iterator();
675 jsr166 1.17 while (it.hasNext()) {
676     assertEquals(it.next(), q.take());
677 jsr166 1.15 }
678 dl 1.1 }
679    
680 dl 1.4 /**
681 dl 1.5 * iterator.remove removes current element
682     */
683 jsr166 1.26 public void testIteratorRemove() {
684 dl 1.5 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
685     q.add(two);
686     q.add(one);
687     q.add(three);
688    
689     Iterator it = q.iterator();
690     it.next();
691     it.remove();
692 jsr166 1.12
693 dl 1.5 it = q.iterator();
694 jsr166 1.25 assertSame(it.next(), one);
695     assertSame(it.next(), three);
696 dl 1.5 assertFalse(it.hasNext());
697     }
698    
699    
700     /**
701     * iterator ordering is FIFO
702 dl 1.4 */
703 dl 1.1 public void testIteratorOrdering() {
704     final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
705 dl 1.3 q.add(one);
706     q.add(two);
707     q.add(three);
708 dl 1.4 assertEquals(0, q.remainingCapacity());
709 dl 1.1 int k = 0;
710     for (Iterator it = q.iterator(); it.hasNext();) {
711 jsr166 1.23 assertEquals(++k, it.next());
712 dl 1.1 }
713 dl 1.4 assertEquals(3, k);
714 dl 1.1 }
715    
716 dl 1.4 /**
717 dl 1.5 * Modifications do not cause iterators to fail
718 dl 1.4 */
719 jsr166 1.26 public void testWeaklyConsistentIteration() {
720 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
721 dl 1.3 q.add(one);
722     q.add(two);
723     q.add(three);
724 jsr166 1.17 for (Iterator it = q.iterator(); it.hasNext();) {
725     q.remove();
726     it.next();
727 dl 1.1 }
728 dl 1.4 assertEquals(0, q.size());
729 dl 1.1 }
730    
731    
732 dl 1.4 /**
733 dl 1.5 * toString contains toStrings of elements
734 dl 1.4 */
735     public void testToString() {
736 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
737 dl 1.1 String s = q.toString();
738 dl 1.3 for (int i = 0; i < SIZE; ++i) {
739 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
740     }
741 jsr166 1.12 }
742 dl 1.1
743    
744 dl 1.4 /**
745 dl 1.5 * offer transfers elements across Executor tasks
746 dl 1.4 */
747 dl 1.1 public void testOfferInExecutor() {
748     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
749 dl 1.3 q.add(one);
750     q.add(two);
751 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
752 jsr166 1.17 executor.execute(new CheckedRunnable() {
753     public void realRun() throws InterruptedException {
754 jsr166 1.24 assertFalse(q.offer(three));
755     assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
756     assertEquals(0, q.remainingCapacity());
757 jsr166 1.17 }});
758    
759     executor.execute(new CheckedRunnable() {
760     public void realRun() throws InterruptedException {
761     Thread.sleep(SMALL_DELAY_MS);
762 jsr166 1.24 assertSame(one, q.take());
763 jsr166 1.17 }});
764 jsr166 1.12
765 dl 1.3 joinPool(executor);
766 dl 1.1 }
767    
768 dl 1.4 /**
769 dl 1.5 * poll retrieves elements across Executor threads
770 dl 1.4 */
771 dl 1.1 public void testPollInExecutor() {
772     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
773     ExecutorService executor = Executors.newFixedThreadPool(2);
774 jsr166 1.17 executor.execute(new CheckedRunnable() {
775     public void realRun() throws InterruptedException {
776 jsr166 1.24 assertNull(q.poll());
777     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
778     assertTrue(q.isEmpty());
779 jsr166 1.17 }});
780    
781     executor.execute(new CheckedRunnable() {
782     public void realRun() throws InterruptedException {
783     Thread.sleep(SMALL_DELAY_MS);
784     q.put(one);
785     }});
786 jsr166 1.12
787 dl 1.3 joinPool(executor);
788 dl 1.2 }
789    
790 dl 1.4 /**
791 dl 1.5 * A deserialized serialized queue has same elements in same order
792 dl 1.4 */
793 jsr166 1.17 public void testSerialization() throws Exception {
794 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
795 dl 1.2
796 jsr166 1.17 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
797     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
798     out.writeObject(q);
799     out.close();
800    
801     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
802     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
803     LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject();
804     assertEquals(q.size(), r.size());
805     while (!q.isEmpty())
806     assertEquals(q.remove(), r.remove());
807 dl 1.6 }
808    
809     /**
810     * drainTo(null) throws NPE
811 jsr166 1.12 */
812 dl 1.6 public void testDrainToNull() {
813     LinkedBlockingQueue q = populatedQueue(SIZE);
814     try {
815     q.drainTo(null);
816     shouldThrow();
817 jsr166 1.17 } catch (NullPointerException success) {}
818 dl 1.6 }
819    
820     /**
821     * drainTo(this) throws IAE
822 jsr166 1.12 */
823 dl 1.6 public void testDrainToSelf() {
824     LinkedBlockingQueue q = populatedQueue(SIZE);
825     try {
826     q.drainTo(q);
827     shouldThrow();
828 jsr166 1.17 } catch (IllegalArgumentException success) {}
829 dl 1.6 }
830    
831     /**
832     * drainTo(c) empties queue into another collection c
833 jsr166 1.12 */
834 dl 1.6 public void testDrainTo() {
835     LinkedBlockingQueue q = populatedQueue(SIZE);
836     ArrayList l = new ArrayList();
837     q.drainTo(l);
838     assertEquals(q.size(), 0);
839     assertEquals(l.size(), SIZE);
840 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
841 dl 1.6 assertEquals(l.get(i), new Integer(i));
842 dl 1.10 q.add(zero);
843     q.add(one);
844     assertFalse(q.isEmpty());
845     assertTrue(q.contains(zero));
846     assertTrue(q.contains(one));
847     l.clear();
848     q.drainTo(l);
849     assertEquals(q.size(), 0);
850     assertEquals(l.size(), 2);
851 jsr166 1.12 for (int i = 0; i < 2; ++i)
852 dl 1.10 assertEquals(l.get(i), new Integer(i));
853 dl 1.6 }
854    
855     /**
856     * drainTo empties full queue, unblocking a waiting put.
857 jsr166 1.12 */
858 jsr166 1.17 public void testDrainToWithActivePut() throws InterruptedException {
859 dl 1.6 final LinkedBlockingQueue q = populatedQueue(SIZE);
860 jsr166 1.17 Thread t = new Thread(new CheckedRunnable() {
861     public void realRun() throws InterruptedException {
862     q.put(new Integer(SIZE+1));
863     }});
864    
865     t.start();
866     ArrayList l = new ArrayList();
867     q.drainTo(l);
868     assertTrue(l.size() >= SIZE);
869     for (int i = 0; i < SIZE; ++i)
870     assertEquals(l.get(i), new Integer(i));
871     t.join();
872     assertTrue(q.size() + l.size() >= SIZE);
873 dl 1.6 }
874    
875     /**
876     * drainTo(null, n) throws NPE
877 jsr166 1.12 */
878 dl 1.6 public void testDrainToNullN() {
879     LinkedBlockingQueue q = populatedQueue(SIZE);
880     try {
881     q.drainTo(null, 0);
882     shouldThrow();
883 jsr166 1.17 } catch (NullPointerException success) {}
884 dl 1.6 }
885    
886     /**
887     * drainTo(this, n) throws IAE
888 jsr166 1.12 */
889 dl 1.6 public void testDrainToSelfN() {
890     LinkedBlockingQueue q = populatedQueue(SIZE);
891     try {
892     q.drainTo(q, 0);
893     shouldThrow();
894 jsr166 1.17 } catch (IllegalArgumentException success) {}
895 dl 1.6 }
896    
897     /**
898 jsr166 1.30 * drainTo(c, n) empties first min(n, size) elements of queue into c
899 jsr166 1.12 */
900 dl 1.6 public void testDrainToN() {
901 dl 1.10 LinkedBlockingQueue q = new LinkedBlockingQueue();
902 dl 1.6 for (int i = 0; i < SIZE + 2; ++i) {
903 jsr166 1.13 for (int j = 0; j < SIZE; j++)
904 dl 1.10 assertTrue(q.offer(new Integer(j)));
905 dl 1.6 ArrayList l = new ArrayList();
906     q.drainTo(l, i);
907 jsr166 1.31 int k = (i < SIZE) ? i : SIZE;
908 dl 1.10 assertEquals(l.size(), k);
909 dl 1.6 assertEquals(q.size(), SIZE-k);
910 jsr166 1.12 for (int j = 0; j < k; ++j)
911 dl 1.6 assertEquals(l.get(j), new Integer(j));
912 dl 1.10 while (q.poll() != null) ;
913 dl 1.2 }
914 dl 1.1 }
915    
916     }