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