ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.73
Committed: Sat May 13 22:38:09 2017 UTC (6 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.72: +1 -1 lines
Log Message:
claw back some millis using assertThreadBlocks

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