330 awaitTermination(t); |
330 awaitTermination(t); |
331 assertEquals(4, q.size()); |
331 assertEquals(4, q.size()); |
332 } |
332 } |
333 |
333 |
334 /** |
334 /** |
335 * timed offer does not time out |
335 * Queue is unbounded, so timed offer never times out |
336 */ |
336 */ |
337 public void testTimedOffer() throws InterruptedException { |
337 public void testTimedOffer() throws InterruptedException { |
338 final DelayQueue q = new DelayQueue(); |
338 final DelayQueue q = new DelayQueue(); |
339 Thread t = newStartedThread(new CheckedRunnable() { |
339 Thread t = newStartedThread(new CheckedRunnable() { |
340 public void realRun() throws InterruptedException { |
340 public void realRun() throws InterruptedException { |
434 public void testInterruptedTimedPoll() throws InterruptedException { |
434 public void testInterruptedTimedPoll() throws InterruptedException { |
435 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
435 final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
436 final DelayQueue q = populatedQueue(SIZE); |
436 final DelayQueue q = populatedQueue(SIZE); |
437 Thread t = newStartedThread(new CheckedRunnable() { |
437 Thread t = newStartedThread(new CheckedRunnable() { |
438 public void realRun() throws InterruptedException { |
438 public void realRun() throws InterruptedException { |
439 long startTime = System.nanoTime(); |
|
440 for (int i = 0; i < SIZE; i++) |
439 for (int i = 0; i < SIZE; i++) |
441 assertEquals(new PDelay(i), |
440 assertEquals(new PDelay(i), |
442 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
441 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
443 |
442 |
444 Thread.currentThread().interrupt(); |
443 Thread.currentThread().interrupt(); |
445 try { |
444 try { |
446 q.poll(LONG_DELAY_MS, MILLISECONDS); |
445 q.poll(randomTimeout(), randomTimeUnit()); |
447 shouldThrow(); |
446 shouldThrow(); |
448 } catch (InterruptedException success) {} |
447 } catch (InterruptedException success) {} |
449 assertFalse(Thread.interrupted()); |
448 assertFalse(Thread.interrupted()); |
450 |
449 |
451 pleaseInterrupt.countDown(); |
450 pleaseInterrupt.countDown(); |
452 try { |
451 try { |
453 q.poll(LONG_DELAY_MS, MILLISECONDS); |
452 q.poll(LONGER_DELAY_MS, MILLISECONDS); |
454 shouldThrow(); |
453 shouldThrow(); |
455 } catch (InterruptedException success) {} |
454 } catch (InterruptedException success) {} |
456 assertFalse(Thread.interrupted()); |
455 assertFalse(Thread.interrupted()); |
457 |
|
458 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
459 }}); |
456 }}); |
460 |
457 |
461 await(pleaseInterrupt); |
458 await(pleaseInterrupt); |
462 assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
459 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
463 t.interrupt(); |
460 t.interrupt(); |
464 awaitTermination(t); |
461 awaitTermination(t); |
465 checkEmpty(q); |
462 checkEmpty(q); |
466 } |
463 } |
467 |
464 |