8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
import java.util.concurrent.*; |
13 |
|
|
14 |
|
public class DelayQueueTest extends JSR166TestCase { |
144 |
|
try { |
145 |
|
DelayQueue q = new DelayQueue(null); |
146 |
|
shouldThrow(); |
147 |
< |
} |
147 |
< |
catch (NullPointerException success) {} |
147 |
> |
} catch (NullPointerException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
155 |
|
PDelay[] ints = new PDelay[SIZE]; |
156 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
157 |
|
shouldThrow(); |
158 |
< |
} |
159 |
< |
catch (NullPointerException success) {} |
158 |
> |
} catch (NullPointerException success) {} |
159 |
|
} |
160 |
|
|
161 |
|
/** |
168 |
|
ints[i] = new PDelay(i); |
169 |
|
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
|
shouldThrow(); |
171 |
< |
} |
173 |
< |
catch (NullPointerException success) {} |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
227 |
|
DelayQueue q = new DelayQueue(); |
228 |
|
q.offer(null); |
229 |
|
shouldThrow(); |
230 |
< |
} catch (NullPointerException success) { } |
230 |
> |
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
|
|
233 |
|
/** |
238 |
|
DelayQueue q = new DelayQueue(); |
239 |
|
q.add(null); |
240 |
|
shouldThrow(); |
241 |
< |
} catch (NullPointerException success) { } |
241 |
> |
} catch (NullPointerException success) {} |
242 |
|
} |
243 |
|
|
244 |
|
/** |
269 |
|
DelayQueue q = new DelayQueue(); |
270 |
|
q.addAll(null); |
271 |
|
shouldThrow(); |
272 |
< |
} |
275 |
< |
catch (NullPointerException success) {} |
272 |
> |
} catch (NullPointerException success) {} |
273 |
|
} |
274 |
|
|
275 |
|
|
281 |
|
DelayQueue q = populatedQueue(SIZE); |
282 |
|
q.addAll(q); |
283 |
|
shouldThrow(); |
284 |
< |
} |
288 |
< |
catch (IllegalArgumentException success) {} |
284 |
> |
} catch (IllegalArgumentException success) {} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
293 |
|
PDelay[] ints = new PDelay[SIZE]; |
294 |
|
q.addAll(Arrays.asList(ints)); |
295 |
|
shouldThrow(); |
296 |
< |
} |
301 |
< |
catch (NullPointerException success) {} |
296 |
> |
} catch (NullPointerException success) {} |
297 |
|
} |
298 |
|
/** |
299 |
|
* addAll of a collection with any null elements throws NPE after |
307 |
|
ints[i] = new PDelay(i); |
308 |
|
q.addAll(Arrays.asList(ints)); |
309 |
|
shouldThrow(); |
310 |
< |
} |
316 |
< |
catch (NullPointerException success) {} |
310 |
> |
} catch (NullPointerException success) {} |
311 |
|
} |
312 |
|
|
313 |
|
/** |
336 |
|
DelayQueue q = new DelayQueue(); |
337 |
|
q.put(null); |
338 |
|
shouldThrow(); |
339 |
< |
} |
346 |
< |
catch (NullPointerException success) { |
347 |
< |
} |
339 |
> |
} catch (NullPointerException success) {} |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
|
* all elements successfully put are contained |
344 |
|
*/ |
345 |
|
public void testPut() { |
346 |
< |
try { |
347 |
< |
DelayQueue q = new DelayQueue(); |
348 |
< |
for (int i = 0; i < SIZE; ++i) { |
349 |
< |
PDelay I = new PDelay(i); |
350 |
< |
q.put(I); |
359 |
< |
assertTrue(q.contains(I)); |
360 |
< |
} |
361 |
< |
assertEquals(SIZE, q.size()); |
346 |
> |
DelayQueue q = new DelayQueue(); |
347 |
> |
for (int i = 0; i < SIZE; ++i) { |
348 |
> |
PDelay I = new PDelay(i); |
349 |
> |
q.put(I); |
350 |
> |
assertTrue(q.contains(I)); |
351 |
|
} |
352 |
< |
finally { |
364 |
< |
} |
352 |
> |
assertEquals(SIZE, q.size()); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* put doesn't block waiting for take |
357 |
|
*/ |
358 |
< |
public void testPutWithTake() { |
358 |
> |
public void testPutWithTake() throws InterruptedException { |
359 |
|
final DelayQueue q = new DelayQueue(); |
360 |
< |
Thread t = new Thread(new Runnable() { |
361 |
< |
public void run() { |
362 |
< |
int added = 0; |
363 |
< |
try { |
364 |
< |
q.put(new PDelay(0)); |
365 |
< |
++added; |
366 |
< |
q.put(new PDelay(0)); |
367 |
< |
++added; |
368 |
< |
q.put(new PDelay(0)); |
369 |
< |
++added; |
370 |
< |
q.put(new PDelay(0)); |
371 |
< |
++added; |
372 |
< |
threadAssertTrue(added == 4); |
385 |
< |
} finally { |
386 |
< |
} |
387 |
< |
} |
388 |
< |
}); |
389 |
< |
try { |
390 |
< |
t.start(); |
391 |
< |
Thread.sleep(SHORT_DELAY_MS); |
392 |
< |
q.take(); |
393 |
< |
t.interrupt(); |
394 |
< |
t.join(); |
395 |
< |
} catch (Exception e) { |
396 |
< |
unexpectedException(); |
397 |
< |
} |
360 |
> |
Thread t = new Thread(new CheckedRunnable() { |
361 |
> |
public void realRun() { |
362 |
> |
q.put(new PDelay(0)); |
363 |
> |
q.put(new PDelay(0)); |
364 |
> |
q.put(new PDelay(0)); |
365 |
> |
q.put(new PDelay(0)); |
366 |
> |
}}); |
367 |
> |
|
368 |
> |
t.start(); |
369 |
> |
Thread.sleep(SHORT_DELAY_MS); |
370 |
> |
q.take(); |
371 |
> |
t.interrupt(); |
372 |
> |
t.join(); |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
|
* timed offer does not time out |
377 |
|
*/ |
378 |
< |
public void testTimedOffer() { |
378 |
> |
public void testTimedOffer() throws InterruptedException { |
379 |
|
final DelayQueue q = new DelayQueue(); |
380 |
< |
Thread t = new Thread(new Runnable() { |
381 |
< |
public void run() { |
382 |
< |
try { |
383 |
< |
q.put(new PDelay(0)); |
384 |
< |
q.put(new PDelay(0)); |
385 |
< |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
386 |
< |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
412 |
< |
} finally { } |
413 |
< |
} |
414 |
< |
}); |
380 |
> |
Thread t = new Thread(new CheckedRunnable() { |
381 |
> |
public void realRun() throws InterruptedException { |
382 |
> |
q.put(new PDelay(0)); |
383 |
> |
q.put(new PDelay(0)); |
384 |
> |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
385 |
> |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
386 |
> |
}}); |
387 |
|
|
388 |
< |
try { |
389 |
< |
t.start(); |
390 |
< |
Thread.sleep(SMALL_DELAY_MS); |
391 |
< |
t.interrupt(); |
420 |
< |
t.join(); |
421 |
< |
} catch (Exception e) { |
422 |
< |
unexpectedException(); |
423 |
< |
} |
388 |
> |
t.start(); |
389 |
> |
Thread.sleep(SMALL_DELAY_MS); |
390 |
> |
t.interrupt(); |
391 |
> |
t.join(); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* take retrieves elements in priority order |
396 |
|
*/ |
397 |
< |
public void testTake() { |
398 |
< |
try { |
399 |
< |
DelayQueue q = populatedQueue(SIZE); |
400 |
< |
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
434 |
< |
} |
435 |
< |
} catch (InterruptedException e) { |
436 |
< |
unexpectedException(); |
397 |
> |
public void testTake() throws InterruptedException { |
398 |
> |
DelayQueue q = populatedQueue(SIZE); |
399 |
> |
for (int i = 0; i < SIZE; ++i) { |
400 |
> |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* take blocks interruptibly when empty |
406 |
|
*/ |
407 |
< |
public void testTakeFromEmpty() { |
407 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
408 |
|
final DelayQueue q = new DelayQueue(); |
409 |
< |
Thread t = new Thread(new Runnable() { |
410 |
< |
public void run() { |
411 |
< |
try { |
412 |
< |
q.take(); |
413 |
< |
threadShouldThrow(); |
414 |
< |
} catch (InterruptedException success) { } |
415 |
< |
} |
416 |
< |
}); |
417 |
< |
try { |
454 |
< |
t.start(); |
455 |
< |
Thread.sleep(SHORT_DELAY_MS); |
456 |
< |
t.interrupt(); |
457 |
< |
t.join(); |
458 |
< |
} catch (Exception e) { |
459 |
< |
unexpectedException(); |
460 |
< |
} |
409 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
410 |
> |
public void realRun() throws InterruptedException { |
411 |
> |
q.take(); |
412 |
> |
}}; |
413 |
> |
|
414 |
> |
t.start(); |
415 |
> |
Thread.sleep(SHORT_DELAY_MS); |
416 |
> |
t.interrupt(); |
417 |
> |
t.join(); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
|
* Take removes existing elements until empty, then blocks interruptibly |
422 |
|
*/ |
423 |
< |
public void testBlockingTake() { |
424 |
< |
Thread t = new Thread(new Runnable() { |
425 |
< |
public void run() { |
426 |
< |
try { |
427 |
< |
DelayQueue q = populatedQueue(SIZE); |
428 |
< |
for (int i = 0; i < SIZE; ++i) { |
429 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
430 |
< |
} |
431 |
< |
q.take(); |
432 |
< |
threadShouldThrow(); |
476 |
< |
} catch (InterruptedException success) { |
477 |
< |
} |
478 |
< |
}}); |
423 |
> |
public void testBlockingTake() throws InterruptedException { |
424 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
425 |
> |
public void realRun() throws InterruptedException { |
426 |
> |
DelayQueue q = populatedQueue(SIZE); |
427 |
> |
for (int i = 0; i < SIZE; ++i) { |
428 |
> |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
429 |
> |
} |
430 |
> |
q.take(); |
431 |
> |
}}; |
432 |
> |
|
433 |
|
t.start(); |
434 |
< |
try { |
435 |
< |
Thread.sleep(SHORT_DELAY_MS); |
436 |
< |
t.interrupt(); |
483 |
< |
t.join(); |
484 |
< |
} |
485 |
< |
catch (InterruptedException ie) { |
486 |
< |
unexpectedException(); |
487 |
< |
} |
434 |
> |
Thread.sleep(SHORT_DELAY_MS); |
435 |
> |
t.interrupt(); |
436 |
> |
t.join(); |
437 |
|
} |
438 |
|
|
439 |
|
|
451 |
|
/** |
452 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
453 |
|
*/ |
454 |
< |
public void testTimedPoll0() { |
455 |
< |
try { |
456 |
< |
DelayQueue q = populatedQueue(SIZE); |
457 |
< |
for (int i = 0; i < SIZE; ++i) { |
509 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
510 |
< |
} |
511 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
512 |
< |
} catch (InterruptedException e) { |
513 |
< |
unexpectedException(); |
454 |
> |
public void testTimedPoll0() throws InterruptedException { |
455 |
> |
DelayQueue q = populatedQueue(SIZE); |
456 |
> |
for (int i = 0; i < SIZE; ++i) { |
457 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
458 |
|
} |
459 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
464 |
|
*/ |
465 |
< |
public void testTimedPoll() { |
466 |
< |
try { |
467 |
< |
DelayQueue q = populatedQueue(SIZE); |
468 |
< |
for (int i = 0; i < SIZE; ++i) { |
524 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
525 |
< |
} |
526 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
527 |
< |
} catch (InterruptedException e) { |
528 |
< |
unexpectedException(); |
465 |
> |
public void testTimedPoll() throws InterruptedException { |
466 |
> |
DelayQueue q = populatedQueue(SIZE); |
467 |
> |
for (int i = 0; i < SIZE; ++i) { |
468 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
469 |
|
} |
470 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
|
* Interrupted timed poll throws InterruptedException instead of |
475 |
|
* returning timeout status |
476 |
|
*/ |
477 |
< |
public void testInterruptedTimedPoll() { |
478 |
< |
Thread t = new Thread(new Runnable() { |
479 |
< |
public void run() { |
480 |
< |
try { |
481 |
< |
DelayQueue q = populatedQueue(SIZE); |
482 |
< |
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
484 |
< |
} |
485 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
486 |
< |
} catch (InterruptedException success) { |
487 |
< |
} |
488 |
< |
}}); |
477 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
478 |
> |
Thread t = new Thread(new CheckedRunnable() { |
479 |
> |
public void realRun() throws InterruptedException { |
480 |
> |
DelayQueue q = populatedQueue(SIZE); |
481 |
> |
for (int i = 0; i < SIZE; ++i) { |
482 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
483 |
> |
} |
484 |
> |
try { |
485 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
> |
shouldThrow(); |
487 |
> |
} catch (InterruptedException success) {} |
488 |
> |
}}); |
489 |
> |
|
490 |
|
t.start(); |
491 |
< |
try { |
492 |
< |
Thread.sleep(SHORT_DELAY_MS); |
493 |
< |
t.interrupt(); |
552 |
< |
t.join(); |
553 |
< |
} |
554 |
< |
catch (InterruptedException ie) { |
555 |
< |
unexpectedException(); |
556 |
< |
} |
491 |
> |
Thread.sleep(SHORT_DELAY_MS); |
492 |
> |
t.interrupt(); |
493 |
> |
t.join(); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
498 |
|
* on interruption throws |
499 |
|
*/ |
500 |
< |
public void testTimedPollWithOffer() { |
500 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
501 |
|
final DelayQueue q = new DelayQueue(); |
502 |
< |
Thread t = new Thread(new Runnable() { |
503 |
< |
public void run() { |
504 |
< |
try { |
505 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
506 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
507 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
508 |
< |
threadFail("Should block"); |
509 |
< |
} catch (InterruptedException success) { } |
510 |
< |
} |
511 |
< |
}); |
512 |
< |
try { |
513 |
< |
t.start(); |
514 |
< |
Thread.sleep(SMALL_DELAY_MS); |
515 |
< |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
516 |
< |
t.interrupt(); |
580 |
< |
t.join(); |
581 |
< |
} catch (Exception e) { |
582 |
< |
unexpectedException(); |
583 |
< |
} |
502 |
> |
Thread t = new Thread(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
505 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
506 |
> |
try { |
507 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
508 |
> |
shouldThrow(); |
509 |
> |
} catch (InterruptedException success) {} |
510 |
> |
}}); |
511 |
> |
|
512 |
> |
t.start(); |
513 |
> |
Thread.sleep(SMALL_DELAY_MS); |
514 |
> |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
515 |
> |
t.interrupt(); |
516 |
> |
t.join(); |
517 |
|
} |
518 |
|
|
519 |
|
|
545 |
|
try { |
546 |
|
q.element(); |
547 |
|
shouldThrow(); |
548 |
< |
} |
616 |
< |
catch (NoSuchElementException success) {} |
548 |
> |
} catch (NoSuchElementException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
559 |
|
try { |
560 |
|
q.remove(); |
561 |
|
shouldThrow(); |
562 |
< |
} catch (NoSuchElementException success) { |
631 |
< |
} |
562 |
> |
} catch (NoSuchElementException success) {} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
658 |
|
/** |
659 |
|
* toArray contains all elements |
660 |
|
*/ |
661 |
< |
public void testToArray() { |
661 |
> |
public void testToArray() throws InterruptedException { |
662 |
|
DelayQueue q = populatedQueue(SIZE); |
663 |
|
Object[] o = q.toArray(); |
664 |
|
Arrays.sort(o); |
734 |
– |
try { |
665 |
|
for (int i = 0; i < o.length; i++) |
666 |
|
assertEquals(o[i], q.take()); |
737 |
– |
} catch (InterruptedException e) { |
738 |
– |
unexpectedException(); |
739 |
– |
} |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
|
* toArray(a) contains all elements |
671 |
|
*/ |
672 |
< |
public void testToArray2() { |
672 |
> |
public void testToArray2() throws InterruptedException { |
673 |
|
DelayQueue q = populatedQueue(SIZE); |
674 |
|
PDelay[] ints = new PDelay[SIZE]; |
675 |
|
ints = (PDelay[])q.toArray(ints); |
676 |
|
Arrays.sort(ints); |
677 |
< |
try { |
678 |
< |
for (int i = 0; i < ints.length; i++) |
752 |
< |
assertEquals(ints[i], q.take()); |
753 |
< |
} catch (InterruptedException e) { |
754 |
< |
unexpectedException(); |
755 |
< |
} |
677 |
> |
for (int i = 0; i < ints.length; i++) |
678 |
> |
assertEquals(ints[i], q.take()); |
679 |
|
} |
680 |
|
|
681 |
|
|
698 |
|
DelayQueue q = populatedQueue(SIZE); |
699 |
|
Object o[] = q.toArray(new String[10] ); |
700 |
|
shouldThrow(); |
701 |
< |
} catch (ArrayStoreException success) {} |
701 |
> |
} catch (ArrayStoreException success) {} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
750 |
|
public void testPollInExecutor() { |
751 |
|
final DelayQueue q = new DelayQueue(); |
752 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
753 |
< |
executor.execute(new Runnable() { |
754 |
< |
public void run() { |
753 |
> |
executor.execute(new CheckedRunnable() { |
754 |
> |
public void realRun() throws InterruptedException { |
755 |
|
threadAssertNull(q.poll()); |
756 |
< |
try { |
757 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
758 |
< |
threadAssertTrue(q.isEmpty()); |
759 |
< |
} |
760 |
< |
catch (InterruptedException e) { |
761 |
< |
threadUnexpectedException(); |
762 |
< |
} |
763 |
< |
} |
764 |
< |
}); |
756 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
757 |
> |
threadAssertTrue(q.isEmpty()); |
758 |
> |
}}); |
759 |
> |
|
760 |
> |
executor.execute(new CheckedRunnable() { |
761 |
> |
public void realRun() throws InterruptedException { |
762 |
> |
Thread.sleep(SHORT_DELAY_MS); |
763 |
> |
q.put(new PDelay(1)); |
764 |
> |
}}); |
765 |
|
|
843 |
– |
executor.execute(new Runnable() { |
844 |
– |
public void run() { |
845 |
– |
try { |
846 |
– |
Thread.sleep(SHORT_DELAY_MS); |
847 |
– |
q.put(new PDelay(1)); |
848 |
– |
} |
849 |
– |
catch (InterruptedException e) { |
850 |
– |
threadUnexpectedException(); |
851 |
– |
} |
852 |
– |
} |
853 |
– |
}); |
766 |
|
joinPool(executor); |
767 |
|
|
768 |
|
} |
771 |
|
/** |
772 |
|
* Delayed actions do not occur until their delay elapses |
773 |
|
*/ |
774 |
< |
public void testDelay() { |
774 |
> |
public void testDelay() throws InterruptedException { |
775 |
|
DelayQueue q = new DelayQueue(); |
776 |
|
NanoDelay[] elements = new NanoDelay[SIZE]; |
777 |
|
for (int i = 0; i < SIZE; ++i) { |
781 |
|
q.add(elements[i]); |
782 |
|
} |
783 |
|
|
784 |
< |
try { |
785 |
< |
long last = 0; |
786 |
< |
for (int i = 0; i < SIZE; ++i) { |
787 |
< |
NanoDelay e = (NanoDelay)(q.take()); |
788 |
< |
long tt = e.getTriggerTime(); |
789 |
< |
assertTrue(tt <= System.nanoTime()); |
790 |
< |
if (i != 0) |
791 |
< |
assertTrue(tt >= last); |
880 |
< |
last = tt; |
881 |
< |
} |
882 |
< |
} |
883 |
< |
catch (InterruptedException ie) { |
884 |
< |
unexpectedException(); |
784 |
> |
long last = 0; |
785 |
> |
for (int i = 0; i < SIZE; ++i) { |
786 |
> |
NanoDelay e = (NanoDelay)(q.take()); |
787 |
> |
long tt = e.getTriggerTime(); |
788 |
> |
assertTrue(tt <= System.nanoTime()); |
789 |
> |
if (i != 0) |
790 |
> |
assertTrue(tt >= last); |
791 |
> |
last = tt; |
792 |
|
} |
793 |
|
} |
794 |
|
|
814 |
|
/** |
815 |
|
* timed poll of a non-empty queue returns null if no expired elements. |
816 |
|
*/ |
817 |
< |
public void testTimedPollDelayed() { |
817 |
> |
public void testTimedPollDelayed() throws InterruptedException { |
818 |
|
DelayQueue q = new DelayQueue(); |
819 |
|
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
820 |
< |
try { |
914 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
915 |
< |
} catch (Exception ex) { |
916 |
< |
unexpectedException(); |
917 |
< |
} |
820 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
821 |
|
} |
822 |
|
|
823 |
|
/** |
828 |
|
try { |
829 |
|
q.drainTo(null); |
830 |
|
shouldThrow(); |
831 |
< |
} catch (NullPointerException success) { |
929 |
< |
} |
831 |
> |
} catch (NullPointerException success) {} |
832 |
|
} |
833 |
|
|
834 |
|
/** |
839 |
|
try { |
840 |
|
q.drainTo(q); |
841 |
|
shouldThrow(); |
842 |
< |
} catch (IllegalArgumentException success) { |
941 |
< |
} |
842 |
> |
} catch (IllegalArgumentException success) {} |
843 |
|
} |
844 |
|
|
845 |
|
/** |
873 |
|
/** |
874 |
|
* drainTo empties queue |
875 |
|
*/ |
876 |
< |
public void testDrainToWithActivePut() { |
876 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
877 |
|
final DelayQueue q = populatedQueue(SIZE); |
878 |
< |
Thread t = new Thread(new Runnable() { |
879 |
< |
public void run() { |
880 |
< |
q.put(new PDelay(SIZE+1)); |
881 |
< |
} |
882 |
< |
}); |
883 |
< |
try { |
884 |
< |
t.start(); |
885 |
< |
ArrayList l = new ArrayList(); |
886 |
< |
q.drainTo(l); |
887 |
< |
assertTrue(l.size() >= SIZE); |
888 |
< |
t.join(); |
988 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
989 |
< |
} catch (Exception e) { |
990 |
< |
unexpectedException(); |
991 |
< |
} |
878 |
> |
Thread t = new Thread(new CheckedRunnable() { |
879 |
> |
public void realRun() { |
880 |
> |
q.put(new PDelay(SIZE+1)); |
881 |
> |
}}); |
882 |
> |
|
883 |
> |
t.start(); |
884 |
> |
ArrayList l = new ArrayList(); |
885 |
> |
q.drainTo(l); |
886 |
> |
assertTrue(l.size() >= SIZE); |
887 |
> |
t.join(); |
888 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
889 |
|
} |
890 |
|
|
891 |
|
/** |
896 |
|
try { |
897 |
|
q.drainTo(null, 0); |
898 |
|
shouldThrow(); |
899 |
< |
} catch (NullPointerException success) { |
1003 |
< |
} |
899 |
> |
} catch (NullPointerException success) {} |
900 |
|
} |
901 |
|
|
902 |
|
/** |
907 |
|
try { |
908 |
|
q.drainTo(q, 0); |
909 |
|
shouldThrow(); |
910 |
< |
} catch (IllegalArgumentException success) { |
1015 |
< |
} |
910 |
> |
} catch (IllegalArgumentException success) {} |
911 |
|
} |
912 |
|
|
913 |
|
/** |