ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.49 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.51 by jsr166, Sat May 28 12:45:38 2011 UTC

# Line 49 | Line 49 | public class DelayQueueTest extends JSR1
49              return other.pseudodelay == pseudodelay;
50          }
51  
52
52          public long getDelay(TimeUnit ignore) {
53              return pseudodelay;
54          }
# Line 62 | Line 61 | public class DelayQueueTest extends JSR1
61          }
62      }
63  
65
64      /**
65       * Delayed implementation that actually delays
66       */
# Line 104 | Line 102 | public class DelayQueueTest extends JSR1
102          }
103      }
104  
107
105      /**
106       * Create a queue of given size containing consecutive
107       * PDelays 0 ... n.
# Line 261 | Line 258 | public class DelayQueueTest extends JSR1
258          } catch (NullPointerException success) {}
259      }
260  
264
261      /**
262       * addAll(this) throws IAE
263       */
# Line 344 | Line 340 | public class DelayQueueTest extends JSR1
340       */
341      public void testPutWithTake() throws InterruptedException {
342          final DelayQueue q = new DelayQueue();
343 <        Thread t = new Thread(new CheckedRunnable() {
343 >        Thread t = newStartedThread(new CheckedRunnable() {
344              public void realRun() {
345                  q.put(new PDelay(0));
346                  q.put(new PDelay(0));
# Line 352 | Line 348 | public class DelayQueueTest extends JSR1
348                  q.put(new PDelay(0));
349              }});
350  
351 <        t.start();
352 <        delay(SHORT_DELAY_MS);
357 <        q.take();
358 <        t.interrupt();
359 <        t.join();
351 >        awaitTermination(t);
352 >        assertEquals(4, q.size());
353      }
354  
355      /**
# Line 364 | Line 357 | public class DelayQueueTest extends JSR1
357       */
358      public void testTimedOffer() throws InterruptedException {
359          final DelayQueue q = new DelayQueue();
360 <        Thread t = new Thread(new CheckedRunnable() {
360 >        Thread t = newStartedThread(new CheckedRunnable() {
361              public void realRun() throws InterruptedException {
362                  q.put(new PDelay(0));
363                  q.put(new PDelay(0));
# Line 372 | Line 365 | public class DelayQueueTest extends JSR1
365                  assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
366              }});
367  
368 <        t.start();
376 <        delay(SMALL_DELAY_MS);
377 <        t.interrupt();
378 <        t.join();
368 >        awaitTermination(t);
369      }
370  
371      /**
# Line 397 | Line 387 | public class DelayQueueTest extends JSR1
387          final CountDownLatch threadStarted = new CountDownLatch(1);
388          Thread t = newStartedThread(new CheckedRunnable() {
389              public void realRun() {
400                long t0 = System.nanoTime();
390                  threadStarted.countDown();
391                  try {
392                      q.take();
393                      shouldThrow();
394                  } catch (InterruptedException success) {}
395 <                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
395 >                assertFalse(Thread.interrupted());
396              }});
397 <        threadStarted.await();
398 <        delay(SHORT_DELAY_MS);
399 <        assertTrue(t.isAlive());
397 >
398 >        await(threadStarted);
399 >        assertThreadStaysAlive(t);
400          t.interrupt();
401 <        awaitTermination(t, MEDIUM_DELAY_MS);
413 <        assertFalse(t.isAlive());
401 >        awaitTermination(t);
402      }
403  
404      /**
# Line 418 | Line 406 | public class DelayQueueTest extends JSR1
406       */
407      public void testBlockingTake() throws InterruptedException {
408          final DelayQueue q = populatedQueue(SIZE);
409 <        Thread t = new Thread(new CheckedRunnable() {
409 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
410 >        Thread t = newStartedThread(new CheckedRunnable() {
411              public void realRun() throws InterruptedException {
412                  for (int i = 0; i < SIZE; ++i) {
413                      assertEquals(new PDelay(i), ((PDelay)q.take()));
414                  }
415 +
416 +                Thread.currentThread().interrupt();
417                  try {
418                      q.take();
419                      shouldThrow();
420                  } catch (InterruptedException success) {}
421 +                assertFalse(Thread.interrupted());
422 +
423 +                pleaseInterrupt.countDown();
424 +                try {
425 +                    q.take();
426 +                    shouldThrow();
427 +                } catch (InterruptedException success) {}
428 +                assertFalse(Thread.interrupted());
429              }});
430  
431 <        t.start();
432 <        delay(SHORT_DELAY_MS);
431 >        await(pleaseInterrupt);
432 >        assertThreadStaysAlive(t);
433          t.interrupt();
434 <        t.join();
434 >        awaitTermination(t);
435      }
436  
438
437      /**
438       * poll succeeds unless empty
439       */
# Line 464 | Line 462 | public class DelayQueueTest extends JSR1
462      public void testTimedPoll() throws InterruptedException {
463          DelayQueue q = populatedQueue(SIZE);
464          for (int i = 0; i < SIZE; ++i) {
465 <            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
466 <        }
467 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
465 >            long startTime = System.nanoTime();
466 >            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
467 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
468 >        }
469 >        long startTime = System.nanoTime();
470 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
471 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
472 >        checkEmpty(q);
473      }
474  
475      /**
# Line 474 | Line 477 | public class DelayQueueTest extends JSR1
477       * returning timeout status
478       */
479      public void testInterruptedTimedPoll() throws InterruptedException {
480 <        Thread t = new Thread(new CheckedRunnable() {
480 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
481 >        Thread t = newStartedThread(new CheckedRunnable() {
482              public void realRun() throws InterruptedException {
483                  DelayQueue q = populatedQueue(SIZE);
484                  for (int i = 0; i < SIZE; ++i) {
485                      assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
486                  }
487 +
488 +                Thread.currentThread().interrupt();
489 +                try {
490 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
491 +                    shouldThrow();
492 +                } catch (InterruptedException success) {}
493 +                assertFalse(Thread.interrupted());
494 +
495 +                pleaseInterrupt.countDown();
496                  try {
497 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
497 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
498                      shouldThrow();
499                  } catch (InterruptedException success) {}
500 +                assertFalse(Thread.interrupted());
501              }});
502  
503 <        t.start();
504 <        delay(SHORT_DELAY_MS);
503 >        await(pleaseInterrupt);
504 >        assertThreadStaysAlive(t);
505          t.interrupt();
506 <        t.join();
506 >        awaitTermination(t);
507      }
508  
509      /**
# Line 500 | Line 514 | public class DelayQueueTest extends JSR1
514          final DelayQueue q = new DelayQueue();
515          final PDelay pdelay = new PDelay(0);
516          final CheckedBarrier barrier = new CheckedBarrier(2);
517 <        Thread t = new Thread(new CheckedRunnable() {
517 >        Thread t = newStartedThread(new CheckedRunnable() {
518              public void realRun() throws InterruptedException {
519                  assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
520  
521                  barrier.await();
522 <                assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
522 >                assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS));
523  
524                  Thread.currentThread().interrupt();
525                  try {
526 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
526 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
527                      shouldThrow();
528                  } catch (InterruptedException success) {}
529 +                assertFalse(Thread.interrupted());
530  
531                  barrier.await();
532                  try {
533 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
533 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
534                      shouldThrow();
535                  } catch (InterruptedException success) {}
536 +                assertFalse(Thread.interrupted());
537              }});
538  
523        t.start();
539          barrier.await();
540 <        assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS));
540 >        long startTime = System.nanoTime();
541 >        assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS));
542 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
543 >
544          barrier.await();
545 <        sleep(SHORT_DELAY_MS);
545 >        assertThreadStaysAlive(t);
546          t.interrupt();
547 <        t.join();
547 >        awaitTermination(t);
548      }
549  
532
550      /**
551       * peek returns next element, or null if empty
552       */
# Line 692 | Line 709 | public class DelayQueueTest extends JSR1
709              assertSame(ints[i], q.remove());
710      }
711  
695
712      /**
713       * toArray(null) throws NullPointerException
714       */
# Line 746 | Line 762 | public class DelayQueueTest extends JSR1
762          assertFalse(it.hasNext());
763      }
764  
749
765      /**
766       * toString contains toStrings of elements
767       */
# Line 754 | Line 769 | public class DelayQueueTest extends JSR1
769          DelayQueue q = populatedQueue(SIZE);
770          String s = q.toString();
771          for (int i = 0; i < SIZE; ++i) {
772 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
772 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
773          }
774      }
775  
776      /**
777 <     * offer transfers elements across Executor tasks
777 >     * timed poll transfers elements across Executor tasks
778       */
779      public void testPollInExecutor() {
780          final DelayQueue q = new DelayQueue();
781 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
782          ExecutorService executor = Executors.newFixedThreadPool(2);
783          executor.execute(new CheckedRunnable() {
784              public void realRun() throws InterruptedException {
785                  assertNull(q.poll());
786 <                assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
787 <                assertTrue(q.isEmpty());
786 >                threadsStarted.await();
787 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
788 >                checkEmpty(q);
789              }});
790  
791          executor.execute(new CheckedRunnable() {
792              public void realRun() throws InterruptedException {
793 <                delay(SHORT_DELAY_MS);
793 >                threadsStarted.await();
794                  q.put(new PDelay(1));
795              }});
796  
797          joinPool(executor);
798      }
799  
783
800      /**
801       * Delayed actions do not occur until their delay elapses
802       */
# Line 810 | Line 826 | public class DelayQueueTest extends JSR1
826          assertNotNull(q.peek());
827      }
828  
813
829      /**
830       * poll of a non-empty queue returns null if no expired elements.
831       */
# Line 933 | Line 948 | public class DelayQueueTest extends JSR1
948          }
949      }
950  
936
951   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines