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) { |
167 |
|
* Queue contains all elements of collection used to initialize |
168 |
|
*/ |
169 |
|
public void testConstructor6() { |
170 |
< |
try { |
171 |
< |
PDelay[] ints = new PDelay[SIZE]; |
172 |
< |
for (int i = 0; i < SIZE; ++i) |
173 |
< |
ints[i] = new PDelay(i); |
174 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
175 |
< |
for (int i = 0; i < SIZE; ++i) |
184 |
< |
assertEquals(ints[i], q.poll()); |
185 |
< |
} |
186 |
< |
finally {} |
170 |
> |
PDelay[] ints = new PDelay[SIZE]; |
171 |
> |
for (int i = 0; i < SIZE; ++i) |
172 |
> |
ints[i] = new PDelay(i); |
173 |
> |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
174 |
> |
for (int i = 0; i < SIZE; ++i) |
175 |
> |
assertEquals(ints[i], q.poll()); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
303 |
|
* Queue contains all elements of successful addAll |
304 |
|
*/ |
305 |
|
public void testAddAll5() { |
306 |
< |
try { |
307 |
< |
PDelay[] empty = new PDelay[0]; |
308 |
< |
PDelay[] ints = new PDelay[SIZE]; |
309 |
< |
for (int i = SIZE-1; i >= 0; --i) |
310 |
< |
ints[i] = new PDelay(i); |
311 |
< |
DelayQueue q = new DelayQueue(); |
312 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
313 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
314 |
< |
for (int i = 0; i < SIZE; ++i) |
326 |
< |
assertEquals(ints[i], q.poll()); |
327 |
< |
} |
328 |
< |
finally {} |
306 |
> |
PDelay[] empty = new PDelay[0]; |
307 |
> |
PDelay[] ints = new PDelay[SIZE]; |
308 |
> |
for (int i = SIZE-1; i >= 0; --i) |
309 |
> |
ints[i] = new PDelay(i); |
310 |
> |
DelayQueue q = new DelayQueue(); |
311 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
312 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
313 |
> |
for (int i = 0; i < SIZE; ++i) |
314 |
> |
assertEquals(ints[i], q.poll()); |
315 |
|
} |
316 |
|
|
317 |
|
/** |
367 |
|
public void realRun() throws InterruptedException { |
368 |
|
q.put(new PDelay(0)); |
369 |
|
q.put(new PDelay(0)); |
370 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
371 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
370 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
371 |
> |
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
372 |
|
}}); |
373 |
|
|
374 |
|
t.start(); |
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 { |
426 |
– |
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); |
471 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
472 |
|
} |
473 |
|
try { |
474 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
474 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
475 |
|
shouldThrow(); |
476 |
|
} catch (InterruptedException success) {} |
477 |
|
}}); |
488 |
|
*/ |
489 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
490 |
|
final DelayQueue q = new DelayQueue(); |
491 |
+ |
final PDelay pdelay = new PDelay(0); |
492 |
|
Thread t = new Thread(new CheckedRunnable() { |
493 |
|
public void realRun() throws InterruptedException { |
494 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
495 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
495 |
> |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
496 |
|
try { |
497 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
498 |
|
shouldThrow(); |
501 |
|
|
502 |
|
t.start(); |
503 |
|
Thread.sleep(SMALL_DELAY_MS); |
504 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
504 |
> |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
505 |
|
t.interrupt(); |
506 |
|
t.join(); |
507 |
|
} |
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 { |
687 |
– |
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); |
699 |
< |
Object o[] = q.toArray(new String[10] ); |
689 |
> |
Object o[] = q.toArray(new String[10]); |
690 |
|
shouldThrow(); |
691 |
|
} catch (ArrayStoreException success) {} |
692 |
|
} |
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() { |