13 |
|
|
14 |
|
public class DelayQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
|
19 |
|
public static Test suite() { |
24 |
|
|
25 |
|
/** |
26 |
|
* A delayed implementation for testing. |
27 |
< |
* Most tests use Pseudodelays, where delays are all elapsed |
27 |
> |
* Most tests use Pseudodelays, where delays are all elapsed |
28 |
|
* (so, no blocking solely for delays) but are still ordered |
29 |
|
*/ |
30 |
|
static class PDelay implements Delayed { |
32 |
|
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
33 |
|
public int compareTo(PDelay y) { |
34 |
|
int i = pseudodelay; |
35 |
< |
int j = ((PDelay)y).pseudodelay; |
35 |
> |
int j = y.pseudodelay; |
36 |
|
if (i < j) return -1; |
37 |
|
if (i > j) return 1; |
38 |
|
return 0; |
39 |
|
} |
40 |
|
|
41 |
|
public int compareTo(Delayed y) { |
42 |
< |
int i = pseudodelay; |
43 |
< |
int j = ((PDelay)y).pseudodelay; |
44 |
< |
if (i < j) return -1; |
45 |
< |
if (i > j) return 1; |
46 |
< |
return 0; |
42 |
> |
return compareTo((PDelay)y); |
43 |
|
} |
44 |
|
|
45 |
|
public boolean equals(Object other) { |
46 |
< |
return ((PDelay)other).pseudodelay == pseudodelay; |
46 |
> |
return equals((PDelay)other); |
47 |
|
} |
48 |
|
public boolean equals(PDelay other) { |
49 |
< |
return ((PDelay)other).pseudodelay == pseudodelay; |
49 |
> |
return other.pseudodelay == pseudodelay; |
50 |
|
} |
51 |
|
|
52 |
|
|
73 |
|
} |
74 |
|
public int compareTo(NanoDelay y) { |
75 |
|
long i = trigger; |
76 |
< |
long j = ((NanoDelay)y).trigger; |
76 |
> |
long j = y.trigger; |
77 |
|
if (i < j) return -1; |
78 |
|
if (i > j) return 1; |
79 |
|
return 0; |
80 |
|
} |
81 |
|
|
82 |
|
public int compareTo(Delayed y) { |
83 |
< |
long i = trigger; |
88 |
< |
long j = ((NanoDelay)y).trigger; |
89 |
< |
if (i < j) return -1; |
90 |
< |
if (i > j) return 1; |
91 |
< |
return 0; |
83 |
> |
return compareTo((NanoDelay)y); |
84 |
|
} |
85 |
|
|
86 |
|
public boolean equals(Object other) { |
87 |
< |
return ((NanoDelay)other).trigger == trigger; |
87 |
> |
return equals((NanoDelay)other); |
88 |
|
} |
89 |
|
public boolean equals(NanoDelay other) { |
90 |
< |
return ((NanoDelay)other).trigger == trigger; |
90 |
> |
return other.trigger == trigger; |
91 |
|
} |
92 |
|
|
93 |
|
public long getDelay(TimeUnit unit) { |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
< |
* remainingCapacity does not change when elementa added or removed, |
194 |
> |
* remainingCapacity does not change when elements added or removed, |
195 |
|
* but size does |
196 |
|
*/ |
197 |
|
public void testRemainingCapacity() { |
407 |
|
* Take removes existing elements until empty, then blocks interruptibly |
408 |
|
*/ |
409 |
|
public void testBlockingTake() throws InterruptedException { |
410 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
410 |
> |
final DelayQueue q = populatedQueue(SIZE); |
411 |
> |
Thread t = new Thread(new CheckedRunnable() { |
412 |
|
public void realRun() throws InterruptedException { |
420 |
– |
DelayQueue q = populatedQueue(SIZE); |
413 |
|
for (int i = 0; i < SIZE; ++i) { |
414 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
414 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
415 |
|
} |
416 |
< |
q.take(); |
417 |
< |
}}; |
416 |
> |
try { |
417 |
> |
q.take(); |
418 |
> |
shouldThrow(); |
419 |
> |
} catch (InterruptedException success) {} |
420 |
> |
}}); |
421 |
|
|
422 |
|
t.start(); |
423 |
|
Thread.sleep(SHORT_DELAY_MS); |
514 |
|
DelayQueue q = populatedQueue(SIZE); |
515 |
|
for (int i = 0; i < SIZE; ++i) { |
516 |
|
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
517 |
< |
q.poll(); |
517 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
518 |
|
if (q.isEmpty()) |
519 |
|
assertNull(q.peek()); |
520 |
|
else |
521 |
< |
assertTrue(i != ((PDelay)q.peek()).intValue()); |
521 |
> |
assertFalse(new PDelay(i).equals(q.peek())); |
522 |
|
} |
523 |
|
assertNull(q.peek()); |
524 |
|
} |
673 |
|
* toArray(null) throws NPE |
674 |
|
*/ |
675 |
|
public void testToArray_BadArg() { |
676 |
+ |
DelayQueue q = populatedQueue(SIZE); |
677 |
|
try { |
682 |
– |
DelayQueue q = populatedQueue(SIZE); |
678 |
|
Object o[] = q.toArray(null); |
679 |
|
shouldThrow(); |
680 |
|
} catch (NullPointerException success) {} |
684 |
|
* toArray with incompatible array type throws CCE |
685 |
|
*/ |
686 |
|
public void testToArray1_BadArg() { |
687 |
+ |
DelayQueue q = populatedQueue(SIZE); |
688 |
|
try { |
689 |
< |
DelayQueue q = populatedQueue(SIZE); |
694 |
< |
Object o[] = q.toArray(new String[10] ); |
689 |
> |
Object o[] = q.toArray(new String[10]); |
690 |
|
shouldThrow(); |
691 |
|
} catch (ArrayStoreException success) {} |
692 |
|
} |
708 |
|
/** |
709 |
|
* iterator.remove removes current element |
710 |
|
*/ |
711 |
< |
public void testIteratorRemove () { |
711 |
> |
public void testIteratorRemove() { |
712 |
|
final DelayQueue q = new DelayQueue(); |
713 |
|
q.add(new PDelay(2)); |
714 |
|
q.add(new PDelay(1)); |
742 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
743 |
|
executor.execute(new CheckedRunnable() { |
744 |
|
public void realRun() throws InterruptedException { |
745 |
< |
threadAssertNull(q.poll()); |
746 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
747 |
< |
threadAssertTrue(q.isEmpty()); |
745 |
> |
assertNull(q.poll()); |
746 |
> |
assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
747 |
> |
assertTrue(q.isEmpty()); |
748 |
|
}}); |
749 |
|
|
750 |
|
executor.execute(new CheckedRunnable() { |
754 |
|
}}); |
755 |
|
|
756 |
|
joinPool(executor); |
762 |
– |
|
757 |
|
} |
758 |
|
|
759 |
|
|