ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.38
Committed: Sat May 21 06:24:33 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +27 -18 lines
Log Message:
various test improvements

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     /**
303     * A new deque has the indicated capacity, or Integer.MAX_VALUE if
304     * none given
305     */
306     public void testConstructor1() {
307     assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
308     assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
309     }
310    
311     /**
312 jsr166 1.11 * Constructor throws IAE if capacity argument nonpositive
313 dl 1.1 */
314     public void testConstructor2() {
315     try {
316     LinkedBlockingDeque q = new LinkedBlockingDeque(0);
317     shouldThrow();
318 jsr166 1.9 } catch (IllegalArgumentException success) {}
319 dl 1.1 }
320    
321     /**
322     * Initializing from null Collection throws NPE
323     */
324     public void testConstructor3() {
325     try {
326     LinkedBlockingDeque q = new LinkedBlockingDeque(null);
327     shouldThrow();
328 jsr166 1.9 } catch (NullPointerException success) {}
329 dl 1.1 }
330    
331     /**
332     * Initializing from Collection of null elements throws NPE
333     */
334     public void testConstructor4() {
335     try {
336     Integer[] ints = new Integer[SIZE];
337     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
338     shouldThrow();
339 jsr166 1.9 } catch (NullPointerException success) {}
340 dl 1.1 }
341    
342     /**
343     * Initializing from Collection with some null elements throws NPE
344     */
345     public void testConstructor5() {
346     try {
347     Integer[] ints = new Integer[SIZE];
348     for (int i = 0; i < SIZE-1; ++i)
349     ints[i] = new Integer(i);
350     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
351     shouldThrow();
352 jsr166 1.9 } catch (NullPointerException success) {}
353 dl 1.1 }
354    
355     /**
356     * Deque contains all elements of collection used to initialize
357     */
358     public void testConstructor6() {
359 jsr166 1.12 Integer[] ints = new Integer[SIZE];
360     for (int i = 0; i < SIZE; ++i)
361     ints[i] = new Integer(i);
362     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
363     for (int i = 0; i < SIZE; ++i)
364     assertEquals(ints[i], q.poll());
365 dl 1.1 }
366    
367     /**
368     * Deque transitions from empty to full when elements added
369     */
370     public void testEmptyFull() {
371     LinkedBlockingDeque q = new LinkedBlockingDeque(2);
372     assertTrue(q.isEmpty());
373     assertEquals("should have room for 2", 2, q.remainingCapacity());
374     q.add(one);
375     assertFalse(q.isEmpty());
376     q.add(two);
377     assertFalse(q.isEmpty());
378     assertEquals(0, q.remainingCapacity());
379     assertFalse(q.offer(three));
380     }
381    
382     /**
383     * remainingCapacity decreases on add, increases on remove
384     */
385     public void testRemainingCapacity() {
386     LinkedBlockingDeque q = populatedDeque(SIZE);
387     for (int i = 0; i < SIZE; ++i) {
388     assertEquals(i, q.remainingCapacity());
389     assertEquals(SIZE-i, q.size());
390     q.remove();
391     }
392     for (int i = 0; i < SIZE; ++i) {
393     assertEquals(SIZE-i, q.remainingCapacity());
394     assertEquals(i, q.size());
395     q.add(new Integer(i));
396     }
397     }
398    
399     /**
400     * offer(null) throws NPE
401     */
402     public void testOfferNull() {
403 jsr166 1.7 try {
404 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
405     q.offer(null);
406     shouldThrow();
407 jsr166 1.9 } catch (NullPointerException success) {}
408 dl 1.1 }
409    
410     /**
411     * add(null) throws NPE
412     */
413     public void testAddNull() {
414 jsr166 1.7 try {
415 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
416     q.add(null);
417     shouldThrow();
418 jsr166 1.9 } catch (NullPointerException success) {}
419 dl 1.1 }
420    
421     /**
422     * push(null) throws NPE
423     */
424     public void testPushNull() {
425 jsr166 1.7 try {
426 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
427     q.push(null);
428     shouldThrow();
429 jsr166 1.9 } catch (NullPointerException success) {}
430 dl 1.1 }
431    
432     /**
433     * push succeeds if not full; throws ISE if full
434     */
435     public void testPush() {
436 jsr166 1.7 try {
437 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
438     for (int i = 0; i < SIZE; ++i) {
439     Integer I = new Integer(i);
440     q.push(I);
441     assertEquals(I, q.peek());
442     }
443     assertEquals(0, q.remainingCapacity());
444     q.push(new Integer(SIZE));
445 jsr166 1.9 shouldThrow();
446     } catch (IllegalStateException success) {}
447 dl 1.1 }
448    
449     /**
450     * peekFirst returns element inserted with push
451     */
452     public void testPushWithPeek() {
453     LinkedBlockingDeque q = populatedDeque(3);
454     q.pollLast();
455 jsr166 1.7 q.push(four);
456 jsr166 1.20 assertSame(four, q.peekFirst());
457 jsr166 1.4 }
458 dl 1.1
459    
460     /**
461 jsr166 1.25 * pop removes next element, or throws NSEE if empty
462 dl 1.1 */
463     public void testPop() {
464     LinkedBlockingDeque q = populatedDeque(SIZE);
465     for (int i = 0; i < SIZE; ++i) {
466 jsr166 1.18 assertEquals(i, q.pop());
467 dl 1.1 }
468     try {
469     q.pop();
470     shouldThrow();
471 jsr166 1.9 } catch (NoSuchElementException success) {}
472 dl 1.1 }
473    
474    
475     /**
476     * Offer succeeds if not full; fails if full
477     */
478     public void testOffer() {
479     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
480     assertTrue(q.offer(zero));
481     assertFalse(q.offer(one));
482     }
483    
484     /**
485     * add succeeds if not full; throws ISE if full
486     */
487     public void testAdd() {
488 jsr166 1.7 try {
489 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
490     for (int i = 0; i < SIZE; ++i) {
491     assertTrue(q.add(new Integer(i)));
492     }
493     assertEquals(0, q.remainingCapacity());
494     q.add(new Integer(SIZE));
495 jsr166 1.9 shouldThrow();
496     } catch (IllegalStateException success) {}
497 dl 1.1 }
498    
499     /**
500     * addAll(null) throws NPE
501     */
502     public void testAddAll1() {
503     try {
504     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
505     q.addAll(null);
506     shouldThrow();
507 jsr166 1.9 } catch (NullPointerException success) {}
508 dl 1.1 }
509    
510     /**
511     * addAll(this) throws IAE
512     */
513     public void testAddAllSelf() {
514     try {
515     LinkedBlockingDeque q = populatedDeque(SIZE);
516     q.addAll(q);
517     shouldThrow();
518 jsr166 1.9 } catch (IllegalArgumentException success) {}
519 dl 1.1 }
520    
521     /**
522     * addAll of a collection with null elements throws NPE
523     */
524     public void testAddAll2() {
525     try {
526     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
527     Integer[] ints = new Integer[SIZE];
528     q.addAll(Arrays.asList(ints));
529     shouldThrow();
530 jsr166 1.9 } catch (NullPointerException success) {}
531 dl 1.1 }
532 jsr166 1.22
533 dl 1.1 /**
534     * addAll of a collection with any null elements throws NPE after
535     * possibly adding some elements
536     */
537     public void testAddAll3() {
538     try {
539     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
540     Integer[] ints = new Integer[SIZE];
541     for (int i = 0; i < SIZE-1; ++i)
542     ints[i] = new Integer(i);
543     q.addAll(Arrays.asList(ints));
544     shouldThrow();
545 jsr166 1.9 } catch (NullPointerException success) {}
546 dl 1.1 }
547 jsr166 1.22
548 dl 1.1 /**
549     * addAll throws ISE if not enough room
550     */
551     public void testAddAll4() {
552     try {
553     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
554     Integer[] ints = new Integer[SIZE];
555     for (int i = 0; i < SIZE; ++i)
556     ints[i] = new Integer(i);
557     q.addAll(Arrays.asList(ints));
558     shouldThrow();
559 jsr166 1.9 } catch (IllegalStateException success) {}
560 dl 1.1 }
561 jsr166 1.16
562 dl 1.1 /**
563     * Deque contains all elements, in traversal order, of successful addAll
564     */
565     public void testAddAll5() {
566 jsr166 1.12 Integer[] empty = new Integer[0];
567     Integer[] ints = new Integer[SIZE];
568     for (int i = 0; i < SIZE; ++i)
569     ints[i] = new Integer(i);
570     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571     assertFalse(q.addAll(Arrays.asList(empty)));
572     assertTrue(q.addAll(Arrays.asList(ints)));
573     for (int i = 0; i < SIZE; ++i)
574     assertEquals(ints[i], q.poll());
575 dl 1.1 }
576    
577    
578     /**
579     * put(null) throws NPE
580     */
581 jsr166 1.10 public void testPutNull() throws InterruptedException {
582 jsr166 1.7 try {
583 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584     q.put(null);
585     shouldThrow();
586 jsr166 1.9 } catch (NullPointerException success) {}
587 jsr166 1.10 }
588 dl 1.1
589     /**
590     * all elements successfully put are contained
591     */
592 jsr166 1.10 public void testPut() throws InterruptedException {
593     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
594     for (int i = 0; i < SIZE; ++i) {
595     Integer I = new Integer(i);
596     q.put(I);
597     assertTrue(q.contains(I));
598     }
599     assertEquals(0, q.remainingCapacity());
600 dl 1.1 }
601    
602     /**
603     * put blocks interruptibly if full
604     */
605 jsr166 1.9 public void testBlockingPut() throws InterruptedException {
606 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
607 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
608 jsr166 1.17 public void realRun() throws InterruptedException {
609     for (int i = 0; i < SIZE; ++i)
610     q.put(i);
611     assertEquals(SIZE, q.size());
612     assertEquals(0, q.remainingCapacity());
613 jsr166 1.9 try {
614 jsr166 1.17 q.put(99);
615     shouldThrow();
616     } catch (InterruptedException success) {}
617 jsr166 1.9 }});
618    
619 dl 1.1 t.start();
620 dl 1.37 delay(SHORT_DELAY_MS);
621 jsr166 1.9 t.interrupt();
622     t.join();
623 jsr166 1.17 assertEquals(SIZE, q.size());
624     assertEquals(0, q.remainingCapacity());
625 dl 1.1 }
626    
627     /**
628     * put blocks waiting for take when full
629     */
630 jsr166 1.9 public void testPutWithTake() throws InterruptedException {
631 jsr166 1.17 final int capacity = 2;
632     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
633 jsr166 1.10 Thread t = new Thread(new CheckedRunnable() {
634 jsr166 1.17 public void realRun() throws InterruptedException {
635     for (int i = 0; i < capacity + 1; i++)
636     q.put(i);
637 jsr166 1.10 try {
638 jsr166 1.17 q.put(99);
639     shouldThrow();
640     } catch (InterruptedException success) {}
641 jsr166 1.10 }});
642 jsr166 1.9
643     t.start();
644 dl 1.37 delay(SHORT_DELAY_MS);
645 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
646     assertEquals(0, q.take());
647 dl 1.37 delay(SHORT_DELAY_MS);
648 jsr166 1.9 t.interrupt();
649     t.join();
650 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
651 dl 1.1 }
652    
653     /**
654     * timed offer times out if full and elements not taken
655     */
656 jsr166 1.9 public void testTimedOffer() throws InterruptedException {
657 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
658 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
659     Thread t = newStartedThread(new CheckedRunnable() {
660 jsr166 1.9 public void realRun() throws InterruptedException {
661     q.put(new Object());
662     q.put(new Object());
663 jsr166 1.38 long startTime = System.nanoTime();
664     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
665     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
666     pleaseInterrupt.countDown();
667 jsr166 1.16 try {
668 jsr166 1.38 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
669 jsr166 1.16 shouldThrow();
670     } catch (InterruptedException success) {}
671     }});
672 jsr166 1.4
673 jsr166 1.38 await(pleaseInterrupt);
674 jsr166 1.9 t.interrupt();
675 jsr166 1.38 awaitTermination(t);
676 dl 1.1 }
677    
678     /**
679     * take retrieves elements in FIFO order
680     */
681 jsr166 1.9 public void testTake() throws InterruptedException {
682     LinkedBlockingDeque q = populatedDeque(SIZE);
683     for (int i = 0; i < SIZE; ++i) {
684 jsr166 1.18 assertEquals(i, q.take());
685 jsr166 1.7 }
686 dl 1.1 }
687    
688     /**
689     * Take removes existing elements until empty, then blocks interruptibly
690     */
691 jsr166 1.9 public void testBlockingTake() throws InterruptedException {
692 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
693     Thread t = new Thread(new CheckedRunnable() {
694 jsr166 1.9 public void realRun() throws InterruptedException {
695     for (int i = 0; i < SIZE; ++i) {
696 jsr166 1.18 assertEquals(i, q.take());
697 jsr166 1.9 }
698 jsr166 1.17 try {
699     q.take();
700     shouldThrow();
701     } catch (InterruptedException success) {}
702     }});
703 jsr166 1.9
704 dl 1.1 t.start();
705 dl 1.37 delay(SHORT_DELAY_MS);
706 jsr166 1.9 t.interrupt();
707     t.join();
708 dl 1.1 }
709    
710    
711     /**
712     * poll succeeds unless empty
713     */
714     public void testPoll() {
715     LinkedBlockingDeque q = populatedDeque(SIZE);
716     for (int i = 0; i < SIZE; ++i) {
717 jsr166 1.18 assertEquals(i, q.poll());
718 dl 1.1 }
719 jsr166 1.7 assertNull(q.poll());
720 dl 1.1 }
721    
722     /**
723     * timed poll with zero timeout succeeds when non-empty, else times out
724     */
725 jsr166 1.9 public void testTimedPoll0() throws InterruptedException {
726     LinkedBlockingDeque q = populatedDeque(SIZE);
727     for (int i = 0; i < SIZE; ++i) {
728 jsr166 1.18 assertEquals(i, q.poll(0, MILLISECONDS));
729 jsr166 1.7 }
730 jsr166 1.9 assertNull(q.poll(0, MILLISECONDS));
731 dl 1.1 }
732    
733     /**
734     * timed poll with nonzero timeout succeeds when non-empty, else times out
735     */
736 jsr166 1.9 public void testTimedPoll() throws InterruptedException {
737     LinkedBlockingDeque q = populatedDeque(SIZE);
738     for (int i = 0; i < SIZE; ++i) {
739 jsr166 1.18 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
740 jsr166 1.7 }
741 jsr166 1.9 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
742 dl 1.1 }
743    
744     /**
745     * Interrupted timed poll throws InterruptedException instead of
746     * returning timeout status
747     */
748 jsr166 1.9 public void testInterruptedTimedPoll() throws InterruptedException {
749 jsr166 1.34 final BlockingQueue<Integer> q = populatedDeque(SIZE);
750     final CountDownLatch aboutToWait = new CountDownLatch(1);
751     Thread t = newStartedThread(new CheckedRunnable() {
752 jsr166 1.9 public void realRun() throws InterruptedException {
753     for (int i = 0; i < SIZE; ++i) {
754 jsr166 1.34 long t0 = System.nanoTime();
755     assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
756     assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757 jsr166 1.9 }
758 jsr166 1.34 long t0 = System.nanoTime();
759     aboutToWait.countDown();
760 jsr166 1.13 try {
761 jsr166 1.34 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762 jsr166 1.13 shouldThrow();
763 jsr166 1.34 } catch (InterruptedException success) {
764     assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765     }
766 jsr166 1.13 }});
767 jsr166 1.9
768 jsr166 1.34 aboutToWait.await();
769     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770 jsr166 1.9 t.interrupt();
771 jsr166 1.34 awaitTermination(t, MEDIUM_DELAY_MS);
772     checkEmpty(q);
773 dl 1.1 }
774    
775     /**
776     * putFirst(null) throws NPE
777     */
778 jsr166 1.36 public void testPutFirstNull() throws InterruptedException {
779 jsr166 1.7 try {
780 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781     q.putFirst(null);
782     shouldThrow();
783 jsr166 1.9 } catch (NullPointerException success) {}
784 jsr166 1.36 }
785 dl 1.1
786     /**
787     * all elements successfully putFirst are contained
788     */
789 jsr166 1.36 public void testPutFirst() throws InterruptedException {
790     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791     for (int i = 0; i < SIZE; ++i) {
792     Integer I = new Integer(i);
793     q.putFirst(I);
794     assertTrue(q.contains(I));
795     }
796     assertEquals(0, q.remainingCapacity());
797 dl 1.1 }
798    
799     /**
800     * putFirst blocks interruptibly if full
801     */
802 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
803 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
804 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
805 jsr166 1.17 public void realRun() throws InterruptedException {
806     for (int i = 0; i < SIZE; ++i)
807     q.putFirst(i);
808     assertEquals(SIZE, q.size());
809     assertEquals(0, q.remainingCapacity());
810 jsr166 1.11 try {
811 jsr166 1.17 q.putFirst(99);
812     shouldThrow();
813     } catch (InterruptedException success) {}
814 jsr166 1.11 }});
815 jsr166 1.9
816 dl 1.1 t.start();
817 dl 1.37 delay(SHORT_DELAY_MS);
818 jsr166 1.9 t.interrupt();
819     t.join();
820 jsr166 1.17 assertEquals(SIZE, q.size());
821     assertEquals(0, q.remainingCapacity());
822 dl 1.1 }
823    
824     /**
825     * putFirst blocks waiting for take when full
826     */
827 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
828 jsr166 1.17 final int capacity = 2;
829     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
830 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
831 jsr166 1.17 public void realRun() throws InterruptedException {
832     for (int i = 0; i < capacity + 1; i++)
833     q.putFirst(i);
834 jsr166 1.11 try {
835 jsr166 1.17 q.putFirst(99);
836     shouldThrow();
837     } catch (InterruptedException success) {}
838 jsr166 1.11 }});
839 jsr166 1.9
840     t.start();
841 dl 1.37 delay(SHORT_DELAY_MS);
842 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
843     assertEquals(capacity - 1, q.take());
844 dl 1.37 delay(SHORT_DELAY_MS);
845 jsr166 1.9 t.interrupt();
846     t.join();
847 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
848 dl 1.1 }
849    
850     /**
851     * timed offerFirst times out if full and elements not taken
852     */
853 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
854 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
855 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
856     Thread t = newStartedThread(new CheckedRunnable() {
857 jsr166 1.9 public void realRun() throws InterruptedException {
858     q.putFirst(new Object());
859     q.putFirst(new Object());
860 jsr166 1.38 long startTime = System.nanoTime();
861     assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
862     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
863     pleaseInterrupt.countDown();
864 jsr166 1.16 try {
865 jsr166 1.38 q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
866 jsr166 1.16 shouldThrow();
867     } catch (InterruptedException success) {}
868     }});
869 jsr166 1.4
870 jsr166 1.38 await(pleaseInterrupt);
871 jsr166 1.9 t.interrupt();
872 jsr166 1.38 awaitTermination(t);
873 dl 1.1 }
874    
875     /**
876     * take retrieves elements in FIFO order
877     */
878 jsr166 1.9 public void testTakeFirst() throws InterruptedException {
879     LinkedBlockingDeque q = populatedDeque(SIZE);
880     for (int i = 0; i < SIZE; ++i) {
881 jsr166 1.18 assertEquals(i, q.takeFirst());
882 jsr166 1.7 }
883 dl 1.1 }
884    
885     /**
886     * takeFirst blocks interruptibly when empty
887     */
888 jsr166 1.9 public void testTakeFirstFromEmpty() throws InterruptedException {
889 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
890 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
891     public void realRun() throws InterruptedException {
892     q.takeFirst();
893     }};
894    
895     t.start();
896 dl 1.37 delay(SHORT_DELAY_MS);
897 jsr166 1.9 t.interrupt();
898     t.join();
899 dl 1.1 }
900    
901     /**
902     * TakeFirst removes existing elements until empty, then blocks interruptibly
903     */
904 jsr166 1.9 public void testBlockingTakeFirst() throws InterruptedException {
905 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
906     Thread t = new Thread(new CheckedRunnable() {
907 jsr166 1.9 public void realRun() throws InterruptedException {
908 jsr166 1.17 for (int i = 0; i < SIZE; ++i)
909     assertEquals(i, q.takeFirst());
910     try {
911     q.takeFirst();
912     shouldThrow();
913     } catch (InterruptedException success) {}
914     }});
915 jsr166 1.9
916 dl 1.1 t.start();
917 dl 1.37 delay(SHORT_DELAY_MS);
918 jsr166 1.9 t.interrupt();
919     t.join();
920 dl 1.1 }
921    
922    
923     /**
924     * timed pollFirst with zero timeout succeeds when non-empty, else times out
925     */
926 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
927     LinkedBlockingDeque q = populatedDeque(SIZE);
928     for (int i = 0; i < SIZE; ++i) {
929 jsr166 1.18 assertEquals(i, q.pollFirst(0, MILLISECONDS));
930 jsr166 1.7 }
931 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
932 dl 1.1 }
933    
934     /**
935     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
936     */
937 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
938     LinkedBlockingDeque q = populatedDeque(SIZE);
939     for (int i = 0; i < SIZE; ++i) {
940 jsr166 1.18 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
941 jsr166 1.7 }
942 jsr166 1.9 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
943 dl 1.1 }
944    
945     /**
946     * Interrupted timed pollFirst throws InterruptedException instead of
947     * returning timeout status
948     */
949 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
950 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
951 jsr166 1.9 public void realRun() throws InterruptedException {
952     LinkedBlockingDeque q = populatedDeque(SIZE);
953     for (int i = 0; i < SIZE; ++i) {
954 jsr166 1.18 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
955 jsr166 1.9 }
956 jsr166 1.13 try {
957     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
958     shouldThrow();
959     } catch (InterruptedException success) {}
960     }});
961 jsr166 1.9
962 dl 1.1 t.start();
963 dl 1.37 delay(SHORT_DELAY_MS);
964 jsr166 1.9 t.interrupt();
965     t.join();
966 dl 1.1 }
967    
968     /**
969 jsr166 1.25 * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
970     * on interruption throws
971 dl 1.1 */
972 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
973 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
974 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
975 jsr166 1.9 public void realRun() throws InterruptedException {
976 jsr166 1.14 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
977     assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
978     try {
979     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
980     shouldThrow();
981     } catch (InterruptedException success) {}
982     }});
983 jsr166 1.9
984     t.start();
985 dl 1.37 delay(SMALL_DELAY_MS);
986 jsr166 1.9 assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
987     t.interrupt();
988     t.join();
989 jsr166 1.4 }
990 dl 1.1
991     /**
992     * putLast(null) throws NPE
993     */
994 jsr166 1.36 public void testPutLastNull() throws InterruptedException {
995 jsr166 1.7 try {
996 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
997     q.putLast(null);
998     shouldThrow();
999 jsr166 1.9 } catch (NullPointerException success) {}
1000 jsr166 1.36 }
1001 dl 1.1
1002     /**
1003     * all elements successfully putLast are contained
1004     */
1005 jsr166 1.36 public void testPutLast() throws InterruptedException {
1006     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007     for (int i = 0; i < SIZE; ++i) {
1008     Integer I = new Integer(i);
1009     q.putLast(I);
1010     assertTrue(q.contains(I));
1011     }
1012     assertEquals(0, q.remainingCapacity());
1013 dl 1.1 }
1014    
1015     /**
1016     * putLast blocks interruptibly if full
1017     */
1018 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
1019 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1020 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1021 jsr166 1.17 public void realRun() throws InterruptedException {
1022     for (int i = 0; i < SIZE; ++i)
1023     q.putLast(i);
1024     assertEquals(SIZE, q.size());
1025     assertEquals(0, q.remainingCapacity());
1026 jsr166 1.11 try {
1027 jsr166 1.17 q.putLast(99);
1028     shouldThrow();
1029     } catch (InterruptedException success) {}
1030 jsr166 1.11 }});
1031    
1032 dl 1.1 t.start();
1033 dl 1.37 delay(SHORT_DELAY_MS);
1034 jsr166 1.9 t.interrupt();
1035     t.join();
1036 jsr166 1.17 assertEquals(SIZE, q.size());
1037     assertEquals(0, q.remainingCapacity());
1038 dl 1.1 }
1039    
1040     /**
1041     * putLast blocks waiting for take when full
1042     */
1043 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1044 jsr166 1.17 final int capacity = 2;
1045     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1046 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1047 jsr166 1.17 public void realRun() throws InterruptedException {
1048     for (int i = 0; i < capacity + 1; i++)
1049     q.putLast(i);
1050 jsr166 1.11 try {
1051 jsr166 1.17 q.putLast(99);
1052     shouldThrow();
1053     } catch (InterruptedException success) {}
1054 jsr166 1.11 }});
1055 jsr166 1.9
1056     t.start();
1057 dl 1.37 delay(SHORT_DELAY_MS);
1058 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1059     assertEquals(0, q.take());
1060 dl 1.37 delay(SHORT_DELAY_MS);
1061 jsr166 1.9 t.interrupt();
1062     t.join();
1063 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1064 dl 1.1 }
1065    
1066     /**
1067     * timed offerLast times out if full and elements not taken
1068     */
1069 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1070 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1071 jsr166 1.38 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1072     Thread t = newStartedThread(new CheckedRunnable() {
1073 jsr166 1.9 public void realRun() throws InterruptedException {
1074     q.putLast(new Object());
1075     q.putLast(new Object());
1076 jsr166 1.38 long startTime = System.nanoTime();
1077     assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1078     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1079     pleaseInterrupt.countDown();
1080 jsr166 1.16 try {
1081 jsr166 1.38 q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1082 jsr166 1.16 shouldThrow();
1083     } catch (InterruptedException success) {}
1084     }});
1085 jsr166 1.4
1086 jsr166 1.38 await(pleaseInterrupt);
1087 jsr166 1.9 t.interrupt();
1088 jsr166 1.38 awaitTermination(t);
1089 dl 1.1 }
1090    
1091     /**
1092     * takeLast retrieves elements in FIFO order
1093     */
1094 jsr166 1.9 public void testTakeLast() throws InterruptedException {
1095     LinkedBlockingDeque q = populatedDeque(SIZE);
1096     for (int i = 0; i < SIZE; ++i) {
1097 jsr166 1.18 assertEquals(SIZE-i-1, q.takeLast());
1098 jsr166 1.7 }
1099 dl 1.1 }
1100    
1101     /**
1102     * takeLast blocks interruptibly when empty
1103     */
1104 jsr166 1.9 public void testTakeLastFromEmpty() throws InterruptedException {
1105 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1106 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1107     public void realRun() throws InterruptedException {
1108     q.takeLast();
1109     }};
1110    
1111     t.start();
1112 dl 1.37 delay(SHORT_DELAY_MS);
1113 jsr166 1.9 t.interrupt();
1114     t.join();
1115 dl 1.1 }
1116    
1117     /**
1118     * TakeLast removes existing elements until empty, then blocks interruptibly
1119     */
1120 jsr166 1.9 public void testBlockingTakeLast() throws InterruptedException {
1121 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
1122     Thread t = new Thread(new CheckedRunnable() {
1123 jsr166 1.9 public void realRun() throws InterruptedException {
1124 jsr166 1.17 for (int i = 0; i < SIZE; ++i)
1125     assertEquals(SIZE - 1 - i, q.takeLast());
1126     try {
1127     q.takeLast();
1128     shouldThrow();
1129     } catch (InterruptedException success) {}
1130     }});
1131 jsr166 1.9
1132 dl 1.1 t.start();
1133 dl 1.37 delay(SHORT_DELAY_MS);
1134 jsr166 1.9 t.interrupt();
1135     t.join();
1136 dl 1.1 }
1137    
1138     /**
1139     * timed pollLast with zero timeout succeeds when non-empty, else times out
1140     */
1141 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1142     LinkedBlockingDeque q = populatedDeque(SIZE);
1143     for (int i = 0; i < SIZE; ++i) {
1144 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1145 jsr166 1.7 }
1146 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1147 dl 1.1 }
1148    
1149     /**
1150     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1151     */
1152 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1153     LinkedBlockingDeque q = populatedDeque(SIZE);
1154     for (int i = 0; i < SIZE; ++i) {
1155 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1156 jsr166 1.7 }
1157 jsr166 1.9 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1158 dl 1.1 }
1159    
1160     /**
1161     * Interrupted timed pollLast throws InterruptedException instead of
1162     * returning timeout status
1163     */
1164 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1165 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
1166 jsr166 1.9 public void realRun() throws InterruptedException {
1167     LinkedBlockingDeque q = populatedDeque(SIZE);
1168     for (int i = 0; i < SIZE; ++i) {
1169 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1170 jsr166 1.9 }
1171 jsr166 1.13 try {
1172     q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1173     shouldThrow();
1174     } catch (InterruptedException success) {}
1175     }});
1176 jsr166 1.9
1177 dl 1.1 t.start();
1178 dl 1.37 delay(SHORT_DELAY_MS);
1179 jsr166 1.9 t.interrupt();
1180     t.join();
1181 dl 1.1 }
1182    
1183     /**
1184 jsr166 1.24 * timed poll before a delayed offerLast fails; after offerLast succeeds;
1185     * on interruption throws
1186 dl 1.1 */
1187 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1188 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1189 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1190     public void realRun() throws InterruptedException {
1191     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1192     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1193     try {
1194     q.poll(LONG_DELAY_MS, MILLISECONDS);
1195     shouldThrow();
1196     } catch (InterruptedException success) {}
1197     }});
1198    
1199     t.start();
1200 dl 1.37 delay(SMALL_DELAY_MS);
1201 jsr166 1.9 assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1202     t.interrupt();
1203     t.join();
1204 jsr166 1.4 }
1205 dl 1.1
1206    
1207     /**
1208     * element returns next element, or throws NSEE if empty
1209     */
1210     public void testElement() {
1211     LinkedBlockingDeque q = populatedDeque(SIZE);
1212     for (int i = 0; i < SIZE; ++i) {
1213 jsr166 1.18 assertEquals(i, q.element());
1214 dl 1.1 q.poll();
1215     }
1216     try {
1217     q.element();
1218     shouldThrow();
1219 jsr166 1.9 } catch (NoSuchElementException success) {}
1220 dl 1.1 }
1221    
1222     /**
1223     * remove(x) removes x and returns true if present
1224     */
1225     public void testRemoveElement() {
1226     LinkedBlockingDeque q = populatedDeque(SIZE);
1227     for (int i = 1; i < SIZE; i+=2) {
1228 jsr166 1.33 assertTrue(q.contains(i));
1229     assertTrue(q.remove(i));
1230     assertFalse(q.contains(i));
1231     assertTrue(q.contains(i-1));
1232 dl 1.1 }
1233     for (int i = 0; i < SIZE; i+=2) {
1234 jsr166 1.33 assertTrue(q.contains(i));
1235     assertTrue(q.remove(i));
1236     assertFalse(q.contains(i));
1237     assertFalse(q.remove(i+1));
1238     assertFalse(q.contains(i+1));
1239 dl 1.1 }
1240     assertTrue(q.isEmpty());
1241     }
1242 jsr166 1.4
1243 dl 1.1 /**
1244     * contains(x) reports true when elements added but not yet removed
1245     */
1246     public void testContains() {
1247     LinkedBlockingDeque q = populatedDeque(SIZE);
1248     for (int i = 0; i < SIZE; ++i) {
1249     assertTrue(q.contains(new Integer(i)));
1250     q.poll();
1251     assertFalse(q.contains(new Integer(i)));
1252     }
1253     }
1254    
1255     /**
1256     * clear removes all elements
1257     */
1258     public void testClear() {
1259     LinkedBlockingDeque q = populatedDeque(SIZE);
1260     q.clear();
1261     assertTrue(q.isEmpty());
1262     assertEquals(0, q.size());
1263     assertEquals(SIZE, q.remainingCapacity());
1264     q.add(one);
1265     assertFalse(q.isEmpty());
1266     assertTrue(q.contains(one));
1267     q.clear();
1268     assertTrue(q.isEmpty());
1269     }
1270    
1271     /**
1272     * containsAll(c) is true when c contains a subset of elements
1273     */
1274     public void testContainsAll() {
1275     LinkedBlockingDeque q = populatedDeque(SIZE);
1276     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1277     for (int i = 0; i < SIZE; ++i) {
1278     assertTrue(q.containsAll(p));
1279     assertFalse(p.containsAll(q));
1280     p.add(new Integer(i));
1281     }
1282     assertTrue(p.containsAll(q));
1283     }
1284    
1285     /**
1286     * retainAll(c) retains only those elements of c and reports true if changed
1287     */
1288     public void testRetainAll() {
1289     LinkedBlockingDeque q = populatedDeque(SIZE);
1290     LinkedBlockingDeque p = populatedDeque(SIZE);
1291     for (int i = 0; i < SIZE; ++i) {
1292     boolean changed = q.retainAll(p);
1293     if (i == 0)
1294     assertFalse(changed);
1295     else
1296     assertTrue(changed);
1297    
1298     assertTrue(q.containsAll(p));
1299     assertEquals(SIZE-i, q.size());
1300     p.remove();
1301     }
1302     }
1303    
1304     /**
1305     * removeAll(c) removes only those elements of c and reports true if changed
1306     */
1307     public void testRemoveAll() {
1308     for (int i = 1; i < SIZE; ++i) {
1309     LinkedBlockingDeque q = populatedDeque(SIZE);
1310     LinkedBlockingDeque p = populatedDeque(i);
1311     assertTrue(q.removeAll(p));
1312     assertEquals(SIZE-i, q.size());
1313     for (int j = 0; j < i; ++j) {
1314     Integer I = (Integer)(p.remove());
1315     assertFalse(q.contains(I));
1316     }
1317     }
1318     }
1319    
1320     /**
1321 jsr166 1.31 * toArray contains all elements in FIFO order
1322 dl 1.1 */
1323 jsr166 1.9 public void testToArray() throws InterruptedException{
1324 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1325 jsr166 1.7 Object[] o = q.toArray();
1326     for (int i = 0; i < o.length; i++)
1327 jsr166 1.31 assertSame(o[i], q.poll());
1328 dl 1.1 }
1329    
1330     /**
1331 jsr166 1.31 * toArray(a) contains all elements in FIFO order
1332 dl 1.1 */
1333 jsr166 1.31 public void testToArray2() {
1334 jsr166 1.32 LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1335 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1336 jsr166 1.32 Integer[] array = q.toArray(ints);
1337     assertSame(ints, array);
1338 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1339 jsr166 1.31 assertSame(ints[i], q.remove());
1340 dl 1.1 }
1341    
1342     /**
1343 jsr166 1.30 * toArray(null) throws NullPointerException
1344 dl 1.1 */
1345 jsr166 1.30 public void testToArray_NullArg() {
1346 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1347 jsr166 1.7 try {
1348 jsr166 1.30 q.toArray(null);
1349 jsr166 1.7 shouldThrow();
1350     } catch (NullPointerException success) {}
1351 dl 1.1 }
1352    
1353     /**
1354 jsr166 1.29 * toArray(incompatible array type) throws ArrayStoreException
1355 dl 1.1 */
1356     public void testToArray1_BadArg() {
1357 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1358 jsr166 1.7 try {
1359 jsr166 1.29 q.toArray(new String[10]);
1360 jsr166 1.7 shouldThrow();
1361 jsr166 1.9 } catch (ArrayStoreException success) {}
1362 dl 1.1 }
1363    
1364 jsr166 1.4
1365 dl 1.1 /**
1366     * iterator iterates through all elements
1367     */
1368 jsr166 1.9 public void testIterator() throws InterruptedException {
1369 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1370 jsr166 1.7 Iterator it = q.iterator();
1371 jsr166 1.9 while (it.hasNext()) {
1372     assertEquals(it.next(), q.take());
1373 jsr166 1.7 }
1374 dl 1.1 }
1375    
1376     /**
1377     * iterator.remove removes current element
1378     */
1379 jsr166 1.21 public void testIteratorRemove() {
1380 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1381     q.add(two);
1382     q.add(one);
1383     q.add(three);
1384    
1385     Iterator it = q.iterator();
1386     it.next();
1387     it.remove();
1388 jsr166 1.4
1389 dl 1.1 it = q.iterator();
1390 jsr166 1.20 assertSame(it.next(), one);
1391     assertSame(it.next(), three);
1392 dl 1.1 assertFalse(it.hasNext());
1393     }
1394    
1395    
1396     /**
1397     * iterator ordering is FIFO
1398     */
1399     public void testIteratorOrdering() {
1400     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1401     q.add(one);
1402     q.add(two);
1403     q.add(three);
1404     assertEquals(0, q.remainingCapacity());
1405     int k = 0;
1406     for (Iterator it = q.iterator(); it.hasNext();) {
1407 jsr166 1.18 assertEquals(++k, it.next());
1408 dl 1.1 }
1409     assertEquals(3, k);
1410     }
1411    
1412     /**
1413     * Modifications do not cause iterators to fail
1414     */
1415 jsr166 1.21 public void testWeaklyConsistentIteration() {
1416 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1417     q.add(one);
1418     q.add(two);
1419     q.add(three);
1420 jsr166 1.9 for (Iterator it = q.iterator(); it.hasNext();) {
1421     q.remove();
1422     it.next();
1423 dl 1.1 }
1424     assertEquals(0, q.size());
1425     }
1426    
1427    
1428     /**
1429 jsr166 1.25 * Descending iterator iterates through all elements
1430 dl 1.2 */
1431     public void testDescendingIterator() {
1432     LinkedBlockingDeque q = populatedDeque(SIZE);
1433     int i = 0;
1434 jsr166 1.7 Iterator it = q.descendingIterator();
1435 jsr166 1.5 while (it.hasNext()) {
1436 dl 1.2 assertTrue(q.contains(it.next()));
1437     ++i;
1438     }
1439     assertEquals(i, SIZE);
1440     assertFalse(it.hasNext());
1441     try {
1442     it.next();
1443 jsr166 1.9 shouldThrow();
1444     } catch (NoSuchElementException success) {}
1445 dl 1.2 }
1446    
1447     /**
1448 jsr166 1.25 * Descending iterator ordering is reverse FIFO
1449 dl 1.2 */
1450     public void testDescendingIteratorOrdering() {
1451     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1452 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1453     q.add(new Integer(3));
1454     q.add(new Integer(2));
1455     q.add(new Integer(1));
1456     int k = 0;
1457     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1458 jsr166 1.18 assertEquals(++k, it.next());
1459 dl 1.3 }
1460 jsr166 1.4
1461 dl 1.3 assertEquals(3, k);
1462     q.remove();
1463     q.remove();
1464     q.remove();
1465 dl 1.2 }
1466     }
1467    
1468     /**
1469     * descendingIterator.remove removes current element
1470     */
1471 jsr166 1.21 public void testDescendingIteratorRemove() {
1472 dl 1.2 final LinkedBlockingDeque q = new LinkedBlockingDeque();
1473 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1474     q.add(new Integer(3));
1475     q.add(new Integer(2));
1476     q.add(new Integer(1));
1477     Iterator it = q.descendingIterator();
1478     assertEquals(it.next(), new Integer(1));
1479     it.remove();
1480     assertEquals(it.next(), new Integer(2));
1481     it = q.descendingIterator();
1482     assertEquals(it.next(), new Integer(2));
1483     assertEquals(it.next(), new Integer(3));
1484     it.remove();
1485     assertFalse(it.hasNext());
1486     q.remove();
1487     }
1488 dl 1.2 }
1489    
1490    
1491     /**
1492 dl 1.1 * toString contains toStrings of elements
1493     */
1494     public void testToString() {
1495     LinkedBlockingDeque q = populatedDeque(SIZE);
1496     String s = q.toString();
1497     for (int i = 0; i < SIZE; ++i) {
1498     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1499     }
1500 jsr166 1.4 }
1501 dl 1.1
1502    
1503     /**
1504     * offer transfers elements across Executor tasks
1505     */
1506     public void testOfferInExecutor() {
1507     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1508     q.add(one);
1509     q.add(two);
1510     ExecutorService executor = Executors.newFixedThreadPool(2);
1511 jsr166 1.9 executor.execute(new CheckedRunnable() {
1512     public void realRun() throws InterruptedException {
1513 jsr166 1.19 assertFalse(q.offer(three));
1514     assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515     assertEquals(0, q.remainingCapacity());
1516 jsr166 1.9 }});
1517    
1518     executor.execute(new CheckedRunnable() {
1519     public void realRun() throws InterruptedException {
1520 dl 1.37 delay(SMALL_DELAY_MS);
1521 jsr166 1.19 assertSame(one, q.take());
1522 jsr166 1.9 }});
1523 jsr166 1.4
1524 dl 1.1 joinPool(executor);
1525     }
1526    
1527     /**
1528     * poll retrieves elements across Executor threads
1529     */
1530     public void testPollInExecutor() {
1531     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1532     ExecutorService executor = Executors.newFixedThreadPool(2);
1533 jsr166 1.9 executor.execute(new CheckedRunnable() {
1534     public void realRun() throws InterruptedException {
1535 jsr166 1.19 assertNull(q.poll());
1536     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537     assertTrue(q.isEmpty());
1538 jsr166 1.9 }});
1539    
1540     executor.execute(new CheckedRunnable() {
1541     public void realRun() throws InterruptedException {
1542 dl 1.37 delay(SMALL_DELAY_MS);
1543 jsr166 1.9 q.put(one);
1544     }});
1545 jsr166 1.4
1546 dl 1.1 joinPool(executor);
1547     }
1548    
1549     /**
1550     * A deserialized serialized deque has same elements in same order
1551     */
1552 jsr166 1.9 public void testSerialization() throws Exception {
1553 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1554    
1555 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1556     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1557     out.writeObject(q);
1558     out.close();
1559    
1560     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1561     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1562     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1563     assertEquals(q.size(), r.size());
1564     while (!q.isEmpty())
1565     assertEquals(q.remove(), r.remove());
1566 dl 1.1 }
1567    
1568     /**
1569     * drainTo(null) throws NPE
1570 jsr166 1.4 */
1571 dl 1.1 public void testDrainToNull() {
1572     LinkedBlockingDeque q = populatedDeque(SIZE);
1573     try {
1574     q.drainTo(null);
1575     shouldThrow();
1576 jsr166 1.9 } catch (NullPointerException success) {}
1577 dl 1.1 }
1578    
1579     /**
1580     * drainTo(this) throws IAE
1581 jsr166 1.4 */
1582 dl 1.1 public void testDrainToSelf() {
1583     LinkedBlockingDeque q = populatedDeque(SIZE);
1584     try {
1585     q.drainTo(q);
1586     shouldThrow();
1587 jsr166 1.10 } catch (IllegalArgumentException success) {}
1588 dl 1.1 }
1589    
1590     /**
1591     * drainTo(c) empties deque into another collection c
1592 jsr166 1.4 */
1593 dl 1.1 public void testDrainTo() {
1594     LinkedBlockingDeque q = populatedDeque(SIZE);
1595     ArrayList l = new ArrayList();
1596     q.drainTo(l);
1597     assertEquals(q.size(), 0);
1598     assertEquals(l.size(), SIZE);
1599 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1600 dl 1.1 assertEquals(l.get(i), new Integer(i));
1601     q.add(zero);
1602     q.add(one);
1603     assertFalse(q.isEmpty());
1604     assertTrue(q.contains(zero));
1605     assertTrue(q.contains(one));
1606     l.clear();
1607     q.drainTo(l);
1608     assertEquals(q.size(), 0);
1609     assertEquals(l.size(), 2);
1610 jsr166 1.4 for (int i = 0; i < 2; ++i)
1611 dl 1.1 assertEquals(l.get(i), new Integer(i));
1612     }
1613    
1614     /**
1615     * drainTo empties full deque, unblocking a waiting put.
1616 jsr166 1.4 */
1617 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1618 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1619 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1620     public void realRun() throws InterruptedException {
1621     q.put(new Integer(SIZE+1));
1622     }});
1623    
1624     t.start();
1625     ArrayList l = new ArrayList();
1626     q.drainTo(l);
1627     assertTrue(l.size() >= SIZE);
1628     for (int i = 0; i < SIZE; ++i)
1629     assertEquals(l.get(i), new Integer(i));
1630     t.join();
1631     assertTrue(q.size() + l.size() >= SIZE);
1632 dl 1.1 }
1633    
1634     /**
1635     * drainTo(null, n) throws NPE
1636 jsr166 1.4 */
1637 dl 1.1 public void testDrainToNullN() {
1638     LinkedBlockingDeque q = populatedDeque(SIZE);
1639     try {
1640     q.drainTo(null, 0);
1641     shouldThrow();
1642 jsr166 1.9 } catch (NullPointerException success) {}
1643 dl 1.1 }
1644    
1645     /**
1646     * drainTo(this, n) throws IAE
1647 jsr166 1.4 */
1648 dl 1.1 public void testDrainToSelfN() {
1649     LinkedBlockingDeque q = populatedDeque(SIZE);
1650     try {
1651     q.drainTo(q, 0);
1652     shouldThrow();
1653 jsr166 1.9 } catch (IllegalArgumentException success) {}
1654 dl 1.1 }
1655    
1656     /**
1657 jsr166 1.26 * drainTo(c, n) empties first min(n, size) elements of queue into c
1658 jsr166 1.4 */
1659 dl 1.1 public void testDrainToN() {
1660     LinkedBlockingDeque q = new LinkedBlockingDeque();
1661     for (int i = 0; i < SIZE + 2; ++i) {
1662 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1663 dl 1.1 assertTrue(q.offer(new Integer(j)));
1664     ArrayList l = new ArrayList();
1665     q.drainTo(l, i);
1666 jsr166 1.27 int k = (i < SIZE) ? i : SIZE;
1667 dl 1.1 assertEquals(l.size(), k);
1668     assertEquals(q.size(), SIZE-k);
1669 jsr166 1.4 for (int j = 0; j < k; ++j)
1670 dl 1.1 assertEquals(l.get(j), new Integer(j));
1671     while (q.poll() != null) ;
1672     }
1673     }
1674    
1675     }