14 |
|
|
15 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(PriorityBlockingQueueTest.class); |
25 |
|
/** Sample Comparator */ |
26 |
|
static class MyReverseComparator implements Comparator { |
27 |
|
public int compare(Object x, Object y) { |
28 |
< |
int i = ((Integer)x).intValue(); |
29 |
< |
int j = ((Integer)y).intValue(); |
30 |
< |
if (i < j) return 1; |
31 |
< |
if (i > j) return -1; |
32 |
< |
return 0; |
28 |
> |
return ((Comparable)y).compareTo(x); |
29 |
|
} |
30 |
|
} |
31 |
|
|
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* Constructor throws IAE if capacity argument nonpositive |
57 |
> |
* Constructor throws IAE if capacity argument nonpositive |
58 |
|
*/ |
59 |
|
public void testConstructor2() { |
60 |
|
try { |
245 |
|
shouldThrow(); |
246 |
|
} catch (NullPointerException success) {} |
247 |
|
} |
248 |
+ |
|
249 |
|
/** |
250 |
|
* addAll of a collection with any null elements throws NPE after |
251 |
|
* possibly adding some elements |
325 |
|
*/ |
326 |
|
public void testTimedOffer() throws InterruptedException { |
327 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
328 |
< |
Thread t = new Thread(new Runnable() { |
329 |
< |
public void run() { |
330 |
< |
try { |
331 |
< |
q.put(new Integer(0)); |
332 |
< |
q.put(new Integer(0)); |
333 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
334 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
338 |
< |
} finally { } |
339 |
< |
} |
340 |
< |
}); |
328 |
> |
Thread t = new Thread(new CheckedRunnable() { |
329 |
> |
public void realRun() { |
330 |
> |
q.put(new Integer(0)); |
331 |
> |
q.put(new Integer(0)); |
332 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
333 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
334 |
> |
}}); |
335 |
|
|
336 |
|
t.start(); |
337 |
|
Thread.sleep(SMALL_DELAY_MS); |
345 |
|
public void testTake() throws InterruptedException { |
346 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
347 |
|
for (int i = 0; i < SIZE; ++i) { |
348 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
348 |
> |
assertEquals(i, q.take()); |
349 |
|
} |
350 |
|
} |
351 |
|
|
369 |
|
* Take removes existing elements until empty, then blocks interruptibly |
370 |
|
*/ |
371 |
|
public void testBlockingTake() throws InterruptedException { |
372 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
372 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
373 |
> |
Thread t = new Thread(new CheckedRunnable() { |
374 |
|
public void realRun() throws InterruptedException { |
380 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
375 |
|
for (int i = 0; i < SIZE; ++i) { |
376 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
376 |
> |
assertEquals(i, q.take()); |
377 |
|
} |
378 |
< |
q.take(); |
378 |
> |
try { |
379 |
> |
q.take(); |
380 |
> |
shouldThrow(); |
381 |
> |
} catch (InterruptedException success) {} |
382 |
|
}}); |
383 |
|
|
384 |
|
t.start(); |
394 |
|
public void testPoll() { |
395 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
396 |
|
for (int i = 0; i < SIZE; ++i) { |
397 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
397 |
> |
assertEquals(i, q.poll()); |
398 |
|
} |
399 |
|
assertNull(q.poll()); |
400 |
|
} |
405 |
|
public void testTimedPoll0() throws InterruptedException { |
406 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
407 |
|
for (int i = 0; i < SIZE; ++i) { |
408 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
408 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
409 |
|
} |
410 |
|
assertNull(q.poll(0, MILLISECONDS)); |
411 |
|
} |
416 |
|
public void testTimedPoll() throws InterruptedException { |
417 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
418 |
|
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
419 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
420 |
|
} |
421 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
422 |
|
} |
430 |
|
public void realRun() throws InterruptedException { |
431 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
432 |
|
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
433 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
434 |
|
} |
435 |
|
try { |
436 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
437 |
< |
threadShouldThrow(); |
437 |
> |
shouldThrow(); |
438 |
|
} catch (InterruptedException success) {} |
439 |
|
}}); |
440 |
|
|
452 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
453 |
|
Thread t = new Thread(new CheckedRunnable() { |
454 |
|
public void realRun() throws InterruptedException { |
458 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
459 |
– |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
455 |
|
try { |
456 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
457 |
+ |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
458 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
459 |
< |
threadShouldThrow(); |
459 |
> |
shouldThrow(); |
460 |
|
} catch (InterruptedException success) {} |
461 |
|
}}); |
462 |
|
|
463 |
|
t.start(); |
464 |
|
Thread.sleep(SMALL_DELAY_MS); |
465 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
465 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
466 |
|
t.interrupt(); |
467 |
|
t.join(); |
468 |
|
} |
474 |
|
public void testPeek() { |
475 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
476 |
|
for (int i = 0; i < SIZE; ++i) { |
477 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
478 |
< |
q.poll(); |
477 |
> |
assertEquals(i, q.peek()); |
478 |
> |
assertEquals(i, q.poll()); |
479 |
|
assertTrue(q.peek() == null || |
480 |
< |
i != ((Integer)q.peek()).intValue()); |
480 |
> |
!q.peek().equals(i)); |
481 |
|
} |
482 |
|
assertNull(q.peek()); |
483 |
|
} |
488 |
|
public void testElement() { |
489 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
490 |
|
for (int i = 0; i < SIZE; ++i) { |
491 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
492 |
< |
q.poll(); |
491 |
> |
assertEquals(i, q.element()); |
492 |
> |
assertEquals(i, q.poll()); |
493 |
|
} |
494 |
|
try { |
495 |
|
q.element(); |
503 |
|
public void testRemove() { |
504 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
505 |
|
for (int i = 0; i < SIZE; ++i) { |
506 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
506 |
> |
assertEquals(i, q.remove()); |
507 |
|
} |
508 |
|
try { |
509 |
|
q.remove(); |
629 |
|
* toArray(null) throws NPE |
630 |
|
*/ |
631 |
|
public void testToArray_BadArg() { |
632 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
633 |
|
try { |
636 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
634 |
|
Object o[] = q.toArray(null); |
635 |
|
shouldThrow(); |
636 |
|
} catch (NullPointerException success) {} |
640 |
|
* toArray with incompatible array type throws CCE |
641 |
|
*/ |
642 |
|
public void testToArray1_BadArg() { |
643 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
644 |
|
try { |
645 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
648 |
< |
Object o[] = q.toArray(new String[10] ); |
645 |
> |
Object o[] = q.toArray(new String[10]); |
646 |
|
shouldThrow(); |
647 |
< |
} catch (ArrayStoreException success) {} |
647 |
> |
} catch (ArrayStoreException success) {} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
664 |
|
/** |
665 |
|
* iterator.remove removes current element |
666 |
|
*/ |
667 |
< |
public void testIteratorRemove () { |
667 |
> |
public void testIteratorRemove() { |
668 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
669 |
|
q.add(new Integer(2)); |
670 |
|
q.add(new Integer(1)); |
700 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
701 |
|
executor.execute(new CheckedRunnable() { |
702 |
|
public void realRun() throws InterruptedException { |
703 |
< |
threadAssertNull(q.poll()); |
704 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
< |
threadAssertTrue(q.isEmpty()); |
703 |
> |
assertNull(q.poll()); |
704 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
> |
assertTrue(q.isEmpty()); |
706 |
|
}}); |
707 |
|
|
708 |
|
executor.execute(new CheckedRunnable() { |
709 |
|
public void realRun() throws InterruptedException { |
710 |
|
Thread.sleep(SMALL_DELAY_MS); |
711 |
< |
q.put(new Integer(1)); |
711 |
> |
q.put(one); |
712 |
|
}}); |
713 |
|
|
714 |
|
joinPool(executor); |