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.33 by jsr166, Thu Sep 16 00:52:49 2010 UTC vs.
Revision 1.51 by jsr166, Sat May 28 12:45:38 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# 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.
108       */
109 <    private DelayQueue populatedQueue(int n) {
110 <        DelayQueue q = new DelayQueue();
109 >    private DelayQueue<PDelay> populatedQueue(int n) {
110 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
111          assertTrue(q.isEmpty());
112          for (int i = n-1; i >= 0; i-=2)
113              assertTrue(q.offer(new PDelay(i)));
# Line 261 | Line 258 | public class DelayQueueTest extends JSR1
258          } catch (NullPointerException success) {}
259      }
260  
264
261      /**
262       * addAll(this) throws IAE
263       */
# Line 318 | Line 314 | public class DelayQueueTest extends JSR1
314      /**
315       * put(null) throws NPE
316       */
317 <     public void testPutNull() {
317 >    public void testPutNull() {
318          try {
319              DelayQueue q = new DelayQueue();
320              q.put(null);
321              shouldThrow();
322          } catch (NullPointerException success) {}
323 <     }
323 >    }
324  
325      /**
326       * all elements successfully put are contained
327       */
328 <     public void testPut() {
329 <         DelayQueue q = new DelayQueue();
330 <         for (int i = 0; i < SIZE; ++i) {
331 <             PDelay I = new PDelay(i);
332 <             q.put(I);
333 <             assertTrue(q.contains(I));
334 <         }
335 <         assertEquals(SIZE, q.size());
328 >    public void testPut() {
329 >        DelayQueue q = new DelayQueue();
330 >        for (int i = 0; i < SIZE; ++i) {
331 >            PDelay I = new PDelay(i);
332 >            q.put(I);
333 >            assertTrue(q.contains(I));
334 >        }
335 >        assertEquals(SIZE, q.size());
336      }
337  
338      /**
# 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 <        Thread.sleep(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 <        Thread.sleep(SMALL_DELAY_MS);
377 <        t.interrupt();
378 <        t.join();
368 >        awaitTermination(t);
369      }
370  
371      /**
# Line 391 | Line 381 | public class DelayQueueTest extends JSR1
381      /**
382       * take blocks interruptibly when empty
383       */
384 <    public void testTakeFromEmpty() throws InterruptedException {
385 <        final DelayQueue q = new DelayQueue();
386 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
387 <            public void realRun() throws InterruptedException {
388 <                q.take();
389 <            }};
384 >    public void testTakeFromEmptyBlocksInterruptibly()
385 >            throws InterruptedException {
386 >        final BlockingQueue q = new DelayQueue();
387 >        final CountDownLatch threadStarted = new CountDownLatch(1);
388 >        Thread t = newStartedThread(new CheckedRunnable() {
389 >            public void realRun() {
390 >                threadStarted.countDown();
391 >                try {
392 >                    q.take();
393 >                    shouldThrow();
394 >                } catch (InterruptedException success) {}
395 >                assertFalse(Thread.interrupted());
396 >            }});
397  
398 <        t.start();
399 <        Thread.sleep(SHORT_DELAY_MS);
398 >        await(threadStarted);
399 >        assertThreadStaysAlive(t);
400          t.interrupt();
401 <        t.join();
401 >        awaitTermination(t);
402      }
403  
404      /**
# Line 409 | 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 <        Thread.sleep(SHORT_DELAY_MS);
431 >        await(pleaseInterrupt);
432 >        assertThreadStaysAlive(t);
433          t.interrupt();
434 <        t.join();
434 >        awaitTermination(t);
435      }
436  
429
437      /**
438       * poll succeeds unless empty
439       */
# Line 439 | Line 446 | public class DelayQueueTest extends JSR1
446      }
447  
448      /**
449 <     * timed pool with zero timeout succeeds when non-empty, else times out
449 >     * timed poll with zero timeout succeeds when non-empty, else times out
450       */
451      public void testTimedPoll0() throws InterruptedException {
452          DelayQueue q = populatedQueue(SIZE);
# Line 450 | Line 457 | public class DelayQueueTest extends JSR1
457      }
458  
459      /**
460 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
460 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
461       */
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 465 | 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 <        Thread.sleep(SHORT_DELAY_MS);
503 >        await(pleaseInterrupt);
504 >        assertThreadStaysAlive(t);
505          t.interrupt();
506 <        t.join();
506 >        awaitTermination(t);
507      }
508  
509      /**
510 <     *  timed poll before a delayed offer fails; after offer succeeds;
511 <     *  on interruption throws
510 >     * timed poll before a delayed offer fails; after offer succeeds;
511 >     * on interruption throws
512       */
513      public void testTimedPollWithOffer() throws InterruptedException {
514          final DelayQueue q = new DelayQueue();
515          final PDelay pdelay = new PDelay(0);
516 <        Thread t = new Thread(new CheckedRunnable() {
516 >        final CheckedBarrier barrier = new CheckedBarrier(2);
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(LONG_DELAY_MS, MILLISECONDS));
523 +
524 +                Thread.currentThread().interrupt();
525                  try {
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(LONG_DELAY_MS, MILLISECONDS);
534 +                    shouldThrow();
535 +                } catch (InterruptedException success) {}
536 +                assertFalse(Thread.interrupted());
537              }});
538  
539 <        t.start();
540 <        Thread.sleep(SMALL_DELAY_MS);
541 <        assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS));
539 >        barrier.await();
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 >        assertThreadStaysAlive(t);
546          t.interrupt();
547 <        t.join();
547 >        awaitTermination(t);
548      }
549  
510
550      /**
551       * peek returns next element, or null if empty
552       */
# Line 654 | Line 693 | public class DelayQueueTest extends JSR1
693          Object[] o = q.toArray();
694          Arrays.sort(o);
695          for (int i = 0; i < o.length; i++)
696 <            assertEquals(o[i], q.take());
696 >            assertSame(o[i], q.take());
697      }
698  
699      /**
700       * toArray(a) contains all elements
701       */
702 <    public void testToArray2() throws InterruptedException {
703 <        DelayQueue q = populatedQueue(SIZE);
702 >    public void testToArray2() {
703 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
704          PDelay[] ints = new PDelay[SIZE];
705 <        ints = (PDelay[])q.toArray(ints);
705 >        PDelay[] array = q.toArray(ints);
706 >        assertSame(ints, array);
707          Arrays.sort(ints);
708          for (int i = 0; i < ints.length; i++)
709 <            assertEquals(ints[i], q.take());
709 >            assertSame(ints[i], q.remove());
710      }
711  
672
712      /**
713 <     * toArray(null) throws NPE
713 >     * toArray(null) throws NullPointerException
714       */
715 <    public void testToArray_BadArg() {
715 >    public void testToArray_NullArg() {
716          DelayQueue q = populatedQueue(SIZE);
717          try {
718 <            Object o[] = q.toArray(null);
718 >            q.toArray(null);
719              shouldThrow();
720          } catch (NullPointerException success) {}
721      }
722  
723      /**
724 <     * toArray with incompatible array type throws CCE
724 >     * toArray(incompatible array type) throws ArrayStoreException
725       */
726      public void testToArray1_BadArg() {
727          DelayQueue q = populatedQueue(SIZE);
728          try {
729 <            Object o[] = q.toArray(new String[10]);
729 >            q.toArray(new String[10]);
730              shouldThrow();
731          } catch (ArrayStoreException success) {}
732      }
# Line 723 | Line 762 | public class DelayQueueTest extends JSR1
762          assertFalse(it.hasNext());
763      }
764  
726
765      /**
766       * toString contains toStrings of elements
767       */
# Line 731 | 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 <                Thread.sleep(SHORT_DELAY_MS);
793 >                threadsStarted.await();
794                  q.put(new PDelay(1));
795              }});
796  
797          joinPool(executor);
798      }
799  
760
800      /**
801       * Delayed actions do not occur until their delay elapses
802       */
803      public void testDelay() throws InterruptedException {
804 <        DelayQueue q = new DelayQueue();
805 <        NanoDelay[] elements = new NanoDelay[SIZE];
806 <        for (int i = 0; i < SIZE; ++i) {
768 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
769 <        }
770 <        for (int i = 0; i < SIZE; ++i) {
771 <            q.add(elements[i]);
772 <        }
804 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
805 >        for (int i = 0; i < SIZE; ++i)
806 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
807  
808          long last = 0;
809          for (int i = 0; i < SIZE; ++i) {
810 <            NanoDelay e = (NanoDelay)(q.take());
810 >            NanoDelay e = q.take();
811              long tt = e.getTriggerTime();
812 <            assertTrue(tt <= System.nanoTime());
812 >            assertTrue(System.nanoTime() - tt >= 0);
813              if (i != 0)
814                  assertTrue(tt >= last);
815              last = tt;
816          }
817 +        assertTrue(q.isEmpty());
818      }
819  
820      /**
# Line 791 | Line 826 | public class DelayQueueTest extends JSR1
826          assertNotNull(q.peek());
827      }
828  
794
829      /**
830       * poll of a non-empty queue returns null if no expired elements.
831       */
# Line 901 | Line 935 | public class DelayQueueTest extends JSR1
935      }
936  
937      /**
938 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
938 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
939       */
940      public void testDrainToN() {
941          for (int i = 0; i < SIZE + 2; ++i) {
942              DelayQueue q = populatedQueue(SIZE);
943              ArrayList l = new ArrayList();
944              q.drainTo(l, i);
945 <            int k = (i < SIZE)? i : SIZE;
945 >            int k = (i < SIZE) ? i : SIZE;
946              assertEquals(q.size(), SIZE-k);
947              assertEquals(l.size(), k);
948          }
949      }
950  
917
951   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines