ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.39
Committed: Fri May 27 20:07:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +340 -128 lines
Log Message:
performance and robustness improvements to queue tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * 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 jsr166 1.35 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import junit.framework.*;
8     import java.util.*;
9     import java.util.concurrent.*;
10 jsr166 1.8 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 dl 1.1 import java.io.*;
12    
13     public class LinkedBlockingDequeTest extends JSR166TestCase {
14 jsr166 1.24
15     public static class Unbounded extends BlockingQueueTest {
16     protected BlockingQueue emptyCollection() {
17     return new LinkedBlockingDeque();
18     }
19     }
20    
21     public static class Bounded extends BlockingQueueTest {
22     protected BlockingQueue emptyCollection() {
23     return new LinkedBlockingDeque(20);
24     }
25     }
26    
27 dl 1.1 public static void main(String[] args) {
28 jsr166 1.21 junit.textui.TestRunner.run(suite());
29 dl 1.1 }
30    
31     public static Test suite() {
32 jsr166 1.24 return newTestSuite(LinkedBlockingDequeTest.class,
33     new Unbounded().testSuite(),
34     new Bounded().testSuite());
35 dl 1.1 }
36    
37     /**
38     * Create a deque of given size containing consecutive
39     * Integers 0 ... n.
40     */
41 jsr166 1.32 private LinkedBlockingDeque<Integer> populatedDeque(int n) {
42     LinkedBlockingDeque<Integer> q =
43     new LinkedBlockingDeque<Integer>(n);
44 dl 1.1 assertTrue(q.isEmpty());
45 jsr166 1.7 for (int i = 0; i < n; i++)
46     assertTrue(q.offer(new Integer(i)));
47 dl 1.1 assertFalse(q.isEmpty());
48     assertEquals(0, q.remainingCapacity());
49 jsr166 1.7 assertEquals(n, q.size());
50 dl 1.1 return q;
51     }
52    
53     /**
54     * isEmpty is true before add, false after
55     */
56     public void testEmpty() {
57     LinkedBlockingDeque q = new LinkedBlockingDeque();
58     assertTrue(q.isEmpty());
59     q.add(new Integer(1));
60     assertFalse(q.isEmpty());
61     q.add(new Integer(2));
62     q.removeFirst();
63     q.removeFirst();
64     assertTrue(q.isEmpty());
65     }
66    
67     /**
68     * size changes when elements added and removed
69     */
70     public void testSize() {
71     LinkedBlockingDeque q = populatedDeque(SIZE);
72     for (int i = 0; i < SIZE; ++i) {
73     assertEquals(SIZE-i, q.size());
74     q.removeFirst();
75     }
76     for (int i = 0; i < SIZE; ++i) {
77     assertEquals(i, q.size());
78     q.add(new Integer(i));
79     }
80     }
81    
82     /**
83     * offer(null) throws NPE
84     */
85     public void testOfferFirstNull() {
86 jsr166 1.7 try {
87 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque();
88     q.offerFirst(null);
89     shouldThrow();
90 jsr166 1.9 } catch (NullPointerException success) {}
91 dl 1.1 }
92    
93     /**
94 jsr166 1.4 * OfferFirst succeeds
95 dl 1.1 */
96     public void testOfferFirst() {
97     LinkedBlockingDeque q = new LinkedBlockingDeque();
98     assertTrue(q.offerFirst(new Integer(0)));
99     assertTrue(q.offerFirst(new Integer(1)));
100     }
101    
102     /**
103 jsr166 1.4 * OfferLast succeeds
104 dl 1.1 */
105     public void testOfferLast() {
106     LinkedBlockingDeque q = new LinkedBlockingDeque();
107     assertTrue(q.offerLast(new Integer(0)));
108     assertTrue(q.offerLast(new Integer(1)));
109     }
110    
111     /**
112 jsr166 1.25 * pollFirst succeeds unless empty
113 dl 1.1 */
114     public void testPollFirst() {
115     LinkedBlockingDeque q = populatedDeque(SIZE);
116     for (int i = 0; i < SIZE; ++i) {
117 jsr166 1.18 assertEquals(i, q.pollFirst());
118 dl 1.1 }
119 jsr166 1.7 assertNull(q.pollFirst());
120 dl 1.1 }
121    
122     /**
123 jsr166 1.25 * pollLast succeeds unless empty
124 dl 1.1 */
125     public void testPollLast() {
126     LinkedBlockingDeque q = populatedDeque(SIZE);
127     for (int i = SIZE-1; i >= 0; --i) {
128 jsr166 1.18 assertEquals(i, q.pollLast());
129 dl 1.1 }
130 jsr166 1.7 assertNull(q.pollLast());
131 dl 1.1 }
132    
133     /**
134 jsr166 1.25 * peekFirst returns next element, or null if empty
135 dl 1.1 */
136     public void testPeekFirst() {
137     LinkedBlockingDeque q = populatedDeque(SIZE);
138     for (int i = 0; i < SIZE; ++i) {
139 jsr166 1.18 assertEquals(i, q.peekFirst());
140     assertEquals(i, q.pollFirst());
141 dl 1.1 assertTrue(q.peekFirst() == null ||
142 jsr166 1.18 !q.peekFirst().equals(i));
143 dl 1.1 }
144 jsr166 1.7 assertNull(q.peekFirst());
145 dl 1.1 }
146    
147     /**
148 jsr166 1.25 * peek returns next element, or null if empty
149 dl 1.1 */
150     public void testPeek() {
151     LinkedBlockingDeque q = populatedDeque(SIZE);
152     for (int i = 0; i < SIZE; ++i) {
153 jsr166 1.18 assertEquals(i, q.peek());
154     assertEquals(i, q.pollFirst());
155 dl 1.1 assertTrue(q.peek() == null ||
156 jsr166 1.18 !q.peek().equals(i));
157 dl 1.1 }
158 jsr166 1.7 assertNull(q.peek());
159 dl 1.1 }
160    
161     /**
162 jsr166 1.25 * peekLast returns next element, or null if empty
163 dl 1.1 */
164     public void testPeekLast() {
165     LinkedBlockingDeque q = populatedDeque(SIZE);
166     for (int i = SIZE-1; i >= 0; --i) {
167 jsr166 1.18 assertEquals(i, q.peekLast());
168     assertEquals(i, q.pollLast());
169 dl 1.1 assertTrue(q.peekLast() == null ||
170 jsr166 1.18 !q.peekLast().equals(i));
171 dl 1.1 }
172 jsr166 1.7 assertNull(q.peekLast());
173 dl 1.1 }
174    
175     /**
176 jsr166 1.22 * getFirst() returns first element, or throws NSEE if empty
177 dl 1.1 */
178     public void testFirstElement() {
179     LinkedBlockingDeque q = populatedDeque(SIZE);
180     for (int i = 0; i < SIZE; ++i) {
181 jsr166 1.18 assertEquals(i, q.getFirst());
182     assertEquals(i, q.pollFirst());
183 dl 1.1 }
184     try {
185     q.getFirst();
186     shouldThrow();
187 jsr166 1.9 } catch (NoSuchElementException success) {}
188 jsr166 1.15 assertNull(q.peekFirst());
189 dl 1.1 }
190    
191     /**
192 jsr166 1.25 * getLast() returns last element, or throws NSEE if empty
193 dl 1.1 */
194     public void testLastElement() {
195     LinkedBlockingDeque q = populatedDeque(SIZE);
196     for (int i = SIZE-1; i >= 0; --i) {
197 jsr166 1.18 assertEquals(i, q.getLast());
198     assertEquals(i, q.pollLast());
199 dl 1.1 }
200     try {
201     q.getLast();
202     shouldThrow();
203 jsr166 1.9 } catch (NoSuchElementException success) {}
204 jsr166 1.7 assertNull(q.peekLast());
205 dl 1.1 }
206    
207     /**
208 jsr166 1.22 * removeFirst() removes first element, or throws NSEE if empty
209 dl 1.1 */
210     public void testRemoveFirst() {
211     LinkedBlockingDeque q = populatedDeque(SIZE);
212     for (int i = 0; i < SIZE; ++i) {
213 jsr166 1.18 assertEquals(i, q.removeFirst());
214 dl 1.1 }
215     try {
216     q.removeFirst();
217     shouldThrow();
218 jsr166 1.9 } catch (NoSuchElementException success) {}
219 jsr166 1.15 assertNull(q.peekFirst());
220     }
221    
222     /**
223 jsr166 1.22 * removeLast() removes last element, or throws NSEE if empty
224 jsr166 1.15 */
225     public void testRemoveLast() {
226     LinkedBlockingDeque q = populatedDeque(SIZE);
227     for (int i = SIZE - 1; i >= 0; --i) {
228 jsr166 1.18 assertEquals(i, q.removeLast());
229 jsr166 1.15 }
230     try {
231     q.removeLast();
232     shouldThrow();
233     } catch (NoSuchElementException success) {}
234     assertNull(q.peekLast());
235 dl 1.1 }
236    
237     /**
238 jsr166 1.25 * remove removes next element, or throws NSEE if empty
239 dl 1.1 */
240     public void testRemove() {
241     LinkedBlockingDeque q = populatedDeque(SIZE);
242     for (int i = 0; i < SIZE; ++i) {
243 jsr166 1.18 assertEquals(i, q.remove());
244 dl 1.1 }
245     try {
246     q.remove();
247     shouldThrow();
248 jsr166 1.9 } catch (NoSuchElementException success) {}
249 dl 1.1 }
250    
251     /**
252     * removeFirstOccurrence(x) removes x and returns true if present
253     */
254     public void testRemoveFirstOccurrence() {
255     LinkedBlockingDeque q = populatedDeque(SIZE);
256     for (int i = 1; i < SIZE; i+=2) {
257     assertTrue(q.removeFirstOccurrence(new Integer(i)));
258     }
259     for (int i = 0; i < SIZE; i+=2) {
260     assertTrue(q.removeFirstOccurrence(new Integer(i)));
261     assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
262     }
263     assertTrue(q.isEmpty());
264     }
265    
266     /**
267     * removeLastOccurrence(x) removes x and returns true if present
268     */
269     public void testRemoveLastOccurrence() {
270     LinkedBlockingDeque q = populatedDeque(SIZE);
271     for (int i = 1; i < SIZE; i+=2) {
272     assertTrue(q.removeLastOccurrence(new Integer(i)));
273     }
274     for (int i = 0; i < SIZE; i+=2) {
275     assertTrue(q.removeLastOccurrence(new Integer(i)));
276     assertFalse(q.removeLastOccurrence(new Integer(i+1)));
277     }
278     assertTrue(q.isEmpty());
279     }
280    
281     /**
282     * peekFirst returns element inserted with addFirst
283     */
284     public void testAddFirst() {
285     LinkedBlockingDeque q = populatedDeque(3);
286     q.pollLast();
287 jsr166 1.7 q.addFirst(four);
288 jsr166 1.20 assertSame(four, q.peekFirst());
289 jsr166 1.4 }
290 dl 1.1
291     /**
292     * peekLast returns element inserted with addLast
293     */
294     public void testAddLast() {
295     LinkedBlockingDeque q = populatedDeque(3);
296     q.pollLast();
297 jsr166 1.7 q.addLast(four);
298 jsr166 1.20 assertSame(four, q.peekLast());
299 jsr166 1.4 }
300 dl 1.1
301     /**
302     * A new deque has the indicated capacity, or Integer.MAX_VALUE if
303     * none given
304     */
305     public void testConstructor1() {
306     assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
307     assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
308     }
309    
310     /**
311 jsr166 1.11 * Constructor throws IAE if capacity argument nonpositive
312 dl 1.1 */
313     public void testConstructor2() {
314     try {
315     LinkedBlockingDeque q = new LinkedBlockingDeque(0);
316     shouldThrow();
317 jsr166 1.9 } catch (IllegalArgumentException success) {}
318 dl 1.1 }
319    
320     /**
321     * Initializing from null Collection throws NPE
322     */
323     public void testConstructor3() {
324     try {
325     LinkedBlockingDeque q = new LinkedBlockingDeque(null);
326     shouldThrow();
327 jsr166 1.9 } catch (NullPointerException success) {}
328 dl 1.1 }
329    
330     /**
331     * Initializing from Collection of null elements throws NPE
332     */
333     public void testConstructor4() {
334     try {
335     Integer[] ints = new Integer[SIZE];
336     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
337     shouldThrow();
338 jsr166 1.9 } catch (NullPointerException success) {}
339 dl 1.1 }
340    
341     /**
342     * Initializing from Collection with some null elements throws NPE
343     */
344     public void testConstructor5() {
345     try {
346     Integer[] ints = new Integer[SIZE];
347     for (int i = 0; i < SIZE-1; ++i)
348     ints[i] = new Integer(i);
349     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
350     shouldThrow();
351 jsr166 1.9 } catch (NullPointerException success) {}
352 dl 1.1 }
353    
354     /**
355     * Deque contains all elements of collection used to initialize
356     */
357     public void testConstructor6() {
358 jsr166 1.12 Integer[] ints = new Integer[SIZE];
359     for (int i = 0; i < SIZE; ++i)
360     ints[i] = new Integer(i);
361     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
362     for (int i = 0; i < SIZE; ++i)
363     assertEquals(ints[i], q.poll());
364 dl 1.1 }
365    
366     /**
367     * Deque transitions from empty to full when elements added
368     */
369     public void testEmptyFull() {
370     LinkedBlockingDeque q = new LinkedBlockingDeque(2);
371     assertTrue(q.isEmpty());
372     assertEquals("should have room for 2", 2, q.remainingCapacity());
373     q.add(one);
374     assertFalse(q.isEmpty());
375     q.add(two);
376     assertFalse(q.isEmpty());
377     assertEquals(0, q.remainingCapacity());
378     assertFalse(q.offer(three));
379     }
380    
381     /**
382     * remainingCapacity decreases on add, increases on remove
383     */
384     public void testRemainingCapacity() {
385     LinkedBlockingDeque q = populatedDeque(SIZE);
386     for (int i = 0; i < SIZE; ++i) {
387     assertEquals(i, q.remainingCapacity());
388     assertEquals(SIZE-i, q.size());
389     q.remove();
390     }
391     for (int i = 0; i < SIZE; ++i) {
392     assertEquals(SIZE-i, q.remainingCapacity());
393     assertEquals(i, q.size());
394     q.add(new Integer(i));
395     }
396     }
397    
398     /**
399     * offer(null) throws NPE
400     */
401     public void testOfferNull() {
402 jsr166 1.7 try {
403 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
404     q.offer(null);
405     shouldThrow();
406 jsr166 1.9 } catch (NullPointerException success) {}
407 dl 1.1 }
408    
409     /**
410     * add(null) throws NPE
411     */
412     public void testAddNull() {
413 jsr166 1.7 try {
414 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
415     q.add(null);
416     shouldThrow();
417 jsr166 1.9 } catch (NullPointerException success) {}
418 dl 1.1 }
419    
420     /**
421     * push(null) throws NPE
422     */
423     public void testPushNull() {
424 jsr166 1.7 try {
425 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
426     q.push(null);
427     shouldThrow();
428 jsr166 1.9 } catch (NullPointerException success) {}
429 dl 1.1 }
430    
431     /**
432     * push succeeds if not full; throws ISE if full
433     */
434     public void testPush() {
435 jsr166 1.7 try {
436 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
437     for (int i = 0; i < SIZE; ++i) {
438     Integer I = new Integer(i);
439     q.push(I);
440     assertEquals(I, q.peek());
441     }
442     assertEquals(0, q.remainingCapacity());
443     q.push(new Integer(SIZE));
444 jsr166 1.9 shouldThrow();
445     } catch (IllegalStateException success) {}
446 dl 1.1 }
447    
448     /**
449     * peekFirst returns element inserted with push
450     */
451     public void testPushWithPeek() {
452     LinkedBlockingDeque q = populatedDeque(3);
453     q.pollLast();
454 jsr166 1.7 q.push(four);
455 jsr166 1.20 assertSame(four, q.peekFirst());
456 jsr166 1.4 }
457 dl 1.1
458     /**
459 jsr166 1.25 * pop removes next element, or throws NSEE if empty
460 dl 1.1 */
461     public void testPop() {
462     LinkedBlockingDeque q = populatedDeque(SIZE);
463     for (int i = 0; i < SIZE; ++i) {
464 jsr166 1.18 assertEquals(i, q.pop());
465 dl 1.1 }
466     try {
467     q.pop();
468     shouldThrow();
469 jsr166 1.9 } catch (NoSuchElementException success) {}
470 dl 1.1 }
471    
472     /**
473     * Offer succeeds if not full; fails if full
474     */
475     public void testOffer() {
476     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
477     assertTrue(q.offer(zero));
478     assertFalse(q.offer(one));
479     }
480    
481     /**
482     * add succeeds if not full; throws ISE if full
483     */
484     public void testAdd() {
485 jsr166 1.7 try {
486 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487     for (int i = 0; i < SIZE; ++i) {
488     assertTrue(q.add(new Integer(i)));
489     }
490     assertEquals(0, q.remainingCapacity());
491     q.add(new Integer(SIZE));
492 jsr166 1.9 shouldThrow();
493     } catch (IllegalStateException success) {}
494 dl 1.1 }
495    
496     /**
497     * addAll(null) throws NPE
498     */
499     public void testAddAll1() {
500     try {
501     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
502     q.addAll(null);
503     shouldThrow();
504 jsr166 1.9 } catch (NullPointerException success) {}
505 dl 1.1 }
506    
507     /**
508     * addAll(this) throws IAE
509     */
510     public void testAddAllSelf() {
511     try {
512     LinkedBlockingDeque q = populatedDeque(SIZE);
513     q.addAll(q);
514     shouldThrow();
515 jsr166 1.9 } catch (IllegalArgumentException success) {}
516 dl 1.1 }
517    
518     /**
519     * addAll of a collection with null elements throws NPE
520     */
521     public void testAddAll2() {
522     try {
523     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524     Integer[] ints = new Integer[SIZE];
525     q.addAll(Arrays.asList(ints));
526     shouldThrow();
527 jsr166 1.9 } catch (NullPointerException success) {}
528 dl 1.1 }
529 jsr166 1.22
530 dl 1.1 /**
531     * addAll of a collection with any null elements throws NPE after
532     * possibly adding some elements
533     */
534     public void testAddAll3() {
535     try {
536     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
537     Integer[] ints = new Integer[SIZE];
538     for (int i = 0; i < SIZE-1; ++i)
539     ints[i] = new Integer(i);
540     q.addAll(Arrays.asList(ints));
541     shouldThrow();
542 jsr166 1.9 } catch (NullPointerException success) {}
543 dl 1.1 }
544 jsr166 1.22
545 dl 1.1 /**
546     * addAll throws ISE if not enough room
547     */
548     public void testAddAll4() {
549     try {
550     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
551     Integer[] ints = new Integer[SIZE];
552     for (int i = 0; i < SIZE; ++i)
553     ints[i] = new Integer(i);
554     q.addAll(Arrays.asList(ints));
555     shouldThrow();
556 jsr166 1.9 } catch (IllegalStateException success) {}
557 dl 1.1 }
558 jsr166 1.16
559 dl 1.1 /**
560     * Deque contains all elements, in traversal order, of successful addAll
561     */
562     public void testAddAll5() {
563 jsr166 1.12 Integer[] empty = new Integer[0];
564     Integer[] ints = new Integer[SIZE];
565     for (int i = 0; i < SIZE; ++i)
566     ints[i] = new Integer(i);
567     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568     assertFalse(q.addAll(Arrays.asList(empty)));
569     assertTrue(q.addAll(Arrays.asList(ints)));
570     for (int i = 0; i < SIZE; ++i)
571     assertEquals(ints[i], q.poll());
572 dl 1.1 }
573    
574     /**
575     * put(null) throws NPE
576     */
577 jsr166 1.10 public void testPutNull() throws InterruptedException {
578 jsr166 1.7 try {
579 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
580     q.put(null);
581     shouldThrow();
582 jsr166 1.9 } catch (NullPointerException success) {}
583 jsr166 1.10 }
584 dl 1.1
585     /**
586     * all elements successfully put are contained
587     */
588 jsr166 1.10 public void testPut() throws InterruptedException {
589     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
590     for (int i = 0; i < SIZE; ++i) {
591     Integer I = new Integer(i);
592     q.put(I);
593     assertTrue(q.contains(I));
594     }
595     assertEquals(0, q.remainingCapacity());
596 dl 1.1 }
597    
598     /**
599     * put blocks interruptibly if full
600     */
601 jsr166 1.9 public void testBlockingPut() throws InterruptedException {
602 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
603 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
604     Thread t = newStartedThread(new CheckedRunnable() {
605 jsr166 1.17 public void realRun() throws InterruptedException {
606     for (int i = 0; i < SIZE; ++i)
607     q.put(i);
608     assertEquals(SIZE, q.size());
609     assertEquals(0, q.remainingCapacity());
610 jsr166 1.39
611     Thread.currentThread().interrupt();
612 jsr166 1.9 try {
613 jsr166 1.17 q.put(99);
614     shouldThrow();
615     } catch (InterruptedException success) {}
616 jsr166 1.39 assertFalse(Thread.interrupted());
617    
618     pleaseInterrupt.countDown();
619     try {
620     q.put(99);
621     shouldThrow();
622     } catch (InterruptedException success) {}
623     assertFalse(Thread.interrupted());
624 jsr166 1.9 }});
625    
626 jsr166 1.39 await(pleaseInterrupt);
627     assertThreadStaysAlive(t);
628 jsr166 1.9 t.interrupt();
629 jsr166 1.39 awaitTermination(t);
630 jsr166 1.17 assertEquals(SIZE, q.size());
631     assertEquals(0, q.remainingCapacity());
632 dl 1.1 }
633    
634     /**
635 jsr166 1.39 * put blocks interruptibly waiting for take when full
636 dl 1.1 */
637 jsr166 1.9 public void testPutWithTake() throws InterruptedException {
638 jsr166 1.17 final int capacity = 2;
639     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
640 jsr166 1.39 final CountDownLatch pleaseTake = new CountDownLatch(1);
641     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
642     Thread t = newStartedThread(new CheckedRunnable() {
643 jsr166 1.17 public void realRun() throws InterruptedException {
644 jsr166 1.39 for (int i = 0; i < capacity; i++)
645 jsr166 1.17 q.put(i);
646 jsr166 1.39 pleaseTake.countDown();
647     q.put(86);
648    
649     pleaseInterrupt.countDown();
650 jsr166 1.10 try {
651 jsr166 1.17 q.put(99);
652     shouldThrow();
653     } catch (InterruptedException success) {}
654 jsr166 1.39 assertFalse(Thread.interrupted());
655 jsr166 1.10 }});
656 jsr166 1.9
657 jsr166 1.39 await(pleaseTake);
658 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
659     assertEquals(0, q.take());
660 jsr166 1.39
661     await(pleaseInterrupt);
662     assertThreadStaysAlive(t);
663 jsr166 1.9 t.interrupt();
664 jsr166 1.39 awaitTermination(t);
665 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
666 dl 1.1 }
667    
668     /**
669     * timed offer times out if full and elements not taken
670     */
671 jsr166 1.9 public void testTimedOffer() throws InterruptedException {
672 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
674     Thread t = newStartedThread(new CheckedRunnable() {
675 jsr166 1.9 public void realRun() throws InterruptedException {
676     q.put(new Object());
677     q.put(new Object());
678 jsr166 1.38 long startTime = System.nanoTime();
679     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
680     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
681     pleaseInterrupt.countDown();
682 jsr166 1.16 try {
683 jsr166 1.38 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
684 jsr166 1.16 shouldThrow();
685     } catch (InterruptedException success) {}
686     }});
687 jsr166 1.4
688 jsr166 1.38 await(pleaseInterrupt);
689 jsr166 1.39 assertThreadStaysAlive(t);
690 jsr166 1.9 t.interrupt();
691 jsr166 1.38 awaitTermination(t);
692 dl 1.1 }
693    
694     /**
695     * take retrieves elements in FIFO order
696     */
697 jsr166 1.9 public void testTake() throws InterruptedException {
698     LinkedBlockingDeque q = populatedDeque(SIZE);
699     for (int i = 0; i < SIZE; ++i) {
700 jsr166 1.18 assertEquals(i, q.take());
701 jsr166 1.7 }
702 dl 1.1 }
703    
704     /**
705 jsr166 1.39 * take removes existing elements until empty, then blocks interruptibly
706 dl 1.1 */
707 jsr166 1.9 public void testBlockingTake() throws InterruptedException {
708 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
709 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
710     Thread t = newStartedThread(new CheckedRunnable() {
711 jsr166 1.9 public void realRun() throws InterruptedException {
712     for (int i = 0; i < SIZE; ++i) {
713 jsr166 1.18 assertEquals(i, q.take());
714 jsr166 1.9 }
715 jsr166 1.39
716     Thread.currentThread().interrupt();
717 jsr166 1.17 try {
718     q.take();
719     shouldThrow();
720     } catch (InterruptedException success) {}
721 jsr166 1.39 assertFalse(Thread.interrupted());
722    
723     pleaseInterrupt.countDown();
724     try {
725     q.take();
726     shouldThrow();
727     } catch (InterruptedException success) {}
728     assertFalse(Thread.interrupted());
729 jsr166 1.17 }});
730 jsr166 1.9
731 jsr166 1.39 await(pleaseInterrupt);
732     assertThreadStaysAlive(t);
733 jsr166 1.9 t.interrupt();
734 jsr166 1.39 awaitTermination(t);
735 dl 1.1 }
736    
737     /**
738     * poll succeeds unless empty
739     */
740     public void testPoll() {
741     LinkedBlockingDeque q = populatedDeque(SIZE);
742     for (int i = 0; i < SIZE; ++i) {
743 jsr166 1.18 assertEquals(i, q.poll());
744 dl 1.1 }
745 jsr166 1.7 assertNull(q.poll());
746 dl 1.1 }
747    
748     /**
749     * timed poll with zero timeout succeeds when non-empty, else times out
750     */
751 jsr166 1.9 public void testTimedPoll0() throws InterruptedException {
752     LinkedBlockingDeque q = populatedDeque(SIZE);
753     for (int i = 0; i < SIZE; ++i) {
754 jsr166 1.18 assertEquals(i, q.poll(0, MILLISECONDS));
755 jsr166 1.7 }
756 jsr166 1.9 assertNull(q.poll(0, MILLISECONDS));
757 dl 1.1 }
758    
759     /**
760     * timed poll with nonzero timeout succeeds when non-empty, else times out
761     */
762 jsr166 1.9 public void testTimedPoll() throws InterruptedException {
763     LinkedBlockingDeque q = populatedDeque(SIZE);
764     for (int i = 0; i < SIZE; ++i) {
765 jsr166 1.39 long startTime = System.nanoTime();
766     assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
767     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
768     }
769     long startTime = System.nanoTime();
770     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
771     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
772     checkEmpty(q);
773 dl 1.1 }
774    
775     /**
776     * Interrupted timed poll throws InterruptedException instead of
777     * returning timeout status
778     */
779 jsr166 1.9 public void testInterruptedTimedPoll() throws InterruptedException {
780 jsr166 1.34 final BlockingQueue<Integer> q = populatedDeque(SIZE);
781     final CountDownLatch aboutToWait = new CountDownLatch(1);
782     Thread t = newStartedThread(new CheckedRunnable() {
783 jsr166 1.9 public void realRun() throws InterruptedException {
784     for (int i = 0; i < SIZE; ++i) {
785 jsr166 1.34 long t0 = System.nanoTime();
786     assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
787     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
788 jsr166 1.9 }
789 jsr166 1.34 long t0 = System.nanoTime();
790     aboutToWait.countDown();
791 jsr166 1.13 try {
792 jsr166 1.34 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
793 jsr166 1.13 shouldThrow();
794 jsr166 1.34 } catch (InterruptedException success) {
795     assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
796     }
797 jsr166 1.13 }});
798 jsr166 1.9
799 jsr166 1.34 aboutToWait.await();
800     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
801 jsr166 1.9 t.interrupt();
802 jsr166 1.34 awaitTermination(t, MEDIUM_DELAY_MS);
803     checkEmpty(q);
804 dl 1.1 }
805    
806     /**
807     * putFirst(null) throws NPE
808     */
809 jsr166 1.36 public void testPutFirstNull() throws InterruptedException {
810 jsr166 1.7 try {
811 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
812     q.putFirst(null);
813     shouldThrow();
814 jsr166 1.9 } catch (NullPointerException success) {}
815 jsr166 1.36 }
816 dl 1.1
817     /**
818     * all elements successfully putFirst are contained
819     */
820 jsr166 1.36 public void testPutFirst() throws InterruptedException {
821     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
822     for (int i = 0; i < SIZE; ++i) {
823     Integer I = new Integer(i);
824     q.putFirst(I);
825     assertTrue(q.contains(I));
826     }
827     assertEquals(0, q.remainingCapacity());
828 dl 1.1 }
829    
830     /**
831     * putFirst blocks interruptibly if full
832     */
833 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
834 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
835 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
836     Thread t = newStartedThread(new CheckedRunnable() {
837 jsr166 1.17 public void realRun() throws InterruptedException {
838     for (int i = 0; i < SIZE; ++i)
839     q.putFirst(i);
840     assertEquals(SIZE, q.size());
841     assertEquals(0, q.remainingCapacity());
842 jsr166 1.39
843     Thread.currentThread().interrupt();
844     try {
845     q.putFirst(99);
846     shouldThrow();
847     } catch (InterruptedException success) {}
848     assertFalse(Thread.interrupted());
849    
850     pleaseInterrupt.countDown();
851 jsr166 1.11 try {
852 jsr166 1.17 q.putFirst(99);
853     shouldThrow();
854     } catch (InterruptedException success) {}
855 jsr166 1.39 assertFalse(Thread.interrupted());
856 jsr166 1.11 }});
857 jsr166 1.9
858 jsr166 1.39 await(pleaseInterrupt);
859     assertThreadStaysAlive(t);
860 jsr166 1.9 t.interrupt();
861 jsr166 1.39 awaitTermination(t);
862 jsr166 1.17 assertEquals(SIZE, q.size());
863     assertEquals(0, q.remainingCapacity());
864 dl 1.1 }
865    
866     /**
867 jsr166 1.39 * putFirst blocks interruptibly waiting for take when full
868 dl 1.1 */
869 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
870 jsr166 1.17 final int capacity = 2;
871     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
872 jsr166 1.39 final CountDownLatch pleaseTake = new CountDownLatch(1);
873     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
874     Thread t = newStartedThread(new CheckedRunnable() {
875 jsr166 1.17 public void realRun() throws InterruptedException {
876 jsr166 1.39 for (int i = 0; i < capacity; i++)
877 jsr166 1.17 q.putFirst(i);
878 jsr166 1.39 pleaseTake.countDown();
879     q.putFirst(86);
880    
881     pleaseInterrupt.countDown();
882 jsr166 1.11 try {
883 jsr166 1.17 q.putFirst(99);
884     shouldThrow();
885     } catch (InterruptedException success) {}
886 jsr166 1.39 assertFalse(Thread.interrupted());
887 jsr166 1.11 }});
888 jsr166 1.9
889 jsr166 1.39 await(pleaseTake);
890 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
891     assertEquals(capacity - 1, q.take());
892 jsr166 1.39
893     await(pleaseInterrupt);
894     assertThreadStaysAlive(t);
895 jsr166 1.9 t.interrupt();
896 jsr166 1.39 awaitTermination(t);
897 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
898 dl 1.1 }
899    
900     /**
901     * timed offerFirst times out if full and elements not taken
902     */
903 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
904 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
906     Thread t = newStartedThread(new CheckedRunnable() {
907 jsr166 1.9 public void realRun() throws InterruptedException {
908     q.putFirst(new Object());
909     q.putFirst(new Object());
910 jsr166 1.38 long startTime = System.nanoTime();
911     assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
912     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
913     pleaseInterrupt.countDown();
914 jsr166 1.16 try {
915 jsr166 1.38 q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
916 jsr166 1.16 shouldThrow();
917     } catch (InterruptedException success) {}
918     }});
919 jsr166 1.4
920 jsr166 1.38 await(pleaseInterrupt);
921 jsr166 1.39 assertThreadStaysAlive(t);
922 jsr166 1.9 t.interrupt();
923 jsr166 1.38 awaitTermination(t);
924 dl 1.1 }
925    
926     /**
927     * take retrieves elements in FIFO order
928     */
929 jsr166 1.9 public void testTakeFirst() throws InterruptedException {
930     LinkedBlockingDeque q = populatedDeque(SIZE);
931     for (int i = 0; i < SIZE; ++i) {
932 jsr166 1.18 assertEquals(i, q.takeFirst());
933 jsr166 1.7 }
934 dl 1.1 }
935    
936     /**
937 jsr166 1.39 * takeFirst() blocks interruptibly when empty
938     */
939     public void testTakeFirstFromEmptyBlocksInterruptibly() {
940     final BlockingDeque q = new LinkedBlockingDeque();
941     final CountDownLatch threadStarted = new CountDownLatch(1);
942     Thread t = newStartedThread(new CheckedRunnable() {
943     public void realRun() {
944     threadStarted.countDown();
945     try {
946     q.takeFirst();
947     shouldThrow();
948     } catch (InterruptedException success) {}
949     assertFalse(Thread.interrupted());
950     }});
951    
952     await(threadStarted);
953     assertThreadStaysAlive(t);
954     t.interrupt();
955     awaitTermination(t);
956     }
957    
958     /**
959     * takeFirst() throws InterruptedException immediately if interrupted
960     * before waiting
961     */
962     public void testTakeFirstFromEmptyAfterInterrupt() {
963     final BlockingDeque q = new LinkedBlockingDeque();
964     Thread t = newStartedThread(new CheckedRunnable() {
965     public void realRun() {
966     Thread.currentThread().interrupt();
967     try {
968     q.takeFirst();
969     shouldThrow();
970     } catch (InterruptedException success) {}
971     assertFalse(Thread.interrupted());
972     }});
973    
974     awaitTermination(t);
975     }
976    
977     /**
978     * takeLast() blocks interruptibly when empty
979 dl 1.1 */
980 jsr166 1.39 public void testTakeLastFromEmptyBlocksInterruptibly() {
981     final BlockingDeque q = new LinkedBlockingDeque();
982     final CountDownLatch threadStarted = new CountDownLatch(1);
983     Thread t = newStartedThread(new CheckedRunnable() {
984     public void realRun() {
985     threadStarted.countDown();
986     try {
987     q.takeLast();
988     shouldThrow();
989     } catch (InterruptedException success) {}
990     assertFalse(Thread.interrupted());
991     }});
992 jsr166 1.9
993 jsr166 1.39 await(threadStarted);
994     assertThreadStaysAlive(t);
995 jsr166 1.9 t.interrupt();
996 jsr166 1.39 awaitTermination(t);
997     }
998    
999     /**
1000     * takeLast() throws InterruptedException immediately if interrupted
1001     * before waiting
1002     */
1003     public void testTakeLastFromEmptyAfterInterrupt() {
1004     final BlockingDeque q = new LinkedBlockingDeque();
1005     Thread t = newStartedThread(new CheckedRunnable() {
1006     public void realRun() {
1007     Thread.currentThread().interrupt();
1008     try {
1009     q.takeLast();
1010     shouldThrow();
1011     } catch (InterruptedException success) {}
1012     assertFalse(Thread.interrupted());
1013     }});
1014    
1015     awaitTermination(t);
1016 dl 1.1 }
1017    
1018     /**
1019 jsr166 1.39 * takeFirst removes existing elements until empty, then blocks interruptibly
1020 dl 1.1 */
1021 jsr166 1.9 public void testBlockingTakeFirst() throws InterruptedException {
1022 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
1023 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1024     Thread t = newStartedThread(new CheckedRunnable() {
1025 jsr166 1.9 public void realRun() throws InterruptedException {
1026 jsr166 1.39 for (int i = 0; i < SIZE; ++i) {
1027 jsr166 1.17 assertEquals(i, q.takeFirst());
1028 jsr166 1.39 }
1029    
1030     Thread.currentThread().interrupt();
1031     try {
1032     q.takeFirst();
1033     shouldThrow();
1034     } catch (InterruptedException success) {}
1035     assertFalse(Thread.interrupted());
1036    
1037     pleaseInterrupt.countDown();
1038 jsr166 1.17 try {
1039     q.takeFirst();
1040     shouldThrow();
1041     } catch (InterruptedException success) {}
1042 jsr166 1.39 assertFalse(Thread.interrupted());
1043 jsr166 1.17 }});
1044 jsr166 1.9
1045 jsr166 1.39 await(pleaseInterrupt);
1046     assertThreadStaysAlive(t);
1047 jsr166 1.9 t.interrupt();
1048 jsr166 1.39 awaitTermination(t);
1049 dl 1.1 }
1050    
1051     /**
1052     * timed pollFirst with zero timeout succeeds when non-empty, else times out
1053     */
1054 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
1055     LinkedBlockingDeque q = populatedDeque(SIZE);
1056     for (int i = 0; i < SIZE; ++i) {
1057 jsr166 1.18 assertEquals(i, q.pollFirst(0, MILLISECONDS));
1058 jsr166 1.7 }
1059 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
1060 dl 1.1 }
1061    
1062     /**
1063     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1064     */
1065 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
1066     LinkedBlockingDeque q = populatedDeque(SIZE);
1067     for (int i = 0; i < SIZE; ++i) {
1068 jsr166 1.39 long startTime = System.nanoTime();
1069     assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1070     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1071     }
1072     long startTime = System.nanoTime();
1073     assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1074     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1075     checkEmpty(q);
1076 dl 1.1 }
1077    
1078     /**
1079     * Interrupted timed pollFirst throws InterruptedException instead of
1080     * returning timeout status
1081     */
1082 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
1083 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1084     Thread t = newStartedThread(new CheckedRunnable() {
1085 jsr166 1.9 public void realRun() throws InterruptedException {
1086     LinkedBlockingDeque q = populatedDeque(SIZE);
1087     for (int i = 0; i < SIZE; ++i) {
1088 jsr166 1.39 assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1089 jsr166 1.9 }
1090 jsr166 1.39
1091     Thread.currentThread().interrupt();
1092     try {
1093     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1094     shouldThrow();
1095     } catch (InterruptedException success) {}
1096     assertFalse(Thread.interrupted());
1097    
1098     pleaseInterrupt.countDown();
1099 jsr166 1.13 try {
1100     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1101     shouldThrow();
1102     } catch (InterruptedException success) {}
1103 jsr166 1.39 assertFalse(Thread.interrupted());
1104 jsr166 1.13 }});
1105 jsr166 1.9
1106 jsr166 1.39 await(pleaseInterrupt);
1107     assertThreadStaysAlive(t);
1108 jsr166 1.9 t.interrupt();
1109 jsr166 1.39 awaitTermination(t);
1110 dl 1.1 }
1111    
1112     /**
1113 jsr166 1.25 * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1114     * on interruption throws
1115 dl 1.1 */
1116 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1117 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1118 jsr166 1.39 final CheckedBarrier barrier = new CheckedBarrier(2);
1119     Thread t = newStartedThread(new CheckedRunnable() {
1120 jsr166 1.9 public void realRun() throws InterruptedException {
1121 jsr166 1.39 long startTime = System.nanoTime();
1122     assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1123     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1124    
1125     barrier.await();
1126    
1127 jsr166 1.14 assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1128 jsr166 1.39
1129     Thread.currentThread().interrupt();
1130     try {
1131     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1132     shouldThrow();
1133     } catch (InterruptedException success) {}
1134    
1135     barrier.await();
1136 jsr166 1.14 try {
1137     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1138     shouldThrow();
1139     } catch (InterruptedException success) {}
1140 jsr166 1.39 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1141 jsr166 1.14 }});
1142 jsr166 1.9
1143 jsr166 1.39 barrier.await();
1144     long startTime = System.nanoTime();
1145     assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1146     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1147     barrier.await();
1148     assertThreadStaysAlive(t);
1149 jsr166 1.9 t.interrupt();
1150 jsr166 1.39 awaitTermination(t);
1151 jsr166 1.4 }
1152 dl 1.1
1153     /**
1154     * putLast(null) throws NPE
1155     */
1156 jsr166 1.36 public void testPutLastNull() throws InterruptedException {
1157 jsr166 1.7 try {
1158 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1159     q.putLast(null);
1160     shouldThrow();
1161 jsr166 1.9 } catch (NullPointerException success) {}
1162 jsr166 1.36 }
1163 dl 1.1
1164     /**
1165     * all elements successfully putLast are contained
1166     */
1167 jsr166 1.36 public void testPutLast() throws InterruptedException {
1168     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1169     for (int i = 0; i < SIZE; ++i) {
1170     Integer I = new Integer(i);
1171     q.putLast(I);
1172     assertTrue(q.contains(I));
1173     }
1174     assertEquals(0, q.remainingCapacity());
1175 dl 1.1 }
1176    
1177     /**
1178     * putLast blocks interruptibly if full
1179     */
1180 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
1181 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1182 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1183     Thread t = newStartedThread(new CheckedRunnable() {
1184 jsr166 1.17 public void realRun() throws InterruptedException {
1185     for (int i = 0; i < SIZE; ++i)
1186     q.putLast(i);
1187     assertEquals(SIZE, q.size());
1188     assertEquals(0, q.remainingCapacity());
1189 jsr166 1.39
1190     Thread.currentThread().interrupt();
1191     try {
1192     q.putLast(99);
1193     shouldThrow();
1194     } catch (InterruptedException success) {}
1195     assertFalse(Thread.interrupted());
1196    
1197     pleaseInterrupt.countDown();
1198 jsr166 1.11 try {
1199 jsr166 1.17 q.putLast(99);
1200     shouldThrow();
1201     } catch (InterruptedException success) {}
1202 jsr166 1.39 assertFalse(Thread.interrupted());
1203 jsr166 1.11 }});
1204    
1205 jsr166 1.39 await(pleaseInterrupt);
1206     assertThreadStaysAlive(t);
1207 jsr166 1.9 t.interrupt();
1208 jsr166 1.39 awaitTermination(t);
1209 jsr166 1.17 assertEquals(SIZE, q.size());
1210     assertEquals(0, q.remainingCapacity());
1211 dl 1.1 }
1212    
1213     /**
1214 jsr166 1.39 * putLast blocks interruptibly waiting for take when full
1215 dl 1.1 */
1216 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1217 jsr166 1.17 final int capacity = 2;
1218     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1219 jsr166 1.39 final CountDownLatch pleaseTake = new CountDownLatch(1);
1220     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1221     Thread t = newStartedThread(new CheckedRunnable() {
1222 jsr166 1.17 public void realRun() throws InterruptedException {
1223 jsr166 1.39 for (int i = 0; i < capacity; i++)
1224 jsr166 1.17 q.putLast(i);
1225 jsr166 1.39 pleaseTake.countDown();
1226     q.putLast(86);
1227    
1228     pleaseInterrupt.countDown();
1229 jsr166 1.11 try {
1230 jsr166 1.17 q.putLast(99);
1231     shouldThrow();
1232     } catch (InterruptedException success) {}
1233 jsr166 1.39 assertFalse(Thread.interrupted());
1234 jsr166 1.11 }});
1235 jsr166 1.9
1236 jsr166 1.39 await(pleaseTake);
1237 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1238     assertEquals(0, q.take());
1239 jsr166 1.39
1240     await(pleaseInterrupt);
1241     assertThreadStaysAlive(t);
1242 jsr166 1.9 t.interrupt();
1243 jsr166 1.39 awaitTermination(t);
1244 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1245 dl 1.1 }
1246    
1247     /**
1248     * timed offerLast times out if full and elements not taken
1249     */
1250 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1251 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1252 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1253     Thread t = newStartedThread(new CheckedRunnable() {
1254 jsr166 1.9 public void realRun() throws InterruptedException {
1255     q.putLast(new Object());
1256     q.putLast(new Object());
1257 jsr166 1.38 long startTime = System.nanoTime();
1258     assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1259     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1260     pleaseInterrupt.countDown();
1261 jsr166 1.16 try {
1262 jsr166 1.38 q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1263 jsr166 1.16 shouldThrow();
1264     } catch (InterruptedException success) {}
1265     }});
1266 jsr166 1.4
1267 jsr166 1.38 await(pleaseInterrupt);
1268 jsr166 1.39 assertThreadStaysAlive(t);
1269 jsr166 1.9 t.interrupt();
1270 jsr166 1.38 awaitTermination(t);
1271 dl 1.1 }
1272    
1273     /**
1274     * takeLast retrieves elements in FIFO order
1275     */
1276 jsr166 1.9 public void testTakeLast() throws InterruptedException {
1277     LinkedBlockingDeque q = populatedDeque(SIZE);
1278     for (int i = 0; i < SIZE; ++i) {
1279 jsr166 1.18 assertEquals(SIZE-i-1, q.takeLast());
1280 jsr166 1.7 }
1281 dl 1.1 }
1282    
1283     /**
1284 jsr166 1.39 * takeLast removes existing elements until empty, then blocks interruptibly
1285 dl 1.1 */
1286 jsr166 1.39 public void testBlockingTakeLast() throws InterruptedException {
1287     final LinkedBlockingDeque q = populatedDeque(SIZE);
1288     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1289     Thread t = newStartedThread(new CheckedRunnable() {
1290 jsr166 1.9 public void realRun() throws InterruptedException {
1291 jsr166 1.39 for (int i = 0; i < SIZE; ++i) {
1292     assertEquals(SIZE-i-1, q.takeLast());
1293     }
1294 jsr166 1.9
1295 jsr166 1.39 Thread.currentThread().interrupt();
1296     try {
1297     q.takeLast();
1298     shouldThrow();
1299     } catch (InterruptedException success) {}
1300     assertFalse(Thread.interrupted());
1301 dl 1.1
1302 jsr166 1.39 pleaseInterrupt.countDown();
1303 jsr166 1.17 try {
1304     q.takeLast();
1305     shouldThrow();
1306     } catch (InterruptedException success) {}
1307 jsr166 1.39 assertFalse(Thread.interrupted());
1308 jsr166 1.17 }});
1309 jsr166 1.9
1310 jsr166 1.39 await(pleaseInterrupt);
1311     assertThreadStaysAlive(t);
1312 jsr166 1.9 t.interrupt();
1313 jsr166 1.39 awaitTermination(t);
1314 dl 1.1 }
1315    
1316     /**
1317     * timed pollLast with zero timeout succeeds when non-empty, else times out
1318     */
1319 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1320     LinkedBlockingDeque q = populatedDeque(SIZE);
1321     for (int i = 0; i < SIZE; ++i) {
1322 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1323 jsr166 1.7 }
1324 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1325 dl 1.1 }
1326    
1327     /**
1328     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1329     */
1330 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1331     LinkedBlockingDeque q = populatedDeque(SIZE);
1332     for (int i = 0; i < SIZE; ++i) {
1333 jsr166 1.39 long startTime = System.nanoTime();
1334     assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1335     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1336     }
1337     long startTime = System.nanoTime();
1338     assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1339     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1340     checkEmpty(q);
1341 dl 1.1 }
1342    
1343     /**
1344     * Interrupted timed pollLast throws InterruptedException instead of
1345     * returning timeout status
1346     */
1347 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1348 jsr166 1.39 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1349     Thread t = newStartedThread(new CheckedRunnable() {
1350 jsr166 1.9 public void realRun() throws InterruptedException {
1351     LinkedBlockingDeque q = populatedDeque(SIZE);
1352     for (int i = 0; i < SIZE; ++i) {
1353 jsr166 1.39 assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1354 jsr166 1.9 }
1355 jsr166 1.39
1356     Thread.currentThread().interrupt();
1357 jsr166 1.13 try {
1358 jsr166 1.39 q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1359 jsr166 1.13 shouldThrow();
1360     } catch (InterruptedException success) {}
1361 jsr166 1.39 assertFalse(Thread.interrupted());
1362    
1363     pleaseInterrupt.countDown();
1364     try {
1365     q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1366     shouldThrow();
1367     } catch (InterruptedException success) {}
1368     assertFalse(Thread.interrupted());
1369 jsr166 1.13 }});
1370 jsr166 1.9
1371 jsr166 1.39 await(pleaseInterrupt);
1372     assertThreadStaysAlive(t);
1373 jsr166 1.9 t.interrupt();
1374 jsr166 1.39 awaitTermination(t);
1375 dl 1.1 }
1376    
1377     /**
1378 jsr166 1.24 * timed poll before a delayed offerLast fails; after offerLast succeeds;
1379     * on interruption throws
1380 dl 1.1 */
1381 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1382 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1383 jsr166 1.39 final CheckedBarrier barrier = new CheckedBarrier(2);
1384     Thread t = newStartedThread(new CheckedRunnable() {
1385 jsr166 1.9 public void realRun() throws InterruptedException {
1386 jsr166 1.39 long startTime = System.nanoTime();
1387     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1388     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1389    
1390     barrier.await();
1391    
1392 jsr166 1.9 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1393 jsr166 1.39
1394     Thread.currentThread().interrupt();
1395 jsr166 1.9 try {
1396     q.poll(LONG_DELAY_MS, MILLISECONDS);
1397     shouldThrow();
1398     } catch (InterruptedException success) {}
1399 jsr166 1.39 assertFalse(Thread.interrupted());
1400    
1401     barrier.await();
1402     try {
1403     q.poll(LONG_DELAY_MS, MILLISECONDS);
1404     shouldThrow();
1405     } catch (InterruptedException success) {}
1406     assertFalse(Thread.interrupted());
1407 jsr166 1.9 }});
1408    
1409 jsr166 1.39 barrier.await();
1410     long startTime = System.nanoTime();
1411     assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1412     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1413    
1414     barrier.await();
1415     assertThreadStaysAlive(t);
1416 jsr166 1.9 t.interrupt();
1417 jsr166 1.39 awaitTermination(t);
1418 jsr166 1.4 }
1419 dl 1.1
1420     /**
1421     * element returns next element, or throws NSEE if empty
1422     */
1423     public void testElement() {
1424     LinkedBlockingDeque q = populatedDeque(SIZE);
1425     for (int i = 0; i < SIZE; ++i) {
1426 jsr166 1.18 assertEquals(i, q.element());
1427 dl 1.1 q.poll();
1428     }
1429     try {
1430     q.element();
1431     shouldThrow();
1432 jsr166 1.9 } catch (NoSuchElementException success) {}
1433 dl 1.1 }
1434    
1435     /**
1436     * remove(x) removes x and returns true if present
1437     */
1438     public void testRemoveElement() {
1439     LinkedBlockingDeque q = populatedDeque(SIZE);
1440     for (int i = 1; i < SIZE; i+=2) {
1441 jsr166 1.33 assertTrue(q.contains(i));
1442     assertTrue(q.remove(i));
1443     assertFalse(q.contains(i));
1444     assertTrue(q.contains(i-1));
1445 dl 1.1 }
1446     for (int i = 0; i < SIZE; i+=2) {
1447 jsr166 1.33 assertTrue(q.contains(i));
1448     assertTrue(q.remove(i));
1449     assertFalse(q.contains(i));
1450     assertFalse(q.remove(i+1));
1451     assertFalse(q.contains(i+1));
1452 dl 1.1 }
1453     assertTrue(q.isEmpty());
1454     }
1455 jsr166 1.4
1456 dl 1.1 /**
1457     * contains(x) reports true when elements added but not yet removed
1458     */
1459     public void testContains() {
1460     LinkedBlockingDeque q = populatedDeque(SIZE);
1461     for (int i = 0; i < SIZE; ++i) {
1462     assertTrue(q.contains(new Integer(i)));
1463     q.poll();
1464     assertFalse(q.contains(new Integer(i)));
1465     }
1466     }
1467    
1468     /**
1469     * clear removes all elements
1470     */
1471     public void testClear() {
1472     LinkedBlockingDeque q = populatedDeque(SIZE);
1473     q.clear();
1474     assertTrue(q.isEmpty());
1475     assertEquals(0, q.size());
1476     assertEquals(SIZE, q.remainingCapacity());
1477     q.add(one);
1478     assertFalse(q.isEmpty());
1479     assertTrue(q.contains(one));
1480     q.clear();
1481     assertTrue(q.isEmpty());
1482     }
1483    
1484     /**
1485     * containsAll(c) is true when c contains a subset of elements
1486     */
1487     public void testContainsAll() {
1488     LinkedBlockingDeque q = populatedDeque(SIZE);
1489     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1490     for (int i = 0; i < SIZE; ++i) {
1491     assertTrue(q.containsAll(p));
1492     assertFalse(p.containsAll(q));
1493     p.add(new Integer(i));
1494     }
1495     assertTrue(p.containsAll(q));
1496     }
1497    
1498     /**
1499     * retainAll(c) retains only those elements of c and reports true if changed
1500     */
1501     public void testRetainAll() {
1502     LinkedBlockingDeque q = populatedDeque(SIZE);
1503     LinkedBlockingDeque p = populatedDeque(SIZE);
1504     for (int i = 0; i < SIZE; ++i) {
1505     boolean changed = q.retainAll(p);
1506     if (i == 0)
1507     assertFalse(changed);
1508     else
1509     assertTrue(changed);
1510    
1511     assertTrue(q.containsAll(p));
1512     assertEquals(SIZE-i, q.size());
1513     p.remove();
1514     }
1515     }
1516    
1517     /**
1518     * removeAll(c) removes only those elements of c and reports true if changed
1519     */
1520     public void testRemoveAll() {
1521     for (int i = 1; i < SIZE; ++i) {
1522     LinkedBlockingDeque q = populatedDeque(SIZE);
1523     LinkedBlockingDeque p = populatedDeque(i);
1524     assertTrue(q.removeAll(p));
1525     assertEquals(SIZE-i, q.size());
1526     for (int j = 0; j < i; ++j) {
1527     Integer I = (Integer)(p.remove());
1528     assertFalse(q.contains(I));
1529     }
1530     }
1531     }
1532    
1533     /**
1534 jsr166 1.31 * toArray contains all elements in FIFO order
1535 dl 1.1 */
1536 jsr166 1.9 public void testToArray() throws InterruptedException{
1537 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1538 jsr166 1.7 Object[] o = q.toArray();
1539     for (int i = 0; i < o.length; i++)
1540 jsr166 1.31 assertSame(o[i], q.poll());
1541 dl 1.1 }
1542    
1543     /**
1544 jsr166 1.31 * toArray(a) contains all elements in FIFO order
1545 dl 1.1 */
1546 jsr166 1.31 public void testToArray2() {
1547 jsr166 1.32 LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1548 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1549 jsr166 1.32 Integer[] array = q.toArray(ints);
1550     assertSame(ints, array);
1551 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1552 jsr166 1.31 assertSame(ints[i], q.remove());
1553 dl 1.1 }
1554    
1555     /**
1556 jsr166 1.30 * toArray(null) throws NullPointerException
1557 dl 1.1 */
1558 jsr166 1.30 public void testToArray_NullArg() {
1559 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1560 jsr166 1.7 try {
1561 jsr166 1.30 q.toArray(null);
1562 jsr166 1.7 shouldThrow();
1563     } catch (NullPointerException success) {}
1564 dl 1.1 }
1565    
1566     /**
1567 jsr166 1.29 * toArray(incompatible array type) throws ArrayStoreException
1568 dl 1.1 */
1569     public void testToArray1_BadArg() {
1570 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1571 jsr166 1.7 try {
1572 jsr166 1.29 q.toArray(new String[10]);
1573 jsr166 1.7 shouldThrow();
1574 jsr166 1.9 } catch (ArrayStoreException success) {}
1575 dl 1.1 }
1576    
1577     /**
1578     * iterator iterates through all elements
1579     */
1580 jsr166 1.9 public void testIterator() throws InterruptedException {
1581 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1582 jsr166 1.7 Iterator it = q.iterator();
1583 jsr166 1.9 while (it.hasNext()) {
1584     assertEquals(it.next(), q.take());
1585 jsr166 1.7 }
1586 dl 1.1 }
1587    
1588     /**
1589     * iterator.remove removes current element
1590     */
1591 jsr166 1.21 public void testIteratorRemove() {
1592 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1593     q.add(two);
1594     q.add(one);
1595     q.add(three);
1596    
1597     Iterator it = q.iterator();
1598     it.next();
1599     it.remove();
1600 jsr166 1.4
1601 dl 1.1 it = q.iterator();
1602 jsr166 1.20 assertSame(it.next(), one);
1603     assertSame(it.next(), three);
1604 dl 1.1 assertFalse(it.hasNext());
1605     }
1606    
1607     /**
1608     * iterator ordering is FIFO
1609     */
1610     public void testIteratorOrdering() {
1611     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1612     q.add(one);
1613     q.add(two);
1614     q.add(three);
1615     assertEquals(0, q.remainingCapacity());
1616     int k = 0;
1617     for (Iterator it = q.iterator(); it.hasNext();) {
1618 jsr166 1.18 assertEquals(++k, it.next());
1619 dl 1.1 }
1620     assertEquals(3, k);
1621     }
1622    
1623     /**
1624     * Modifications do not cause iterators to fail
1625     */
1626 jsr166 1.21 public void testWeaklyConsistentIteration() {
1627 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1628     q.add(one);
1629     q.add(two);
1630     q.add(three);
1631 jsr166 1.9 for (Iterator it = q.iterator(); it.hasNext();) {
1632     q.remove();
1633     it.next();
1634 dl 1.1 }
1635     assertEquals(0, q.size());
1636     }
1637    
1638     /**
1639 jsr166 1.25 * Descending iterator iterates through all elements
1640 dl 1.2 */
1641     public void testDescendingIterator() {
1642     LinkedBlockingDeque q = populatedDeque(SIZE);
1643     int i = 0;
1644 jsr166 1.7 Iterator it = q.descendingIterator();
1645 jsr166 1.5 while (it.hasNext()) {
1646 dl 1.2 assertTrue(q.contains(it.next()));
1647     ++i;
1648     }
1649     assertEquals(i, SIZE);
1650     assertFalse(it.hasNext());
1651     try {
1652     it.next();
1653 jsr166 1.9 shouldThrow();
1654     } catch (NoSuchElementException success) {}
1655 dl 1.2 }
1656    
1657     /**
1658 jsr166 1.25 * Descending iterator ordering is reverse FIFO
1659 dl 1.2 */
1660     public void testDescendingIteratorOrdering() {
1661     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1662 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1663     q.add(new Integer(3));
1664     q.add(new Integer(2));
1665     q.add(new Integer(1));
1666     int k = 0;
1667     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1668 jsr166 1.18 assertEquals(++k, it.next());
1669 dl 1.3 }
1670 jsr166 1.4
1671 dl 1.3 assertEquals(3, k);
1672     q.remove();
1673     q.remove();
1674     q.remove();
1675 dl 1.2 }
1676     }
1677    
1678     /**
1679     * descendingIterator.remove removes current element
1680     */
1681 jsr166 1.21 public void testDescendingIteratorRemove() {
1682 dl 1.2 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1683 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1684     q.add(new Integer(3));
1685     q.add(new Integer(2));
1686     q.add(new Integer(1));
1687     Iterator it = q.descendingIterator();
1688     assertEquals(it.next(), new Integer(1));
1689     it.remove();
1690     assertEquals(it.next(), new Integer(2));
1691     it = q.descendingIterator();
1692     assertEquals(it.next(), new Integer(2));
1693     assertEquals(it.next(), new Integer(3));
1694     it.remove();
1695     assertFalse(it.hasNext());
1696     q.remove();
1697     }
1698 dl 1.2 }
1699    
1700     /**
1701 dl 1.1 * toString contains toStrings of elements
1702     */
1703     public void testToString() {
1704     LinkedBlockingDeque q = populatedDeque(SIZE);
1705     String s = q.toString();
1706     for (int i = 0; i < SIZE; ++i) {
1707 jsr166 1.39 assertTrue(s.contains(String.valueOf(i)));
1708 dl 1.1 }
1709 jsr166 1.4 }
1710 dl 1.1
1711     /**
1712     * offer transfers elements across Executor tasks
1713     */
1714     public void testOfferInExecutor() {
1715     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1716     q.add(one);
1717     q.add(two);
1718     ExecutorService executor = Executors.newFixedThreadPool(2);
1719 jsr166 1.39 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1720 jsr166 1.9 executor.execute(new CheckedRunnable() {
1721     public void realRun() throws InterruptedException {
1722 jsr166 1.19 assertFalse(q.offer(three));
1723 jsr166 1.39 threadsStarted.await();
1724     assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1725 jsr166 1.19 assertEquals(0, q.remainingCapacity());
1726 jsr166 1.9 }});
1727    
1728     executor.execute(new CheckedRunnable() {
1729     public void realRun() throws InterruptedException {
1730 jsr166 1.39 threadsStarted.await();
1731 jsr166 1.19 assertSame(one, q.take());
1732 jsr166 1.9 }});
1733 jsr166 1.4
1734 dl 1.1 joinPool(executor);
1735     }
1736    
1737     /**
1738 jsr166 1.39 * timed poll retrieves elements across Executor threads
1739 dl 1.1 */
1740     public void testPollInExecutor() {
1741     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1742 jsr166 1.39 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1743 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
1744 jsr166 1.9 executor.execute(new CheckedRunnable() {
1745     public void realRun() throws InterruptedException {
1746 jsr166 1.19 assertNull(q.poll());
1747 jsr166 1.39 threadsStarted.await();
1748     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1749     checkEmpty(q);
1750 jsr166 1.9 }});
1751    
1752     executor.execute(new CheckedRunnable() {
1753     public void realRun() throws InterruptedException {
1754 jsr166 1.39 threadsStarted.await();
1755 jsr166 1.9 q.put(one);
1756     }});
1757 jsr166 1.4
1758 dl 1.1 joinPool(executor);
1759     }
1760    
1761     /**
1762     * A deserialized serialized deque has same elements in same order
1763     */
1764 jsr166 1.9 public void testSerialization() throws Exception {
1765 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1766    
1767 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1768     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1769     out.writeObject(q);
1770     out.close();
1771    
1772     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1773     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1774     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1775     assertEquals(q.size(), r.size());
1776     while (!q.isEmpty())
1777     assertEquals(q.remove(), r.remove());
1778 dl 1.1 }
1779    
1780     /**
1781     * drainTo(null) throws NPE
1782 jsr166 1.4 */
1783 dl 1.1 public void testDrainToNull() {
1784     LinkedBlockingDeque q = populatedDeque(SIZE);
1785     try {
1786     q.drainTo(null);
1787     shouldThrow();
1788 jsr166 1.9 } catch (NullPointerException success) {}
1789 dl 1.1 }
1790    
1791     /**
1792     * drainTo(this) throws IAE
1793 jsr166 1.4 */
1794 dl 1.1 public void testDrainToSelf() {
1795     LinkedBlockingDeque q = populatedDeque(SIZE);
1796     try {
1797     q.drainTo(q);
1798     shouldThrow();
1799 jsr166 1.10 } catch (IllegalArgumentException success) {}
1800 dl 1.1 }
1801    
1802     /**
1803     * drainTo(c) empties deque into another collection c
1804 jsr166 1.4 */
1805 dl 1.1 public void testDrainTo() {
1806     LinkedBlockingDeque q = populatedDeque(SIZE);
1807     ArrayList l = new ArrayList();
1808     q.drainTo(l);
1809     assertEquals(q.size(), 0);
1810     assertEquals(l.size(), SIZE);
1811 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1812 dl 1.1 assertEquals(l.get(i), new Integer(i));
1813     q.add(zero);
1814     q.add(one);
1815     assertFalse(q.isEmpty());
1816     assertTrue(q.contains(zero));
1817     assertTrue(q.contains(one));
1818     l.clear();
1819     q.drainTo(l);
1820     assertEquals(q.size(), 0);
1821     assertEquals(l.size(), 2);
1822 jsr166 1.4 for (int i = 0; i < 2; ++i)
1823 dl 1.1 assertEquals(l.get(i), new Integer(i));
1824     }
1825    
1826     /**
1827     * drainTo empties full deque, unblocking a waiting put.
1828 jsr166 1.4 */
1829 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1830 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1831 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1832     public void realRun() throws InterruptedException {
1833     q.put(new Integer(SIZE+1));
1834     }});
1835    
1836     t.start();
1837     ArrayList l = new ArrayList();
1838     q.drainTo(l);
1839     assertTrue(l.size() >= SIZE);
1840     for (int i = 0; i < SIZE; ++i)
1841     assertEquals(l.get(i), new Integer(i));
1842     t.join();
1843     assertTrue(q.size() + l.size() >= SIZE);
1844 dl 1.1 }
1845    
1846     /**
1847     * drainTo(null, n) throws NPE
1848 jsr166 1.4 */
1849 dl 1.1 public void testDrainToNullN() {
1850     LinkedBlockingDeque q = populatedDeque(SIZE);
1851     try {
1852     q.drainTo(null, 0);
1853     shouldThrow();
1854 jsr166 1.9 } catch (NullPointerException success) {}
1855 dl 1.1 }
1856    
1857     /**
1858     * drainTo(this, n) throws IAE
1859 jsr166 1.4 */
1860 dl 1.1 public void testDrainToSelfN() {
1861     LinkedBlockingDeque q = populatedDeque(SIZE);
1862     try {
1863     q.drainTo(q, 0);
1864     shouldThrow();
1865 jsr166 1.9 } catch (IllegalArgumentException success) {}
1866 dl 1.1 }
1867    
1868     /**
1869 jsr166 1.26 * drainTo(c, n) empties first min(n, size) elements of queue into c
1870 jsr166 1.4 */
1871 dl 1.1 public void testDrainToN() {
1872     LinkedBlockingDeque q = new LinkedBlockingDeque();
1873     for (int i = 0; i < SIZE + 2; ++i) {
1874 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1875 dl 1.1 assertTrue(q.offer(new Integer(j)));
1876     ArrayList l = new ArrayList();
1877     q.drainTo(l, i);
1878 jsr166 1.27 int k = (i < SIZE) ? i : SIZE;
1879 dl 1.1 assertEquals(l.size(), k);
1880     assertEquals(q.size(), SIZE-k);
1881 jsr166 1.4 for (int j = 0; j < k; ++j)
1882 dl 1.1 assertEquals(l.get(j), new Integer(j));
1883     while (q.poll() != null) ;
1884     }
1885     }
1886    
1887     }