ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.64
Committed: Sat Aug 6 17:02:49 2016 UTC (7 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.63: +1 -1 lines
Log Message:
simplify calls to waitForThreadToEnterWaitState

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