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.50 by jsr166, Fri May 27 20:07:24 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 474 | Line 472 | public class DelayQueueTest extends JSR1
472       * returning timeout status
473       */
474      public void testInterruptedTimedPoll() throws InterruptedException {
475 <        Thread t = new Thread(new CheckedRunnable() {
475 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
476 >        Thread t = newStartedThread(new CheckedRunnable() {
477              public void realRun() throws InterruptedException {
478                  DelayQueue q = populatedQueue(SIZE);
479                  for (int i = 0; i < SIZE; ++i) {
480                      assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
481                  }
482 +
483 +                Thread.currentThread().interrupt();
484 +                try {
485 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
486 +                    shouldThrow();
487 +                } catch (InterruptedException success) {}
488 +                assertFalse(Thread.interrupted());
489 +
490 +                pleaseInterrupt.countDown();
491                  try {
492 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
492 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
493                      shouldThrow();
494                  } catch (InterruptedException success) {}
495 +                assertFalse(Thread.interrupted());
496              }});
497  
498 <        t.start();
499 <        delay(SHORT_DELAY_MS);
498 >        await(pleaseInterrupt);
499 >        assertThreadStaysAlive(t);
500          t.interrupt();
501 <        t.join();
501 >        awaitTermination(t);
502      }
503  
504      /**
# Line 500 | Line 509 | public class DelayQueueTest extends JSR1
509          final DelayQueue q = new DelayQueue();
510          final PDelay pdelay = new PDelay(0);
511          final CheckedBarrier barrier = new CheckedBarrier(2);
512 <        Thread t = new Thread(new CheckedRunnable() {
512 >        Thread t = newStartedThread(new CheckedRunnable() {
513              public void realRun() throws InterruptedException {
514                  assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
515  
516                  barrier.await();
517 <                assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
517 >                assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS));
518  
519                  Thread.currentThread().interrupt();
520                  try {
521 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
521 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
522                      shouldThrow();
523                  } catch (InterruptedException success) {}
524 +                assertFalse(Thread.interrupted());
525  
526                  barrier.await();
527                  try {
528 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
528 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
529                      shouldThrow();
530                  } catch (InterruptedException success) {}
531 +                assertFalse(Thread.interrupted());
532              }});
533  
523        t.start();
534          barrier.await();
535 <        assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS));
535 >        long startTime = System.nanoTime();
536 >        assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS));
537 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
538 >
539          barrier.await();
540 <        sleep(SHORT_DELAY_MS);
540 >        assertThreadStaysAlive(t);
541          t.interrupt();
542 <        t.join();
542 >        awaitTermination(t);
543      }
544  
532
545      /**
546       * peek returns next element, or null if empty
547       */
# Line 692 | Line 704 | public class DelayQueueTest extends JSR1
704              assertSame(ints[i], q.remove());
705      }
706  
695
707      /**
708       * toArray(null) throws NullPointerException
709       */
# Line 746 | Line 757 | public class DelayQueueTest extends JSR1
757          assertFalse(it.hasNext());
758      }
759  
749
760      /**
761       * toString contains toStrings of elements
762       */
# Line 754 | Line 764 | public class DelayQueueTest extends JSR1
764          DelayQueue q = populatedQueue(SIZE);
765          String s = q.toString();
766          for (int i = 0; i < SIZE; ++i) {
767 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
767 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
768          }
769      }
770  
771      /**
772 <     * offer transfers elements across Executor tasks
772 >     * timed poll transfers elements across Executor tasks
773       */
774      public void testPollInExecutor() {
775          final DelayQueue q = new DelayQueue();
776 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
777          ExecutorService executor = Executors.newFixedThreadPool(2);
778          executor.execute(new CheckedRunnable() {
779              public void realRun() throws InterruptedException {
780                  assertNull(q.poll());
781 <                assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
782 <                assertTrue(q.isEmpty());
781 >                threadsStarted.await();
782 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
783 >                checkEmpty(q);
784              }});
785  
786          executor.execute(new CheckedRunnable() {
787              public void realRun() throws InterruptedException {
788 <                delay(SHORT_DELAY_MS);
788 >                threadsStarted.await();
789                  q.put(new PDelay(1));
790              }});
791  
792          joinPool(executor);
793      }
794  
783
795      /**
796       * Delayed actions do not occur until their delay elapses
797       */
# Line 810 | Line 821 | public class DelayQueueTest extends JSR1
821          assertNotNull(q.peek());
822      }
823  
813
824      /**
825       * poll of a non-empty queue returns null if no expired elements.
826       */
# Line 933 | Line 943 | public class DelayQueueTest extends JSR1
943          }
944      }
945  
936
946   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines