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 |
|
|
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* Constructor throws IAE if capacity argument nonpositive |
61 |
> |
* Constructor throws IAE if capacity argument nonpositive |
62 |
|
*/ |
63 |
|
public void testConstructor2() { |
64 |
|
try { |
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(); |
328 |
|
*/ |
329 |
|
public void testTimedOffer() throws InterruptedException { |
330 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
331 |
< |
Thread t = new Thread(new Runnable() { |
332 |
< |
public void run() { |
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)); |
337 |
< |
} finally { } |
338 |
< |
} |
339 |
< |
}); |
331 |
> |
Thread t = new Thread(new CheckedRunnable() { |
332 |
> |
public void realRun() { |
333 |
> |
q.put(new Integer(0)); |
334 |
> |
q.put(new Integer(0)); |
335 |
> |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
336 |
> |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
337 |
> |
}}); |
338 |
|
|
339 |
|
t.start(); |
340 |
|
Thread.sleep(SMALL_DELAY_MS); |
396 |
|
for (int i = 0; i < SIZE; ++i) { |
397 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
398 |
|
} |
399 |
< |
assertNull(q.poll()); |
399 |
> |
assertNull(q.poll()); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
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, TimeUnit.MILLISECONDS)).intValue()); |
408 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
409 |
|
} |
410 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
410 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
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, TimeUnit.MILLISECONDS)).intValue()); |
419 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
420 |
|
} |
421 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
421 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
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, TimeUnit.MILLISECONDS)).intValue()); |
433 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
434 |
|
} |
435 |
|
try { |
436 |
< |
q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
437 |
< |
threadShouldThrow(); |
436 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
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 { |
455 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
456 |
< |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
455 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
456 |
> |
assertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
457 |
|
try { |
458 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
458 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
459 |
|
threadShouldThrow(); |
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, TimeUnit.MILLISECONDS)); |
465 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
466 |
|
t.interrupt(); |
467 |
|
t.join(); |
468 |
|
} |
479 |
|
assertTrue(q.peek() == null || |
480 |
|
i != ((Integer)q.peek()).intValue()); |
481 |
|
} |
482 |
< |
assertNull(q.peek()); |
482 |
> |
assertNull(q.peek()); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
607 |
|
*/ |
608 |
|
public void testToArray() throws InterruptedException { |
609 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
610 |
< |
Object[] o = q.toArray(); |
610 |
> |
Object[] o = q.toArray(); |
611 |
|
Arrays.sort(o); |
612 |
< |
for (int i = 0; i < o.length; i++) |
613 |
< |
assertEquals(o[i], q.take()); |
612 |
> |
for (int i = 0; i < o.length; i++) |
613 |
> |
assertEquals(o[i], q.take()); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
618 |
|
*/ |
619 |
|
public void testToArray2() throws InterruptedException { |
620 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
621 |
< |
Integer[] ints = new Integer[SIZE]; |
622 |
< |
ints = (Integer[])q.toArray(ints); |
621 |
> |
Integer[] ints = new Integer[SIZE]; |
622 |
> |
ints = (Integer[])q.toArray(ints); |
623 |
|
Arrays.sort(ints); |
624 |
|
for (int i = 0; i < ints.length; i++) |
625 |
|
assertEquals(ints[i], q.take()); |
629 |
|
* toArray(null) throws NPE |
630 |
|
*/ |
631 |
|
public void testToArray_BadArg() { |
632 |
< |
try { |
632 |
> |
try { |
633 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
634 |
< |
Object o[] = q.toArray(null); |
635 |
< |
shouldThrow(); |
636 |
< |
} catch (NullPointerException success) {} |
634 |
> |
Object o[] = q.toArray(null); |
635 |
> |
shouldThrow(); |
636 |
> |
} catch (NullPointerException success) {} |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
|
* toArray with incompatible array type throws CCE |
641 |
|
*/ |
642 |
|
public void testToArray1_BadArg() { |
643 |
< |
try { |
643 |
> |
try { |
644 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
645 |
< |
Object o[] = q.toArray(new String[10] ); |
646 |
< |
shouldThrow(); |
647 |
< |
} catch (ArrayStoreException success) {} |
645 |
> |
Object o[] = q.toArray(new String[10] ); |
646 |
> |
shouldThrow(); |
647 |
> |
} catch (ArrayStoreException success) {} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
653 |
|
public void testIterator() { |
654 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
655 |
|
int i = 0; |
656 |
< |
Iterator it = q.iterator(); |
656 |
> |
Iterator it = q.iterator(); |
657 |
|
while (it.hasNext()) { |
658 |
|
assertTrue(q.contains(it.next())); |
659 |
|
++i; |
701 |
|
executor.execute(new CheckedRunnable() { |
702 |
|
public void realRun() throws InterruptedException { |
703 |
|
threadAssertNull(q.poll()); |
704 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
704 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
|
threadAssertTrue(q.isEmpty()); |
706 |
|
}}); |
707 |
|
|