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.40 by jsr166, Sun Sep 27 18:50:50 2015 UTC vs.
Revision 1.45 by jsr166, Sat Oct 3 00:37:32 2015 UTC

# Line 30 | Line 30 | import java.util.concurrent.ThreadFactor
30   import java.util.concurrent.ThreadPoolExecutor;
31   import java.util.concurrent.TimeoutException;
32   import java.util.concurrent.TimeUnit;
33 + import java.util.concurrent.atomic.AtomicInteger;
34   import java.util.concurrent.locks.Condition;
35   import java.util.concurrent.locks.ReentrantLock;
36  
# Line 100 | 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 113 | 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 124 | Line 129 | public class ThreadPoolExecutorSubclassT
129              long nanos = unit.toNanos(timeout);
130              lock.lock();
131              try {
132 <                for (;;) {
128 <                    if (done) break;
132 >                while (!done) {
133                      if (nanos < 0)
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 702 | Line 708 | public class ThreadPoolExecutorSubclassT
708       * shutdownNow returns a list containing tasks that were not run,
709       * and those tasks are drained from the queue
710       */
711 <    public void testShutdownNow() {
712 <        ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
713 <        List l;
714 <        try {
715 <            for (int i = 0; i < 5; i++)
716 <                p.execute(new MediumPossiblyInterruptedRunnable());
717 <        }
718 <        finally {
711 >    public void testShutdownNow() throws InterruptedException {
712 >        final int poolSize = 2;
713 >        final int count = 5;
714 >        final AtomicInteger ran = new AtomicInteger(0);
715 >        ThreadPoolExecutor p =
716 >            new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
717 >                          new ArrayBlockingQueue<Runnable>(10));
718 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
719 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
720 >            threadsStarted.countDown();
721              try {
722 <                l = p.shutdownNow();
723 <            } catch (SecurityException ok) { return; }
722 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
723 >            } catch (InterruptedException success) {}
724 >            ran.getAndIncrement();
725 >        }};
726 >        for (int i = 0; i < count; i++)
727 >            p.execute(waiter);
728 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
729 >        assertEquals(poolSize, p.getActiveCount());
730 >        assertEquals(0, p.getCompletedTaskCount());
731 >        final List<Runnable> queuedTasks;
732 >        try {
733 >            queuedTasks = p.shutdownNow();
734 >        } catch (SecurityException ok) {
735 >            return; // Allowed in case test doesn't have privs
736          }
737          assertTrue(p.isShutdown());
738          assertTrue(p.getQueue().isEmpty());
739 <        assertTrue(l.size() <= 4);
739 >        assertEquals(count - poolSize, queuedTasks.size());
740 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
741 >        assertTrue(p.isTerminated());
742 >        assertEquals(poolSize, ran.get());
743 >        assertEquals(poolSize, p.getCompletedTaskCount());
744      }
745  
746      // Exception Tests
# Line 1725 | Line 1749 | public class ThreadPoolExecutorSubclassT
1749              l.add(new StringTask());
1750              l.add(new StringTask());
1751              List<Future<String>> futures =
1752 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1752 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1753              assertEquals(2, futures.size());
1754              for (Future<String> future : futures)
1755                  assertSame(TEST_STRING, future.get());
# Line 1855 | Line 1879 | public class ThreadPoolExecutorSubclassT
1879          }
1880      }
1881  
1882 +    /**
1883 +     * get(cancelled task) throws CancellationException
1884 +     * (in part, a test of CustomTPE itself)
1885 +     */
1886 +    public void testGet_cancelled() throws Exception {
1887 +        final ExecutorService e =
1888 +            new CustomTPE(1, 1,
1889 +                          LONG_DELAY_MS, MILLISECONDS,
1890 +                          new LinkedBlockingQueue<Runnable>());
1891 +        try {
1892 +            final CountDownLatch blockerStarted = new CountDownLatch(1);
1893 +            final CountDownLatch done = new CountDownLatch(1);
1894 +            final List<Future<?>> futures = new ArrayList<>();
1895 +            for (int i = 0; i < 2; i++) {
1896 +                Runnable r = new CheckedRunnable() { public void realRun()
1897 +                                                         throws Throwable {
1898 +                    blockerStarted.countDown();
1899 +                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1900 +                }};
1901 +                futures.add(e.submit(r));
1902 +            }
1903 +            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1904 +            for (Future<?> future : futures) future.cancel(false);
1905 +            for (Future<?> future : futures) {
1906 +                try {
1907 +                    future.get();
1908 +                    shouldThrow();
1909 +                } catch (CancellationException success) {}
1910 +                try {
1911 +                    future.get(LONG_DELAY_MS, MILLISECONDS);
1912 +                    shouldThrow();
1913 +                } catch (CancellationException success) {}
1914 +                assertTrue(future.isCancelled());
1915 +                assertTrue(future.isDone());
1916 +            }
1917 +            done.countDown();
1918 +        } finally {
1919 +            joinPool(e);
1920 +        }
1921 +    }
1922 +
1923   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines