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.37 by jsr166, Mon Oct 11 04:35:20 2010 UTC vs.
Revision 1.50 by jsr166, Fri May 27 20:07:24 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);
# Line 465 | 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(SMALL_DELAY_MS, MILLISECONDS);
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(LONG_DELAY_MS, MILLISECONDS);
493 +                    shouldThrow();
494 +                } catch (InterruptedException success) {}
495 +                assertFalse(Thread.interrupted());
496              }});
497  
498 <        t.start();
499 <        Thread.sleep(SHORT_DELAY_MS);
498 >        await(pleaseInterrupt);
499 >        assertThreadStaysAlive(t);
500          t.interrupt();
501 <        t.join();
501 >        awaitTermination(t);
502      }
503  
504      /**
# Line 491 | 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  
514        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  
523
545      /**
546       * peek returns next element, or null if empty
547       */
# Line 667 | Line 688 | public class DelayQueueTest extends JSR1
688          Object[] o = q.toArray();
689          Arrays.sort(o);
690          for (int i = 0; i < o.length; i++)
691 <            assertEquals(o[i], q.take());
691 >            assertSame(o[i], q.take());
692      }
693  
694      /**
695       * toArray(a) contains all elements
696       */
697 <    public void testToArray2() throws InterruptedException {
698 <        DelayQueue q = populatedQueue(SIZE);
697 >    public void testToArray2() {
698 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
699          PDelay[] ints = new PDelay[SIZE];
700 <        ints = (PDelay[])q.toArray(ints);
700 >        PDelay[] array = q.toArray(ints);
701 >        assertSame(ints, array);
702          Arrays.sort(ints);
703          for (int i = 0; i < ints.length; i++)
704 <            assertEquals(ints[i], q.take());
704 >            assertSame(ints[i], q.remove());
705      }
706  
685
707      /**
708 <     * toArray(null) throws NPE
708 >     * toArray(null) throws NullPointerException
709       */
710 <    public void testToArray_BadArg() {
710 >    public void testToArray_NullArg() {
711          DelayQueue q = populatedQueue(SIZE);
712          try {
713 <            Object o[] = q.toArray(null);
713 >            q.toArray(null);
714              shouldThrow();
715          } catch (NullPointerException success) {}
716      }
717  
718      /**
719 <     * toArray with incompatible array type throws CCE
719 >     * toArray(incompatible array type) throws ArrayStoreException
720       */
721      public void testToArray1_BadArg() {
722          DelayQueue q = populatedQueue(SIZE);
723          try {
724 <            Object o[] = q.toArray(new String[10]);
724 >            q.toArray(new String[10]);
725              shouldThrow();
726          } catch (ArrayStoreException success) {}
727      }
# Line 736 | Line 757 | public class DelayQueueTest extends JSR1
757          assertFalse(it.hasNext());
758      }
759  
739
760      /**
761       * toString contains toStrings of elements
762       */
# Line 744 | 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 <                Thread.sleep(SHORT_DELAY_MS);
788 >                threadsStarted.await();
789                  q.put(new PDelay(1));
790              }});
791  
792          joinPool(executor);
793      }
794  
773
795      /**
796       * Delayed actions do not occur until their delay elapses
797       */
# Line 800 | Line 821 | public class DelayQueueTest extends JSR1
821          assertNotNull(q.peek());
822      }
823  
803
824      /**
825       * poll of a non-empty queue returns null if no expired elements.
826       */
# Line 910 | Line 930 | public class DelayQueueTest extends JSR1
930      }
931  
932      /**
933 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
933 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
934       */
935      public void testDrainToN() {
936          for (int i = 0; i < SIZE + 2; ++i) {
937              DelayQueue q = populatedQueue(SIZE);
938              ArrayList l = new ArrayList();
939              q.drainTo(l, i);
940 <            int k = (i < SIZE)? i : SIZE;
940 >            int k = (i < SIZE) ? i : SIZE;
941              assertEquals(q.size(), SIZE-k);
942              assertEquals(l.size(), k);
943          }
944      }
945  
926
946   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines