9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
+ |
|
17 |
+ |
public static class Generic extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new PriorityBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class InitialCapacity extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new PriorityBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
+ |
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
34 |
> |
return newTestSuite(PriorityBlockingQueueTest.class, |
35 |
> |
new Generic().testSuite(), |
36 |
> |
new InitialCapacity().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
41 |
|
/** Sample Comparator */ |
42 |
|
static class MyReverseComparator implements Comparator { |
43 |
|
public int compare(Object x, Object y) { |
44 |
< |
int i = ((Integer)x).intValue(); |
28 |
< |
int j = ((Integer)y).intValue(); |
29 |
< |
if (i < j) return 1; |
30 |
< |
if (i > j) return -1; |
31 |
< |
return 0; |
44 |
> |
return ((Comparable)y).compareTo(x); |
45 |
|
} |
46 |
|
} |
47 |
|
|
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Constructor throws IAE if capacity argument nonpositive |
73 |
> |
* Constructor throws IAE if capacity argument nonpositive |
74 |
|
*/ |
75 |
|
public void testConstructor2() { |
76 |
|
try { |
261 |
|
shouldThrow(); |
262 |
|
} catch (NullPointerException success) {} |
263 |
|
} |
264 |
+ |
|
265 |
|
/** |
266 |
|
* addAll of a collection with any null elements throws NPE after |
267 |
|
* possibly adding some elements |
341 |
|
*/ |
342 |
|
public void testTimedOffer() throws InterruptedException { |
343 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
344 |
< |
Thread t = new Thread(new Runnable() { |
345 |
< |
public void run() { |
346 |
< |
try { |
347 |
< |
q.put(new Integer(0)); |
348 |
< |
q.put(new Integer(0)); |
349 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
350 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
337 |
< |
} finally { } |
338 |
< |
} |
339 |
< |
}); |
344 |
> |
Thread t = new Thread(new CheckedRunnable() { |
345 |
> |
public void realRun() { |
346 |
> |
q.put(new Integer(0)); |
347 |
> |
q.put(new Integer(0)); |
348 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
349 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
350 |
> |
}}); |
351 |
|
|
352 |
|
t.start(); |
353 |
|
Thread.sleep(SMALL_DELAY_MS); |
361 |
|
public void testTake() throws InterruptedException { |
362 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
363 |
|
for (int i = 0; i < SIZE; ++i) { |
364 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
364 |
> |
assertEquals(i, q.take()); |
365 |
|
} |
366 |
|
} |
367 |
|
|
385 |
|
* Take removes existing elements until empty, then blocks interruptibly |
386 |
|
*/ |
387 |
|
public void testBlockingTake() throws InterruptedException { |
388 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
388 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
389 |
> |
Thread t = new Thread(new CheckedRunnable() { |
390 |
|
public void realRun() throws InterruptedException { |
379 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
391 |
|
for (int i = 0; i < SIZE; ++i) { |
392 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
392 |
> |
assertEquals(i, q.take()); |
393 |
|
} |
394 |
< |
q.take(); |
394 |
> |
try { |
395 |
> |
q.take(); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (InterruptedException success) {} |
398 |
|
}}); |
399 |
|
|
400 |
|
t.start(); |
410 |
|
public void testPoll() { |
411 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
413 |
> |
assertEquals(i, q.poll()); |
414 |
|
} |
415 |
|
assertNull(q.poll()); |
416 |
|
} |
421 |
|
public void testTimedPoll0() throws InterruptedException { |
422 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
424 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
|
} |
426 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
426 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
432 |
|
public void testTimedPoll() throws InterruptedException { |
433 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
434 |
|
for (int i = 0; i < SIZE; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
435 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
|
} |
437 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
437 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
446 |
|
public void realRun() throws InterruptedException { |
447 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
448 |
|
for (int i = 0; i < SIZE; ++i) { |
449 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
449 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
451 |
|
try { |
452 |
< |
q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
453 |
< |
threadShouldThrow(); |
452 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
453 |
> |
shouldThrow(); |
454 |
|
} catch (InterruptedException success) {} |
455 |
|
}}); |
456 |
|
|
461 |
|
} |
462 |
|
|
463 |
|
/** |
450 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
451 |
– |
* on interruption throws |
452 |
– |
*/ |
453 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
454 |
– |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
455 |
– |
Thread t = new Thread(new CheckedRunnable() { |
456 |
– |
public void realRun() throws InterruptedException { |
457 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
458 |
– |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
459 |
– |
try { |
460 |
– |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
461 |
– |
threadShouldThrow(); |
462 |
– |
} catch (InterruptedException success) {} |
463 |
– |
}}); |
464 |
– |
|
465 |
– |
t.start(); |
466 |
– |
Thread.sleep(SMALL_DELAY_MS); |
467 |
– |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
– |
t.interrupt(); |
469 |
– |
t.join(); |
470 |
– |
} |
471 |
– |
|
472 |
– |
|
473 |
– |
/** |
464 |
|
* peek returns next element, or null if empty |
465 |
|
*/ |
466 |
|
public void testPeek() { |
467 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
470 |
< |
q.poll(); |
469 |
> |
assertEquals(i, q.peek()); |
470 |
> |
assertEquals(i, q.poll()); |
471 |
|
assertTrue(q.peek() == null || |
472 |
< |
i != ((Integer)q.peek()).intValue()); |
472 |
> |
!q.peek().equals(i)); |
473 |
|
} |
474 |
|
assertNull(q.peek()); |
475 |
|
} |
480 |
|
public void testElement() { |
481 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
484 |
< |
q.poll(); |
483 |
> |
assertEquals(i, q.element()); |
484 |
> |
assertEquals(i, q.poll()); |
485 |
|
} |
486 |
|
try { |
487 |
|
q.element(); |
495 |
|
public void testRemove() { |
496 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
497 |
|
for (int i = 0; i < SIZE; ++i) { |
498 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
498 |
> |
assertEquals(i, q.remove()); |
499 |
|
} |
500 |
|
try { |
501 |
|
q.remove(); |
621 |
|
* toArray(null) throws NPE |
622 |
|
*/ |
623 |
|
public void testToArray_BadArg() { |
624 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
625 |
|
try { |
635 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
626 |
|
Object o[] = q.toArray(null); |
627 |
|
shouldThrow(); |
628 |
|
} catch (NullPointerException success) {} |
632 |
|
* toArray with incompatible array type throws CCE |
633 |
|
*/ |
634 |
|
public void testToArray1_BadArg() { |
635 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
636 |
|
try { |
637 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
< |
Object o[] = q.toArray(new String[10] ); |
637 |
> |
Object o[] = q.toArray(new String[10]); |
638 |
|
shouldThrow(); |
639 |
< |
} catch (ArrayStoreException success) {} |
639 |
> |
} catch (ArrayStoreException success) {} |
640 |
|
} |
641 |
|
|
642 |
|
/** |
656 |
|
/** |
657 |
|
* iterator.remove removes current element |
658 |
|
*/ |
659 |
< |
public void testIteratorRemove () { |
659 |
> |
public void testIteratorRemove() { |
660 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
661 |
|
q.add(new Integer(2)); |
662 |
|
q.add(new Integer(1)); |
692 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
693 |
|
executor.execute(new CheckedRunnable() { |
694 |
|
public void realRun() throws InterruptedException { |
695 |
< |
threadAssertNull(q.poll()); |
696 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
697 |
< |
threadAssertTrue(q.isEmpty()); |
695 |
> |
assertNull(q.poll()); |
696 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
> |
assertTrue(q.isEmpty()); |
698 |
|
}}); |
699 |
|
|
700 |
|
executor.execute(new CheckedRunnable() { |
701 |
|
public void realRun() throws InterruptedException { |
702 |
|
Thread.sleep(SMALL_DELAY_MS); |
703 |
< |
q.put(new Integer(1)); |
703 |
> |
q.put(one); |
704 |
|
}}); |
705 |
|
|
706 |
|
joinPool(executor); |