ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.83
Committed: Thu Sep 5 20:54:24 2019 UTC (4 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.82: +1 -1 lines
Log Message:
testTimedOffer: rely on awaitTermination together with LONGER_DELAY_MS

File Contents

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