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

Comparing jsr166/src/test/tck/ScheduledExecutorTest.java (file contents):
Revision 1.89 by jsr166, Tue Mar 28 18:13:10 2017 UTC vs.
Revision 1.93 by jsr166, Mon May 29 19:15:03 2017 UTC

# Line 73 | Line 73 | public class ScheduledExecutorTest exten
73              Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
74              assertSame(Boolean.TRUE, f.get());
75              assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
76 <            assertTrue(done.await(0L, MILLISECONDS));
76 >            assertEquals(0L, done.getCount());
77          }
78      }
79  
# Line 241 | Line 241 | public class ScheduledExecutorTest exten
241          final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
242          try (PoolCleaner cleaner = cleaner(p)) {
243              try {
244 <                TrackedCallable callable = null;
245 <                Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
244 >                Future f = p.schedule((Callable)null,
245 >                                      randomTimeout(), randomTimeUnit());
246                  shouldThrow();
247              } catch (NullPointerException success) {}
248          }
# Line 746 | Line 746 | public class ScheduledExecutorTest exten
746       * - setContinueExistingPeriodicTasksAfterShutdownPolicy
747       */
748      public void testShutdown_cancellation() throws Exception {
749 <        final int poolSize = 6;
749 >        final int poolSize = 4;
750          final ScheduledThreadPoolExecutor p
751              = new ScheduledThreadPoolExecutor(poolSize);
752          final BlockingQueue<Runnable> q = p.getQueue();
753          final ThreadLocalRandom rnd = ThreadLocalRandom.current();
754 +        final long delay = rnd.nextInt(2);
755 +        final int rounds = rnd.nextInt(1, 3);
756          final boolean effectiveDelayedPolicy;
757          final boolean effectivePeriodicPolicy;
758          final boolean effectiveRemovePolicy;
# Line 779 | Line 781 | public class ScheduledExecutorTest exten
781          assertEquals(effectiveRemovePolicy,
782                       p.getRemoveOnCancelPolicy());
783  
784 <        // System.err.println("effectiveDelayedPolicy="+effectiveDelayedPolicy);
783 <        // System.err.println("effectivePeriodicPolicy="+effectivePeriodicPolicy);
784 <        // System.err.println("effectiveRemovePolicy="+effectiveRemovePolicy);
784 >        final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean();
785  
786          // Strategy: Wedge the pool with one wave of "blocker" tasks,
787 <        // then add a second wave that waits in the queue.
787 >        // then add a second wave that waits in the queue until unblocked.
788          final AtomicInteger ran = new AtomicInteger(0);
789          final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
790          final CountDownLatch unblock = new CountDownLatch(1);
791 +        final RuntimeException exception = new RuntimeException();
792  
793 <        class Task extends CheckedRunnable {
794 <            public void realRun() throws InterruptedException {
795 <                ran.getAndIncrement();
796 <                poolBlocked.countDown();
797 <                await(unblock);
793 >        class Task implements Runnable {
794 >            public void run() {
795 >                try {
796 >                    ran.getAndIncrement();
797 >                    poolBlocked.countDown();
798 >                    await(unblock);
799 >                } catch (Throwable fail) { threadUnexpectedException(fail); }
800              }
801          }
802  
803          class PeriodicTask extends Task {
804              PeriodicTask(int rounds) { this.rounds = rounds; }
805              int rounds;
806 <            public void realRun() throws InterruptedException {
807 <                if (--rounds == 0) super.realRun();
806 >            public void run() {
807 >                if (--rounds == 0) super.run();
808 >                // throw exception to surely terminate this periodic task,
809 >                // but in a separate execution and in a detectable way.
810 >                if (rounds == -1) throw exception;
811              }
812          }
813  
# Line 812 | Line 818 | public class ScheduledExecutorTest exten
818          List<Future<?>> periodics  = new ArrayList<>();
819  
820          immediates.add(p.submit(task));
821 <        delayeds.add(p.schedule(task, 1, MILLISECONDS));
822 <        for (int rounds : new int[] { 1, 2 }) {
823 <            periodics.add(p.scheduleAtFixedRate(
824 <                              new PeriodicTask(rounds), 1, 1, MILLISECONDS));
825 <            periodics.add(p.scheduleWithFixedDelay(
820 <                              new PeriodicTask(rounds), 1, 1, MILLISECONDS));
821 <        }
821 >        delayeds.add(p.schedule(task, delay, MILLISECONDS));
822 >        periodics.add(p.scheduleAtFixedRate(
823 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
824 >        periodics.add(p.scheduleWithFixedDelay(
825 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
826  
827          await(poolBlocked);
828  
829          assertEquals(poolSize, ran.get());
830 +        assertEquals(poolSize, p.getActiveCount());
831          assertTrue(q.isEmpty());
832  
833          // Add second wave of tasks.
834          immediates.add(p.submit(task));
835 <        long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS;
836 <        delayeds.add(p.schedule(task, delay_ms, MILLISECONDS));
837 <        for (int rounds : new int[] { 1, 2 }) {
838 <            periodics.add(p.scheduleAtFixedRate(
839 <                              new PeriodicTask(rounds), 1, 1, MILLISECONDS));
835 <            periodics.add(p.scheduleWithFixedDelay(
836 <                              new PeriodicTask(rounds), 1, 1, MILLISECONDS));
837 <        }
835 >        delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS));
836 >        periodics.add(p.scheduleAtFixedRate(
837 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
838 >        periodics.add(p.scheduleWithFixedDelay(
839 >                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
840  
841          assertEquals(poolSize, q.size());
842          assertEquals(poolSize, ran.get());
# Line 864 | Line 866 | public class ScheduledExecutorTest exten
866          assertTrue(!effectiveDelayedPolicy
867                     ^ q.contains(delayeds.get(1)));
868          assertTrue(!effectivePeriodicPolicy
869 <                   ^ q.containsAll(periodics.subList(4, 8)));
869 >                   ^ q.containsAll(periodics.subList(2, 4)));
870  
871          immediates.forEach(f -> assertFalse(f.isDone()));
872  
# Line 874 | Line 876 | public class ScheduledExecutorTest exten
876          else
877              assertTrue(delayeds.get(1).isCancelled());
878  
879 <        if (testImplementationDetails) {
880 <            if (effectivePeriodicPolicy)
881 <                // TODO: ensure periodic tasks continue executing
882 <                periodics.forEach(
883 <                    f -> {
882 <                        assertFalse(f.isDone());
879 >        if (effectivePeriodicPolicy)
880 >            periodics.forEach(
881 >                f -> {
882 >                    assertFalse(f.isDone());
883 >                    if (!periodicTasksContinue) {
884                          assertTrue(f.cancel(false));
885 <                    });
886 <            else {
887 <                periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone()));
888 <                periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled()));
889 <            }
885 >                        assertTrue(f.isCancelled());
886 >                    }
887 >                });
888 >        else {
889 >            periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone()));
890 >            periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled()));
891          }
892  
893          unblock.countDown();    // Release all pool threads
# Line 896 | Line 898 | public class ScheduledExecutorTest exten
898  
899          assertTrue(q.isEmpty());
900  
901 +        Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream())
902 +            .forEach(f -> assertTrue(f.isDone()));
903 +
904          for (Future<?> f : immediates) assertNull(f.get());
905  
906          assertNull(delayeds.get(0).get());
# Line 904 | Line 909 | public class ScheduledExecutorTest exten
909          else
910              assertTrue(delayeds.get(1).isCancelled());
911  
912 <        periodics.forEach(f -> assertTrue(f.isDone()));
913 <        periodics.forEach(f -> assertTrue(f.isCancelled()));
912 >        if (periodicTasksContinue)
913 >            periodics.forEach(
914 >                f -> {
915 >                    try { f.get(); }
916 >                    catch (ExecutionException success) {
917 >                        assertSame(exception, success.getCause());
918 >                    }
919 >                    catch (Throwable fail) { threadUnexpectedException(fail); }
920 >                });
921 >        else
922 >            periodics.forEach(f -> assertTrue(f.isCancelled()));
923  
924 <        assertEquals(poolSize + 1 + (effectiveDelayedPolicy ? 1 : 0), ran.get());
924 >        assertEquals(poolSize + 1
925 >                     + (effectiveDelayedPolicy ? 1 : 0)
926 >                     + (periodicTasksContinue ? 2 : 0),
927 >                     ran.get());
928      }
929  
930      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines