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

Comparing jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java (file contents):
Revision 1.41 by jsr166, Mon Sep 28 02:41:29 2015 UTC vs.
Revision 1.50 by jsr166, Sun Oct 4 00:40:33 2015 UTC

# Line 101 | Line 101 | public class ThreadPoolExecutorSubclassT
101              }
102              lock.lock();
103              try {
104 <                result = v;
105 <                exception = e;
106 <                done = true;
107 <                thread = null;
108 <                cond.signalAll();
104 >                if (!done) {
105 >                    result = v;
106 >                    exception = e;
107 >                    done = true;
108 >                    thread = null;
109 >                    cond.signalAll();
110 >                }
111              }
112              finally { lock.unlock(); }
113          }
# Line 114 | Line 116 | public class ThreadPoolExecutorSubclassT
116              try {
117                  while (!done)
118                      cond.await();
119 +                if (cancelled)
120 +                    throw new CancellationException();
121                  if (exception != null)
122                      throw new ExecutionException(exception);
123                  return result;
# Line 125 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
133 <                    if (done) break;
130 <                    if (nanos < 0)
132 >                while (!done) {
133 >                    if (nanos <= 0L)
134                          throw new TimeoutException();
135                      nanos = cond.awaitNanos(nanos);
136                  }
137 +                if (cancelled)
138 +                    throw new CancellationException();
139                  if (exception != null)
140                      throw new ExecutionException(exception);
141                  return result;
# Line 227 | Line 232 | public class ThreadPoolExecutorSubclassT
232      public void testExecute() throws InterruptedException {
233          final ThreadPoolExecutor p =
234              new CustomTPE(1, 1,
235 <                          LONG_DELAY_MS, MILLISECONDS,
235 >                          2 * LONG_DELAY_MS, MILLISECONDS,
236                            new ArrayBlockingQueue<Runnable>(10));
237 <        final CountDownLatch done = new CountDownLatch(1);
238 <        final Runnable task = new CheckedRunnable() {
239 <            public void realRun() {
240 <                done.countDown();
236 <            }};
237 <        try {
237 >        try (PoolCleaner<ThreadPoolExecutor> cleaner = cleaner(p)) {
238 >            final CountDownLatch done = new CountDownLatch(1);
239 >            final Runnable task = new CheckedRunnable() {
240 >                public void realRun() { done.countDown(); }};
241              p.execute(task);
242 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
240 <        } finally {
241 <            joinPool(p);
242 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243          }
244      }
245  
# Line 711 | Line 712 | public class ThreadPoolExecutorSubclassT
712              new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
713                            new ArrayBlockingQueue<Runnable>(10));
714          CountDownLatch threadsStarted = new CountDownLatch(poolSize);
715 <        CheckedRunnable waiter = new CheckedRunnable() { public void realRun() {
715 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
716              threadsStarted.countDown();
717              try {
718                  MILLISECONDS.sleep(2 * LONG_DELAY_MS);
# Line 721 | Line 722 | public class ThreadPoolExecutorSubclassT
722          for (int i = 0; i < count; i++)
723              p.execute(waiter);
724          assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
725 +        assertEquals(poolSize, p.getActiveCount());
726 +        assertEquals(0, p.getCompletedTaskCount());
727          final List<Runnable> queuedTasks;
728          try {
729              queuedTasks = p.shutdownNow();
# Line 733 | Line 736 | public class ThreadPoolExecutorSubclassT
736          assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
737          assertTrue(p.isTerminated());
738          assertEquals(poolSize, ran.get());
739 +        assertEquals(poolSize, p.getCompletedTaskCount());
740      }
741  
742      // Exception Tests
# Line 1741 | Line 1745 | public class ThreadPoolExecutorSubclassT
1745              l.add(new StringTask());
1746              l.add(new StringTask());
1747              List<Future<String>> futures =
1748 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1748 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1749              assertEquals(2, futures.size());
1750              for (Future<String> future : futures)
1751                  assertSame(TEST_STRING, future.get());
# Line 1871 | Line 1875 | public class ThreadPoolExecutorSubclassT
1875          }
1876      }
1877  
1878 +    /**
1879 +     * get(cancelled task) throws CancellationException
1880 +     * (in part, a test of CustomTPE itself)
1881 +     */
1882 +    public void testGet_cancelled() throws Exception {
1883 +        final ExecutorService e =
1884 +            new CustomTPE(1, 1,
1885 +                          LONG_DELAY_MS, MILLISECONDS,
1886 +                          new LinkedBlockingQueue<Runnable>());
1887 +        try {
1888 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1889 +            final CountDownLatch done = new CountDownLatch(1);
1890 +            final List<Future<?>> futures = new ArrayList<>();
1891 +            for (int i = 0; i < 2; i++) {
1892 +                Runnable r = new CheckedRunnable() { public void realRun()
1893 +                                                         throws Throwable {
1894 +                    blockerStarted.countDown();
1895 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1896 +                }};
1897 +                futures.add(e.submit(r));
1898 +            }
1899 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1900 +            for (Future<?> future : futures) future.cancel(false);
1901 +            for (Future<?> future : futures) {
1902 +                try {
1903 +                    future.get();
1904 +                    shouldThrow();
1905 +                } catch (CancellationException success) {}
1906 +                try {
1907 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1908 +                    shouldThrow();
1909 +                } catch (CancellationException success) {}
1910 +                assertTrue(future.isCancelled());
1911 +                assertTrue(future.isDone());
1912 +            }
1913 +            done.countDown();
1914 +        } finally {
1915 +            joinPool(e);
1916 +        }
1917 +    }
1918 +
1919   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines