ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
Revision: 1.19
Committed: Sat Nov 21 19:11:53 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +7 -4 lines
Log Message:
reduce scope of check for IE in testInterruptedTimedPoll*

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