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 |
|
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); |
20 |
> |
return new TestSuite(PriorityBlockingQueueTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
40 |
|
private PriorityBlockingQueue populatedQueue(int n) { |
41 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
42 |
|
assertTrue(q.isEmpty()); |
43 |
< |
for (int i = n-1; i >= 0; i-=2) |
44 |
< |
assertTrue(q.offer(new Integer(i))); |
45 |
< |
for (int i = (n & 1); i < n; i+=2) |
46 |
< |
assertTrue(q.offer(new Integer(i))); |
43 |
> |
for (int i = n-1; i >= 0; i-=2) |
44 |
> |
assertTrue(q.offer(new Integer(i))); |
45 |
> |
for (int i = (n & 1); i < n; i+=2) |
46 |
> |
assertTrue(q.offer(new Integer(i))); |
47 |
|
assertFalse(q.isEmpty()); |
48 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
49 |
< |
assertEquals(n, q.size()); |
49 |
> |
assertEquals(n, q.size()); |
50 |
|
return q; |
51 |
|
} |
52 |
|
|
165 |
|
* offer(null) throws NPE |
166 |
|
*/ |
167 |
|
public void testOfferNull() { |
168 |
< |
try { |
168 |
> |
try { |
169 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
170 |
|
q.offer(null); |
171 |
|
shouldThrow(); |
176 |
|
* add(null) throws NPE |
177 |
|
*/ |
178 |
|
public void testAddNull() { |
179 |
< |
try { |
179 |
> |
try { |
180 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
181 |
|
q.add(null); |
182 |
|
shouldThrow(); |
283 |
|
* put(null) throws NPE |
284 |
|
*/ |
285 |
|
public void testPutNull() { |
286 |
< |
try { |
286 |
> |
try { |
287 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
288 |
|
q.put(null); |
289 |
|
shouldThrow(); |
333 |
|
try { |
334 |
|
q.put(new Integer(0)); |
335 |
|
q.put(new Integer(0)); |
336 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
337 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
336 |
> |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
337 |
> |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
338 |
|
} finally { } |
339 |
|
} |
340 |
|
}); |
399 |
|
for (int i = 0; i < SIZE; ++i) { |
400 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
401 |
|
} |
402 |
< |
assertNull(q.poll()); |
402 |
> |
assertNull(q.poll()); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
408 |
|
public void testTimedPoll0() throws InterruptedException { |
409 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
410 |
|
for (int i = 0; i < SIZE; ++i) { |
411 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
411 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
412 |
|
} |
413 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
413 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
419 |
|
public void testTimedPoll() throws InterruptedException { |
420 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
421 |
|
for (int i = 0; i < SIZE; ++i) { |
422 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
422 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
423 |
|
} |
424 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
424 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
425 |
|
} |
426 |
|
|
427 |
|
/** |
433 |
|
public void realRun() throws InterruptedException { |
434 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
435 |
|
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
436 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
437 |
|
} |
438 |
|
try { |
439 |
< |
q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
439 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
440 |
|
threadShouldThrow(); |
441 |
|
} catch (InterruptedException success) {} |
442 |
|
}}); |
455 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
456 |
|
Thread t = new Thread(new CheckedRunnable() { |
457 |
|
public void realRun() throws InterruptedException { |
458 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
459 |
< |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
458 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
459 |
> |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
460 |
|
try { |
461 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
461 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
462 |
|
threadShouldThrow(); |
463 |
|
} catch (InterruptedException success) {} |
464 |
|
}}); |
465 |
|
|
466 |
|
t.start(); |
467 |
|
Thread.sleep(SMALL_DELAY_MS); |
468 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
469 |
|
t.interrupt(); |
470 |
|
t.join(); |
471 |
|
} |
482 |
|
assertTrue(q.peek() == null || |
483 |
|
i != ((Integer)q.peek()).intValue()); |
484 |
|
} |
485 |
< |
assertNull(q.peek()); |
485 |
> |
assertNull(q.peek()); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
610 |
|
*/ |
611 |
|
public void testToArray() throws InterruptedException { |
612 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
613 |
< |
Object[] o = q.toArray(); |
613 |
> |
Object[] o = q.toArray(); |
614 |
|
Arrays.sort(o); |
615 |
< |
for (int i = 0; i < o.length; i++) |
616 |
< |
assertEquals(o[i], q.take()); |
615 |
> |
for (int i = 0; i < o.length; i++) |
616 |
> |
assertEquals(o[i], q.take()); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
621 |
|
*/ |
622 |
|
public void testToArray2() throws InterruptedException { |
623 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
624 |
< |
Integer[] ints = new Integer[SIZE]; |
625 |
< |
ints = (Integer[])q.toArray(ints); |
624 |
> |
Integer[] ints = new Integer[SIZE]; |
625 |
> |
ints = (Integer[])q.toArray(ints); |
626 |
|
Arrays.sort(ints); |
627 |
|
for (int i = 0; i < ints.length; i++) |
628 |
|
assertEquals(ints[i], q.take()); |
632 |
|
* toArray(null) throws NPE |
633 |
|
*/ |
634 |
|
public void testToArray_BadArg() { |
635 |
< |
try { |
635 |
> |
try { |
636 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
637 |
< |
Object o[] = q.toArray(null); |
638 |
< |
shouldThrow(); |
639 |
< |
} catch (NullPointerException success) {} |
637 |
> |
Object o[] = q.toArray(null); |
638 |
> |
shouldThrow(); |
639 |
> |
} catch (NullPointerException success) {} |
640 |
|
} |
641 |
|
|
642 |
|
/** |
643 |
|
* toArray with incompatible array type throws CCE |
644 |
|
*/ |
645 |
|
public void testToArray1_BadArg() { |
646 |
< |
try { |
646 |
> |
try { |
647 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
648 |
< |
Object o[] = q.toArray(new String[10] ); |
649 |
< |
shouldThrow(); |
650 |
< |
} catch (ArrayStoreException success) {} |
648 |
> |
Object o[] = q.toArray(new String[10] ); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (ArrayStoreException success) {} |
651 |
|
} |
652 |
|
|
653 |
|
/** |
656 |
|
public void testIterator() { |
657 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
658 |
|
int i = 0; |
659 |
< |
Iterator it = q.iterator(); |
659 |
> |
Iterator it = q.iterator(); |
660 |
|
while (it.hasNext()) { |
661 |
|
assertTrue(q.contains(it.next())); |
662 |
|
++i; |
704 |
|
executor.execute(new CheckedRunnable() { |
705 |
|
public void realRun() throws InterruptedException { |
706 |
|
threadAssertNull(q.poll()); |
707 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
707 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
708 |
|
threadAssertTrue(q.isEmpty()); |
709 |
|
}}); |
710 |
|
|