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

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.7 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.40 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.12 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.54 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10    
11     import java.util.ArrayList;
12 jsr166 1.45 import java.util.Arrays;
13     import java.util.Collection;
14     import java.util.Iterator;
15     import java.util.NoSuchElementException;
16 jsr166 1.46 import java.util.Queue;
17 jsr166 1.45 import java.util.concurrent.BlockingQueue;
18     import java.util.concurrent.CountDownLatch;
19     import java.util.concurrent.Executors;
20     import java.util.concurrent.ExecutorService;
21 jsr166 1.54 import java.util.concurrent.LinkedBlockingQueue;
22    
23     import junit.framework.Test;
24 dl 1.1
25 dl 1.3 public class LinkedBlockingQueueTest extends JSR166TestCase {
26 dl 1.1
27 jsr166 1.29 public static class Unbounded extends BlockingQueueTest {
28     protected BlockingQueue emptyCollection() {
29     return new LinkedBlockingQueue();
30     }
31     }
32    
33     public static class Bounded extends BlockingQueueTest {
34     protected BlockingQueue emptyCollection() {
35 jsr166 1.47 return new LinkedBlockingQueue(SIZE);
36 jsr166 1.29 }
37     }
38    
39 dl 1.1 public static void main(String[] args) {
40 jsr166 1.59 main(suite(), args);
41 dl 1.1 }
42    
43     public static Test suite() {
44 jsr166 1.29 return newTestSuite(LinkedBlockingQueueTest.class,
45     new Unbounded().testSuite(),
46     new Bounded().testSuite());
47 dl 1.1 }
48    
49     /**
50 jsr166 1.51 * Returns a new queue of given size containing consecutive
51 dl 1.1 * Integers 0 ... n.
52     */
53 jsr166 1.37 private LinkedBlockingQueue<Integer> populatedQueue(int n) {
54     LinkedBlockingQueue<Integer> q =
55     new LinkedBlockingQueue<Integer>(n);
56 dl 1.1 assertTrue(q.isEmpty());
57 jsr166 1.15 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.15 assertEquals(n, q.size());
62 dl 1.1 return q;
63     }
64 jsr166 1.12
65 dl 1.4 /**
66 dl 1.5 * A new queue has the indicated capacity, or Integer.MAX_VALUE if
67     * none given
68 dl 1.4 */
69     public void testConstructor1() {
70 dl 1.3 assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
71 dl 1.5 assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
72 dl 1.1 }
73    
74 dl 1.4 /**
75 jsr166 1.45 * Constructor throws IllegalArgumentException if capacity argument nonpositive
76 dl 1.4 */
77     public void testConstructor2() {
78 dl 1.1 try {
79 jsr166 1.45 new LinkedBlockingQueue(0);
80 dl 1.4 shouldThrow();
81 jsr166 1.17 } catch (IllegalArgumentException success) {}
82 dl 1.1 }
83    
84 dl 1.4 /**
85 jsr166 1.45 * Initializing from null Collection throws NullPointerException
86 dl 1.4 */
87     public void testConstructor3() {
88 dl 1.1 try {
89 jsr166 1.45 new LinkedBlockingQueue(null);
90 dl 1.4 shouldThrow();
91 jsr166 1.17 } catch (NullPointerException success) {}
92 dl 1.1 }
93    
94 dl 1.4 /**
95 jsr166 1.45 * Initializing from Collection of null elements throws NullPointerException
96 dl 1.4 */
97     public void testConstructor4() {
98 jsr166 1.45 Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
99 dl 1.1 try {
100 jsr166 1.45 new LinkedBlockingQueue(elements);
101 dl 1.4 shouldThrow();
102 jsr166 1.17 } catch (NullPointerException success) {}
103 dl 1.1 }
104    
105 dl 1.4 /**
106 jsr166 1.45 * Initializing from Collection with some null elements throws
107     * NullPointerException
108 dl 1.4 */
109     public void testConstructor5() {
110 jsr166 1.45 Integer[] ints = new Integer[SIZE];
111 jsr166 1.60 for (int i = 0; i < SIZE - 1; ++i)
112 jsr166 1.45 ints[i] = new Integer(i);
113     Collection<Integer> elements = Arrays.asList(ints);
114 dl 1.1 try {
115 jsr166 1.45 new LinkedBlockingQueue(elements);
116 dl 1.4 shouldThrow();
117 jsr166 1.17 } catch (NullPointerException success) {}
118 dl 1.1 }
119    
120 dl 1.4 /**
121 dl 1.5 * Queue contains all elements of collection used to initialize
122 dl 1.4 */
123     public void testConstructor6() {
124 jsr166 1.17 Integer[] ints = new Integer[SIZE];
125     for (int i = 0; i < SIZE; ++i)
126     ints[i] = new Integer(i);
127     LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints));
128     for (int i = 0; i < SIZE; ++i)
129     assertEquals(ints[i], q.poll());
130 dl 1.1 }
131    
132 dl 1.4 /**
133 dl 1.5 * Queue transitions from empty to full when elements added
134 dl 1.4 */
135 dl 1.1 public void testEmptyFull() {
136     LinkedBlockingQueue q = new LinkedBlockingQueue(2);
137     assertTrue(q.isEmpty());
138     assertEquals("should have room for 2", 2, q.remainingCapacity());
139 dl 1.3 q.add(one);
140 dl 1.1 assertFalse(q.isEmpty());
141 dl 1.3 q.add(two);
142 dl 1.1 assertFalse(q.isEmpty());
143 dl 1.4 assertEquals(0, q.remainingCapacity());
144     assertFalse(q.offer(three));
145 dl 1.1 }
146    
147 dl 1.4 /**
148 dl 1.5 * remainingCapacity decreases on add, increases on remove
149 dl 1.4 */
150     public void testRemainingCapacity() {
151 jsr166 1.58 BlockingQueue q = populatedQueue(SIZE);
152 dl 1.3 for (int i = 0; i < SIZE; ++i) {
153 dl 1.1 assertEquals(i, q.remainingCapacity());
154 jsr166 1.58 assertEquals(SIZE, q.size() + q.remainingCapacity());
155     assertEquals(i, q.remove());
156 dl 1.1 }
157 dl 1.3 for (int i = 0; i < SIZE; ++i) {
158 jsr166 1.60 assertEquals(SIZE - i, q.remainingCapacity());
159 jsr166 1.58 assertEquals(SIZE, q.size() + q.remainingCapacity());
160     assertTrue(q.add(i));
161 dl 1.1 }
162     }
163    
164 dl 1.4 /**
165 dl 1.5 * Offer succeeds if not full; fails if full
166 dl 1.4 */
167     public void testOffer() {
168 dl 1.1 LinkedBlockingQueue q = new LinkedBlockingQueue(1);
169 dl 1.3 assertTrue(q.offer(zero));
170     assertFalse(q.offer(one));
171 dl 1.1 }
172    
173 dl 1.4 /**
174 jsr166 1.45 * add succeeds if not full; throws IllegalStateException if full
175 dl 1.4 */
176     public void testAdd() {
177 jsr166 1.45 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
178     for (int i = 0; i < SIZE; ++i)
179     assertTrue(q.add(new Integer(i)));
180     assertEquals(0, q.remainingCapacity());
181 jsr166 1.15 try {
182 dl 1.3 q.add(new Integer(SIZE));
183 jsr166 1.17 shouldThrow();
184     } catch (IllegalStateException success) {}
185 dl 1.1 }
186    
187 dl 1.4 /**
188 jsr166 1.45 * addAll(this) throws IllegalArgumentException
189 dl 1.6 */
190     public void testAddAllSelf() {
191 jsr166 1.45 LinkedBlockingQueue q = populatedQueue(SIZE);
192 dl 1.6 try {
193     q.addAll(q);
194     shouldThrow();
195 jsr166 1.17 } catch (IllegalArgumentException success) {}
196 dl 1.6 }
197    
198 dl 1.4 /**
199 dl 1.5 * addAll of a collection with any null elements throws NPE after
200     * possibly adding some elements
201 dl 1.4 */
202     public void testAddAll3() {
203 jsr166 1.45 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
204     Integer[] ints = new Integer[SIZE];
205 jsr166 1.60 for (int i = 0; i < SIZE - 1; ++i)
206 jsr166 1.45 ints[i] = new Integer(i);
207     Collection<Integer> elements = Arrays.asList(ints);
208 dl 1.1 try {
209 jsr166 1.45 q.addAll(elements);
210 dl 1.4 shouldThrow();
211 jsr166 1.17 } catch (NullPointerException success) {}
212 dl 1.1 }
213 jsr166 1.27
214 dl 1.4 /**
215 jsr166 1.45 * addAll throws IllegalStateException if not enough room
216 dl 1.4 */
217     public void testAddAll4() {
218 jsr166 1.45 LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
219     Integer[] ints = new Integer[SIZE];
220     for (int i = 0; i < SIZE; ++i)
221     ints[i] = new Integer(i);
222     Collection<Integer> elements = Arrays.asList(ints);
223 dl 1.1 try {
224 jsr166 1.45 q.addAll(elements);
225 dl 1.4 shouldThrow();
226 jsr166 1.17 } catch (IllegalStateException success) {}
227 dl 1.1 }
228 jsr166 1.27
229 dl 1.4 /**
230 dl 1.5 * Queue contains all elements, in traversal order, of successful addAll
231 dl 1.4 */
232     public void testAddAll5() {
233 jsr166 1.17 Integer[] empty = new Integer[0];
234     Integer[] ints = new Integer[SIZE];
235     for (int i = 0; i < SIZE; ++i)
236     ints[i] = new Integer(i);
237     LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
238     assertFalse(q.addAll(Arrays.asList(empty)));
239     assertTrue(q.addAll(Arrays.asList(ints)));
240     for (int i = 0; i < SIZE; ++i)
241     assertEquals(ints[i], q.poll());
242 dl 1.1 }
243    
244 dl 1.4 /**
245 dl 1.5 * all elements successfully put are contained
246 dl 1.4 */
247 jsr166 1.17 public void testPut() throws InterruptedException {
248     LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
249     for (int i = 0; i < SIZE; ++i) {
250 jsr166 1.56 Integer x = new Integer(i);
251     q.put(x);
252     assertTrue(q.contains(x));
253 dl 1.1 }
254 jsr166 1.17 assertEquals(0, q.remainingCapacity());
255 dl 1.1 }
256    
257 dl 1.4 /**
258 dl 1.5 * put blocks interruptibly if full
259 dl 1.4 */
260 jsr166 1.17 public void testBlockingPut() throws InterruptedException {
261 jsr166 1.22 final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
262 jsr166 1.44 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
263     Thread t = newStartedThread(new CheckedRunnable() {
264 jsr166 1.22 public void realRun() throws InterruptedException {
265     for (int i = 0; i < SIZE; ++i)
266     q.put(i);
267     assertEquals(SIZE, q.size());
268     assertEquals(0, q.remainingCapacity());
269 jsr166 1.44
270     Thread.currentThread().interrupt();
271     try {
272     q.put(99);
273     shouldThrow();
274     } catch (InterruptedException success) {}
275     assertFalse(Thread.interrupted());
276    
277     pleaseInterrupt.countDown();
278 jsr166 1.17 try {
279 jsr166 1.22 q.put(99);
280     shouldThrow();
281     } catch (InterruptedException success) {}
282 jsr166 1.44 assertFalse(Thread.interrupted());
283 jsr166 1.17 }});
284    
285 jsr166 1.44 await(pleaseInterrupt);
286     assertThreadStaysAlive(t);
287 jsr166 1.17 t.interrupt();
288 jsr166 1.44 awaitTermination(t);
289 jsr166 1.22 assertEquals(SIZE, q.size());
290     assertEquals(0, q.remainingCapacity());
291 dl 1.1 }
292    
293 dl 1.4 /**
294 jsr166 1.44 * put blocks interruptibly waiting for take when full
295 dl 1.4 */
296 jsr166 1.17 public void testPutWithTake() throws InterruptedException {
297 jsr166 1.22 final int capacity = 2;
298 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
299 jsr166 1.44 final CountDownLatch pleaseTake = new CountDownLatch(1);
300     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
301     Thread t = newStartedThread(new CheckedRunnable() {
302 jsr166 1.22 public void realRun() throws InterruptedException {
303 jsr166 1.44 for (int i = 0; i < capacity; i++)
304 jsr166 1.22 q.put(i);
305 jsr166 1.44 pleaseTake.countDown();
306     q.put(86);
307    
308     pleaseInterrupt.countDown();
309 jsr166 1.17 try {
310 jsr166 1.22 q.put(99);
311     shouldThrow();
312     } catch (InterruptedException success) {}
313 jsr166 1.44 assertFalse(Thread.interrupted());
314 jsr166 1.17 }});
315    
316 jsr166 1.44 await(pleaseTake);
317 jsr166 1.48 assertEquals(0, q.remainingCapacity());
318 jsr166 1.22 assertEquals(0, q.take());
319 jsr166 1.44
320     await(pleaseInterrupt);
321     assertThreadStaysAlive(t);
322 jsr166 1.17 t.interrupt();
323 jsr166 1.44 awaitTermination(t);
324 jsr166 1.48 assertEquals(0, q.remainingCapacity());
325 dl 1.1 }
326    
327 dl 1.4 /**
328 dl 1.5 * timed offer times out if full and elements not taken
329 dl 1.4 */
330 jsr166 1.43 public void testTimedOffer() {
331 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
332 jsr166 1.43 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
333     Thread t = newStartedThread(new CheckedRunnable() {
334 jsr166 1.17 public void realRun() throws InterruptedException {
335     q.put(new Object());
336     q.put(new Object());
337 jsr166 1.43 long startTime = System.nanoTime();
338     assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
339     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
340     pleaseInterrupt.countDown();
341 jsr166 1.21 try {
342 jsr166 1.43 q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
343 jsr166 1.21 shouldThrow();
344     } catch (InterruptedException success) {}
345     }});
346 jsr166 1.12
347 jsr166 1.43 await(pleaseInterrupt);
348 jsr166 1.44 assertThreadStaysAlive(t);
349 jsr166 1.17 t.interrupt();
350 jsr166 1.43 awaitTermination(t);
351 dl 1.1 }
352    
353 dl 1.4 /**
354 dl 1.5 * take retrieves elements in FIFO order
355 dl 1.4 */
356 jsr166 1.17 public void testTake() throws InterruptedException {
357     LinkedBlockingQueue q = populatedQueue(SIZE);
358     for (int i = 0; i < SIZE; ++i) {
359 jsr166 1.23 assertEquals(i, q.take());
360 jsr166 1.15 }
361 dl 1.1 }
362    
363 dl 1.4 /**
364 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
365 dl 1.4 */
366 jsr166 1.17 public void testBlockingTake() throws InterruptedException {
367 jsr166 1.44 final BlockingQueue q = populatedQueue(SIZE);
368     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369     Thread t = newStartedThread(new CheckedRunnable() {
370 jsr166 1.17 public void realRun() throws InterruptedException {
371     for (int i = 0; i < SIZE; ++i) {
372 jsr166 1.23 assertEquals(i, q.take());
373 jsr166 1.17 }
374 jsr166 1.44
375     Thread.currentThread().interrupt();
376     try {
377     q.take();
378     shouldThrow();
379     } catch (InterruptedException success) {}
380     assertFalse(Thread.interrupted());
381    
382     pleaseInterrupt.countDown();
383 jsr166 1.22 try {
384     q.take();
385     shouldThrow();
386     } catch (InterruptedException success) {}
387 jsr166 1.44 assertFalse(Thread.interrupted());
388 jsr166 1.22 }});
389 jsr166 1.17
390 jsr166 1.44 await(pleaseInterrupt);
391     assertThreadStaysAlive(t);
392 jsr166 1.17 t.interrupt();
393 jsr166 1.44 awaitTermination(t);
394 dl 1.1 }
395    
396 dl 1.4 /**
397 dl 1.5 * poll succeeds unless empty
398 dl 1.4 */
399     public void testPoll() {
400 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
401     for (int i = 0; i < SIZE; ++i) {
402 jsr166 1.23 assertEquals(i, q.poll());
403 dl 1.1 }
404 jsr166 1.15 assertNull(q.poll());
405 dl 1.1 }
406    
407 dl 1.4 /**
408 jsr166 1.33 * timed poll with zero timeout succeeds when non-empty, else times out
409 dl 1.4 */
410 jsr166 1.17 public void testTimedPoll0() throws InterruptedException {
411     LinkedBlockingQueue q = populatedQueue(SIZE);
412     for (int i = 0; i < SIZE; ++i) {
413 jsr166 1.23 assertEquals(i, q.poll(0, MILLISECONDS));
414 jsr166 1.15 }
415 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
416 dl 1.1 }
417    
418 dl 1.4 /**
419 jsr166 1.33 * timed poll with nonzero timeout succeeds when non-empty, else times out
420 dl 1.4 */
421 jsr166 1.17 public void testTimedPoll() throws InterruptedException {
422 jsr166 1.44 LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
423 jsr166 1.17 for (int i = 0; i < SIZE; ++i) {
424 jsr166 1.44 long startTime = System.nanoTime();
425     assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
426     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
427     }
428     long startTime = System.nanoTime();
429     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
430     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
431     checkEmpty(q);
432 dl 1.1 }
433    
434 dl 1.4 /**
435 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
436     * returning timeout status
437 dl 1.4 */
438 jsr166 1.17 public void testInterruptedTimedPoll() throws InterruptedException {
439 jsr166 1.39 final BlockingQueue<Integer> q = populatedQueue(SIZE);
440     final CountDownLatch aboutToWait = new CountDownLatch(1);
441     Thread t = newStartedThread(new CheckedRunnable() {
442 jsr166 1.17 public void realRun() throws InterruptedException {
443 jsr166 1.62 long startTime = System.nanoTime();
444 jsr166 1.17 for (int i = 0; i < SIZE; ++i) {
445 jsr166 1.39 assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
446 jsr166 1.17 }
447 jsr166 1.39 aboutToWait.countDown();
448 jsr166 1.19 try {
449 jsr166 1.62 q.poll(LONG_DELAY_MS, MILLISECONDS);
450 jsr166 1.19 shouldThrow();
451 jsr166 1.39 } catch (InterruptedException success) {
452 jsr166 1.62 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
453 jsr166 1.39 }
454 jsr166 1.19 }});
455 jsr166 1.17
456 jsr166 1.62 await(aboutToWait);
457 jsr166 1.63 waitForThreadToEnterWaitState(t);
458 jsr166 1.17 t.interrupt();
459 jsr166 1.62 awaitTermination(t);
460 jsr166 1.39 checkEmpty(q);
461 dl 1.1 }
462    
463 dl 1.4 /**
464 dl 1.5 * peek returns next element, or null if empty
465 dl 1.4 */
466     public void testPeek() {
467 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
468     for (int i = 0; i < SIZE; ++i) {
469 jsr166 1.23 assertEquals(i, q.peek());
470     assertEquals(i, q.poll());
471 dl 1.1 assertTrue(q.peek() == null ||
472 jsr166 1.23 !q.peek().equals(i));
473 dl 1.1 }
474 jsr166 1.15 assertNull(q.peek());
475 dl 1.1 }
476    
477 dl 1.4 /**
478 dl 1.5 * element returns next element, or throws NSEE if empty
479 dl 1.4 */
480     public void testElement() {
481 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
482     for (int i = 0; i < SIZE; ++i) {
483 jsr166 1.23 assertEquals(i, q.element());
484     assertEquals(i, q.poll());
485 dl 1.1 }
486     try {
487     q.element();
488 dl 1.4 shouldThrow();
489 jsr166 1.17 } catch (NoSuchElementException success) {}
490 dl 1.1 }
491    
492 dl 1.4 /**
493 dl 1.5 * remove removes next element, or throws NSEE if empty
494 dl 1.4 */
495     public void testRemove() {
496 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
497     for (int i = 0; i < SIZE; ++i) {
498 jsr166 1.23 assertEquals(i, q.remove());
499 dl 1.1 }
500     try {
501     q.remove();
502 dl 1.4 shouldThrow();
503 jsr166 1.17 } catch (NoSuchElementException success) {}
504 dl 1.1 }
505    
506 dl 1.4 /**
507 dl 1.11 * An add following remove(x) succeeds
508     */
509 jsr166 1.17 public void testRemoveElementAndAdd() throws InterruptedException {
510     LinkedBlockingQueue q = new LinkedBlockingQueue();
511     assertTrue(q.add(new Integer(1)));
512     assertTrue(q.add(new Integer(2)));
513     assertTrue(q.remove(new Integer(1)));
514     assertTrue(q.remove(new Integer(2)));
515     assertTrue(q.add(new Integer(3)));
516 jsr166 1.52 assertNotNull(q.take());
517 dl 1.11 }
518 jsr166 1.12
519 dl 1.4 /**
520 dl 1.5 * contains(x) reports true when elements added but not yet removed
521 dl 1.4 */
522     public void testContains() {
523 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
524     for (int i = 0; i < SIZE; ++i) {
525 dl 1.1 assertTrue(q.contains(new Integer(i)));
526     q.poll();
527     assertFalse(q.contains(new Integer(i)));
528     }
529     }
530    
531 dl 1.4 /**
532 dl 1.5 * clear removes all elements
533 dl 1.4 */
534     public void testClear() {
535 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
536 dl 1.1 q.clear();
537     assertTrue(q.isEmpty());
538     assertEquals(0, q.size());
539 dl 1.3 assertEquals(SIZE, q.remainingCapacity());
540     q.add(one);
541 dl 1.1 assertFalse(q.isEmpty());
542 dl 1.10 assertTrue(q.contains(one));
543 dl 1.1 q.clear();
544     assertTrue(q.isEmpty());
545     }
546    
547 dl 1.4 /**
548 dl 1.5 * containsAll(c) is true when c contains a subset of elements
549 dl 1.4 */
550     public void testContainsAll() {
551 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
552     LinkedBlockingQueue p = new LinkedBlockingQueue(SIZE);
553     for (int i = 0; i < SIZE; ++i) {
554 dl 1.1 assertTrue(q.containsAll(p));
555     assertFalse(p.containsAll(q));
556     p.add(new Integer(i));
557     }
558     assertTrue(p.containsAll(q));
559     }
560    
561 dl 1.4 /**
562 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
563 dl 1.4 */
564     public void testRetainAll() {
565 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
566     LinkedBlockingQueue p = populatedQueue(SIZE);
567     for (int i = 0; i < SIZE; ++i) {
568 dl 1.1 boolean changed = q.retainAll(p);
569     if (i == 0)
570     assertFalse(changed);
571     else
572     assertTrue(changed);
573    
574     assertTrue(q.containsAll(p));
575 jsr166 1.60 assertEquals(SIZE - i, q.size());
576 dl 1.1 p.remove();
577     }
578     }
579    
580 dl 1.4 /**
581 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
582 dl 1.4 */
583     public void testRemoveAll() {
584 dl 1.3 for (int i = 1; i < SIZE; ++i) {
585     LinkedBlockingQueue q = populatedQueue(SIZE);
586     LinkedBlockingQueue p = populatedQueue(i);
587 dl 1.1 assertTrue(q.removeAll(p));
588 jsr166 1.60 assertEquals(SIZE - i, q.size());
589 dl 1.1 for (int j = 0; j < i; ++j) {
590 jsr166 1.56 Integer x = (Integer)(p.remove());
591     assertFalse(q.contains(x));
592 dl 1.1 }
593     }
594     }
595    
596 dl 1.4 /**
597 jsr166 1.36 * toArray contains all elements in FIFO order
598 dl 1.4 */
599 jsr166 1.36 public void testToArray() {
600 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
601 jsr166 1.15 Object[] o = q.toArray();
602     for (int i = 0; i < o.length; i++)
603 jsr166 1.36 assertSame(o[i], q.poll());
604 dl 1.1 }
605    
606 dl 1.4 /**
607 jsr166 1.36 * toArray(a) contains all elements in FIFO order
608 dl 1.4 */
609 jsr166 1.17 public void testToArray2() throws InterruptedException {
610 jsr166 1.37 LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
611 jsr166 1.15 Integer[] ints = new Integer[SIZE];
612 jsr166 1.37 Integer[] array = q.toArray(ints);
613     assertSame(ints, array);
614 jsr166 1.17 for (int i = 0; i < ints.length; i++)
615 jsr166 1.36 assertSame(ints[i], q.poll());
616 dl 1.1 }
617 dl 1.6
618     /**
619 jsr166 1.34 * toArray(incompatible array type) throws ArrayStoreException
620 dl 1.6 */
621     public void testToArray1_BadArg() {
622 jsr166 1.23 LinkedBlockingQueue q = populatedQueue(SIZE);
623 jsr166 1.15 try {
624 jsr166 1.34 q.toArray(new String[10]);
625 jsr166 1.15 shouldThrow();
626 jsr166 1.17 } catch (ArrayStoreException success) {}
627 dl 1.6 }
628    
629 dl 1.4 /**
630 dl 1.5 * iterator iterates through all elements
631 dl 1.4 */
632 jsr166 1.17 public void testIterator() throws InterruptedException {
633 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
634 jsr166 1.15 Iterator it = q.iterator();
635 jsr166 1.57 int i;
636     for (i = 0; it.hasNext(); i++)
637     assertTrue(q.contains(it.next()));
638     assertEquals(i, SIZE);
639     assertIteratorExhausted(it);
640    
641     it = q.iterator();
642     for (i = 0; it.hasNext(); i++)
643 jsr166 1.17 assertEquals(it.next(), q.take());
644 jsr166 1.57 assertEquals(i, SIZE);
645     assertIteratorExhausted(it);
646     }
647    
648     /**
649     * iterator of empty collection has no elements
650     */
651     public void testEmptyIterator() {
652     assertIteratorExhausted(new LinkedBlockingQueue().iterator());
653 dl 1.1 }
654    
655 dl 1.4 /**
656 dl 1.5 * iterator.remove removes current element
657     */
658 jsr166 1.26 public void testIteratorRemove() {
659 dl 1.5 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
660     q.add(two);
661     q.add(one);
662     q.add(three);
663    
664     Iterator it = q.iterator();
665     it.next();
666     it.remove();
667 jsr166 1.12
668 dl 1.5 it = q.iterator();
669 jsr166 1.25 assertSame(it.next(), one);
670     assertSame(it.next(), three);
671 dl 1.5 assertFalse(it.hasNext());
672     }
673    
674     /**
675     * iterator ordering is FIFO
676 dl 1.4 */
677 dl 1.1 public void testIteratorOrdering() {
678     final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
679 dl 1.3 q.add(one);
680     q.add(two);
681     q.add(three);
682 dl 1.4 assertEquals(0, q.remainingCapacity());
683 dl 1.1 int k = 0;
684     for (Iterator it = q.iterator(); it.hasNext();) {
685 jsr166 1.23 assertEquals(++k, it.next());
686 dl 1.1 }
687 dl 1.4 assertEquals(3, k);
688 dl 1.1 }
689    
690 dl 1.4 /**
691 dl 1.5 * Modifications do not cause iterators to fail
692 dl 1.4 */
693 jsr166 1.26 public void testWeaklyConsistentIteration() {
694 dl 1.1 final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
695 dl 1.3 q.add(one);
696     q.add(two);
697     q.add(three);
698 jsr166 1.17 for (Iterator it = q.iterator(); it.hasNext();) {
699     q.remove();
700     it.next();
701 dl 1.1 }
702 dl 1.4 assertEquals(0, q.size());
703 dl 1.1 }
704    
705 dl 1.4 /**
706 dl 1.5 * toString contains toStrings of elements
707 dl 1.4 */
708     public void testToString() {
709 dl 1.3 LinkedBlockingQueue q = populatedQueue(SIZE);
710 dl 1.1 String s = q.toString();
711 dl 1.3 for (int i = 0; i < SIZE; ++i) {
712 jsr166 1.44 assertTrue(s.contains(String.valueOf(i)));
713 dl 1.1 }
714 jsr166 1.12 }
715 dl 1.1
716 dl 1.4 /**
717 dl 1.5 * offer transfers elements across Executor tasks
718 dl 1.4 */
719 dl 1.1 public void testOfferInExecutor() {
720     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
721 dl 1.3 q.add(one);
722     q.add(two);
723 jsr166 1.44 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
724 jsr166 1.61 final ExecutorService executor = Executors.newFixedThreadPool(2);
725     try (PoolCleaner cleaner = cleaner(executor)) {
726     executor.execute(new CheckedRunnable() {
727     public void realRun() throws InterruptedException {
728     assertFalse(q.offer(three));
729     threadsStarted.await();
730     assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
731     assertEquals(0, q.remainingCapacity());
732     }});
733    
734     executor.execute(new CheckedRunnable() {
735     public void realRun() throws InterruptedException {
736     threadsStarted.await();
737     assertSame(one, q.take());
738     }});
739     }
740 dl 1.1 }
741    
742 dl 1.4 /**
743 jsr166 1.44 * timed poll retrieves elements across Executor threads
744 dl 1.4 */
745 dl 1.1 public void testPollInExecutor() {
746     final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
747 jsr166 1.44 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
748 jsr166 1.61 final ExecutorService executor = Executors.newFixedThreadPool(2);
749     try (PoolCleaner cleaner = cleaner(executor)) {
750     executor.execute(new CheckedRunnable() {
751     public void realRun() throws InterruptedException {
752     assertNull(q.poll());
753     threadsStarted.await();
754     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
755     checkEmpty(q);
756     }});
757    
758     executor.execute(new CheckedRunnable() {
759     public void realRun() throws InterruptedException {
760     threadsStarted.await();
761     q.put(one);
762     }});
763     }
764 dl 1.2 }
765    
766 dl 1.4 /**
767 dl 1.5 * A deserialized serialized queue has same elements in same order
768 dl 1.4 */
769 jsr166 1.17 public void testSerialization() throws Exception {
770 jsr166 1.46 Queue x = populatedQueue(SIZE);
771     Queue y = serialClone(x);
772 dl 1.2
773 jsr166 1.52 assertNotSame(x, y);
774 jsr166 1.46 assertEquals(x.size(), y.size());
775     assertEquals(x.toString(), y.toString());
776     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
777     while (!x.isEmpty()) {
778     assertFalse(y.isEmpty());
779     assertEquals(x.remove(), y.remove());
780     }
781     assertTrue(y.isEmpty());
782 dl 1.6 }
783    
784     /**
785     * drainTo(c) empties queue into another collection c
786 jsr166 1.12 */
787 dl 1.6 public void testDrainTo() {
788     LinkedBlockingQueue q = populatedQueue(SIZE);
789     ArrayList l = new ArrayList();
790     q.drainTo(l);
791 jsr166 1.48 assertEquals(0, q.size());
792     assertEquals(SIZE, l.size());
793 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
794 dl 1.6 assertEquals(l.get(i), new Integer(i));
795 dl 1.10 q.add(zero);
796     q.add(one);
797     assertFalse(q.isEmpty());
798     assertTrue(q.contains(zero));
799     assertTrue(q.contains(one));
800     l.clear();
801     q.drainTo(l);
802 jsr166 1.48 assertEquals(0, q.size());
803     assertEquals(2, l.size());
804 jsr166 1.12 for (int i = 0; i < 2; ++i)
805 dl 1.10 assertEquals(l.get(i), new Integer(i));
806 dl 1.6 }
807    
808     /**
809     * drainTo empties full queue, unblocking a waiting put.
810 jsr166 1.12 */
811 jsr166 1.17 public void testDrainToWithActivePut() throws InterruptedException {
812 dl 1.6 final LinkedBlockingQueue q = populatedQueue(SIZE);
813 jsr166 1.17 Thread t = new Thread(new CheckedRunnable() {
814     public void realRun() throws InterruptedException {
815 jsr166 1.60 q.put(new Integer(SIZE + 1));
816 jsr166 1.17 }});
817    
818     t.start();
819     ArrayList l = new ArrayList();
820     q.drainTo(l);
821     assertTrue(l.size() >= SIZE);
822     for (int i = 0; i < SIZE; ++i)
823     assertEquals(l.get(i), new Integer(i));
824     t.join();
825     assertTrue(q.size() + l.size() >= SIZE);
826 dl 1.6 }
827    
828     /**
829 jsr166 1.30 * drainTo(c, n) empties first min(n, size) elements of queue into c
830 jsr166 1.12 */
831 dl 1.6 public void testDrainToN() {
832 dl 1.10 LinkedBlockingQueue q = new LinkedBlockingQueue();
833 dl 1.6 for (int i = 0; i < SIZE + 2; ++i) {
834 jsr166 1.13 for (int j = 0; j < SIZE; j++)
835 dl 1.10 assertTrue(q.offer(new Integer(j)));
836 dl 1.6 ArrayList l = new ArrayList();
837     q.drainTo(l, i);
838 jsr166 1.31 int k = (i < SIZE) ? i : SIZE;
839 jsr166 1.49 assertEquals(k, l.size());
840 jsr166 1.60 assertEquals(SIZE - k, q.size());
841 jsr166 1.12 for (int j = 0; j < k; ++j)
842 dl 1.6 assertEquals(l.get(j), new Integer(j));
843 jsr166 1.55 do {} while (q.poll() != null);
844 dl 1.2 }
845 dl 1.1 }
846    
847 jsr166 1.53 /**
848     * remove(null), contains(null) always return false
849     */
850     public void testNeverContainsNull() {
851     Collection<?>[] qs = {
852     new LinkedBlockingQueue<Object>(),
853     populatedQueue(2),
854     };
855    
856     for (Collection<?> q : qs) {
857     assertFalse(q.contains(null));
858     assertFalse(q.remove(null));
859     }
860     }
861    
862 dl 1.1 }