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

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.24 by jsr166, Wed Aug 25 00:07:02 2010 UTC vs.
Revision 1.38 by jsr166, Mon Sep 14 03:14:43 2015 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   */
8  
9
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.math.BigInteger;
11 < import java.security.*;
10 >
11 > import java.security.PrivilegedAction;
12 > import java.security.PrivilegedExceptionAction;
13 > import java.util.ArrayList;
14 > import java.util.Collections;
15 > import java.util.List;
16 > import java.util.concurrent.AbstractExecutorService;
17 > import java.util.concurrent.ArrayBlockingQueue;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CancellationException;
20 > import java.util.concurrent.CountDownLatch;
21 > import java.util.concurrent.ExecutionException;
22 > import java.util.concurrent.Executors;
23 > import java.util.concurrent.ExecutorService;
24 > import java.util.concurrent.Future;
25 > import java.util.concurrent.ThreadPoolExecutor;
26 > import java.util.concurrent.TimeUnit;
27 > import java.util.concurrent.atomic.AtomicBoolean;
28 >
29 > import junit.framework.Test;
30 > import junit.framework.TestSuite;
31  
32   public class AbstractExecutorServiceTest extends JSR166TestCase {
33      public static void main(String[] args) {
34 <        junit.textui.TestRunner.run(suite());
34 >        main(suite(), args);
35      }
36      public static Test suite() {
37          return new TestSuite(AbstractExecutorServiceTest.class);
# Line 29 | Line 44 | public class AbstractExecutorServiceTest
44      static class DirectExecutorService extends AbstractExecutorService {
45          public void execute(Runnable r) { r.run(); }
46          public void shutdown() { shutdown = true; }
47 <        public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
47 >        public List<Runnable> shutdownNow() {
48 >            shutdown = true;
49 >            return Collections.EMPTY_LIST;
50 >        }
51          public boolean isShutdown() { return shutdown; }
52          public boolean isTerminated() { return isShutdown(); }
53 <        public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
53 >        public boolean awaitTermination(long timeout, TimeUnit unit) {
54 >            return isShutdown();
55 >        }
56          private volatile boolean shutdown = false;
57      }
58  
# Line 41 | Line 61 | public class AbstractExecutorServiceTest
61       */
62      public void testExecuteRunnable() throws Exception {
63          ExecutorService e = new DirectExecutorService();
64 <        TrackedShortRunnable task = new TrackedShortRunnable();
65 <        assertFalse(task.done);
66 <        Future<?> future = e.submit(task);
67 <        future.get();
68 <        assertTrue(task.done);
64 >        final AtomicBoolean done = new AtomicBoolean(false);
65 >        Future<?> future = e.submit(new CheckedRunnable() {
66 >            public void realRun() {
67 >                done.set(true);
68 >            }});
69 >        assertNull(future.get());
70 >        assertNull(future.get(0, MILLISECONDS));
71 >        assertTrue(done.get());
72 >        assertTrue(future.isDone());
73 >        assertFalse(future.isCancelled());
74      }
75  
51
76      /**
77       * Completed submit(callable) returns result
78       */
# Line 79 | Line 103 | public class AbstractExecutorServiceTest
103          assertSame(TEST_STRING, result);
104      }
105  
82
106      /**
107       * A submitted privileged action runs to completion
108       */
# Line 145 | Line 168 | public class AbstractExecutorServiceTest
168       * execute(null runnable) throws NPE
169       */
170      public void testExecuteNullRunnable() {
171 +        ExecutorService e = new DirectExecutorService();
172          try {
149            ExecutorService e = new DirectExecutorService();
173              e.submit((Runnable) null);
174              shouldThrow();
175          } catch (NullPointerException success) {}
176      }
177  
155
178      /**
179       * submit(null callable) throws NPE
180       */
181      public void testSubmitNullCallable() {
182 +        ExecutorService e = new DirectExecutorService();
183          try {
161            ExecutorService e = new DirectExecutorService();
184              e.submit((Callable) null);
185              shouldThrow();
186          } catch (NullPointerException success) {}
187      }
188  
189      /**
190 <     * submit(runnable) throws RejectedExecutionException if
169 <     * executor is saturated.
170 <     */
171 <    public void testExecute1() {
172 <        ThreadPoolExecutor p =
173 <            new ThreadPoolExecutor(1, 1,
174 <                                   60, TimeUnit.SECONDS,
175 <                                   new ArrayBlockingQueue<Runnable>(1));
176 <        try {
177 <            for (int i = 0; i < 2; ++i)
178 <                p.submit(new MediumRunnable());
179 <            for (int i = 0; i < 2; ++i) {
180 <                try {
181 <                    p.submit(new MediumRunnable());
182 <                    shouldThrow();
183 <                } catch (RejectedExecutionException success) {}
184 <            }
185 <        } finally {
186 <            joinPool(p);
187 <        }
188 <    }
189 <
190 <    /**
191 <     * submit(callable) throws RejectedExecutionException
192 <     * if executor is saturated.
190 >     * submit(callable).get() throws InterruptedException if interrupted
191       */
192 <    public void testExecute2() {
193 <        ThreadPoolExecutor p =
194 <            new ThreadPoolExecutor(1, 1,
195 <                                   60, TimeUnit.SECONDS,
196 <                                   new ArrayBlockingQueue<Runnable>(1));
192 >    public void testInterruptedSubmit() throws InterruptedException {
193 >        final CountDownLatch submitted    = new CountDownLatch(1);
194 >        final CountDownLatch quittingTime = new CountDownLatch(1);
195 >        final ExecutorService p
196 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
197 >                                     new ArrayBlockingQueue<Runnable>(10));
198 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
199 >            public Void realCall() throws InterruptedException {
200 >                quittingTime.await();
201 >                return null;
202 >            }};
203          try {
204 <            for (int i = 0; i < 2; ++i)
205 <                p.submit(new MediumRunnable());
206 <            for (int i = 0; i < 2; ++i) {
207 <                try {
208 <                    p.submit(new SmallCallable());
209 <                    shouldThrow();
210 <                } catch (RejectedExecutionException success) {}
211 <            }
204 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
205 >                public void realRun() throws Exception {
206 >                    Future<Void> future = p.submit(awaiter);
207 >                    submitted.countDown();
208 >                    future.get();
209 >                }});
210 >            t.start();
211 >            submitted.await();
212 >            t.interrupt();
213 >            t.join();
214          } finally {
215 +            quittingTime.countDown();
216              joinPool(p);
217          }
218      }
219  
213
214    /**
215     *  Blocking on submit(callable) throws InterruptedException if
216     *  caller interrupted.
217     */
218    public void testInterruptedSubmit() throws InterruptedException {
219        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
220        Thread t = new Thread(new CheckedInterruptedRunnable() {
221            public void realRun() throws Exception {
222                p.submit(new CheckedCallable<Object>() {
223                             public Object realCall()
224                                 throws InterruptedException {
225                                 Thread.sleep(SMALL_DELAY_MS);
226                                 return null;
227                             }}).get();
228            }});
229
230        t.start();
231        Thread.sleep(SHORT_DELAY_MS);
232        t.interrupt();
233        joinPool(p);
234    }
235
220      /**
221 <     *  get of submitted callable throws InterruptedException if callable
222 <     *  interrupted
239 <     */
240 <    public void testSubmitIE() throws InterruptedException {
241 <        final ThreadPoolExecutor p =
242 <            new ThreadPoolExecutor(1, 1,
243 <                                   60, TimeUnit.SECONDS,
244 <                                   new ArrayBlockingQueue<Runnable>(10));
245 <
246 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
247 <            public void realRun() throws Exception {
248 <                p.submit(new SmallCallable()).get();
249 <            }});
250 <
251 <        t.start();
252 <        Thread.sleep(SHORT_DELAY_MS);
253 <        t.interrupt();
254 <        t.join();
255 <        joinPool(p);
256 <    }
257 <
258 <    /**
259 <     *  get of submit(callable) throws ExecutionException if callable
260 <     *  throws exception
221 >     * get of submit(callable) throws ExecutionException if callable
222 >     * throws exception
223       */
224      public void testSubmitEE() throws InterruptedException {
225          ThreadPoolExecutor p =
# Line 266 | Line 228 | public class AbstractExecutorServiceTest
228                                     new ArrayBlockingQueue<Runnable>(10));
229  
230          Callable c = new Callable() {
231 <            public Object call() { return 5/0; }};
231 >            public Object call() { throw new ArithmeticException(); }};
232  
233          try {
234              p.submit(c).get();
# Line 280 | Line 242 | public class AbstractExecutorServiceTest
242      /**
243       * invokeAny(null) throws NPE
244       */
245 <    public void testInvokeAny1()
284 <        throws InterruptedException, ExecutionException {
245 >    public void testInvokeAny1() throws Exception {
246          ExecutorService e = new DirectExecutorService();
247          try {
248              e.invokeAny(null);
# Line 295 | Line 256 | public class AbstractExecutorServiceTest
256      /**
257       * invokeAny(empty collection) throws IAE
258       */
259 <    public void testInvokeAny2()
299 <        throws InterruptedException, ExecutionException {
259 >    public void testInvokeAny2() throws Exception {
260          ExecutorService e = new DirectExecutorService();
261          try {
262              e.invokeAny(new ArrayList<Callable<String>>());
# Line 312 | Line 272 | public class AbstractExecutorServiceTest
272       */
273      public void testInvokeAny3() throws Exception {
274          ExecutorService e = new DirectExecutorService();
275 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
276 <        l.add(new Callable<Integer>() {
277 <                  public Integer call() { return 5/0; }});
275 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
276 >        l.add(new Callable<Long>() {
277 >            public Long call() { throw new ArithmeticException(); }});
278          l.add(null);
279          try {
280              e.invokeAny(l);
# Line 441 | Line 401 | public class AbstractExecutorServiceTest
401          }
402      }
403  
444
404      /**
405       * timed invokeAny(null) throws NPE
406       */
# Line 491 | Line 450 | public class AbstractExecutorServiceTest
450       */
451      public void testTimedInvokeAny3() throws Exception {
452          ExecutorService e = new DirectExecutorService();
453 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
454 <        l.add(new Callable<Integer>() {
455 <                  public Integer call() { return 5/0; }});
453 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
454 >        l.add(new Callable<Long>() {
455 >            public Long call() { throw new ArithmeticException(); }});
456          l.add(null);
457          try {
458              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 629 | Line 588 | public class AbstractExecutorServiceTest
588              l.add(new StringTask());
589              l.add(new StringTask());
590              List<Future<String>> futures =
591 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
591 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
592              assertEquals(2, futures.size());
593              for (Future<String> future : futures)
594                  assertSame(TEST_STRING, future.get());
# Line 641 | Line 600 | public class AbstractExecutorServiceTest
600      /**
601       * timed invokeAll cancels tasks not completed by timeout
602       */
603 <    public void testTimedInvokeAll6() throws InterruptedException {
603 >    public void testTimedInvokeAll6() throws Exception {
604          ExecutorService e = new DirectExecutorService();
605          try {
606 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
607 <            l.add(new StringTask());
608 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
609 <            l.add(new StringTask());
606 >            long timeout = timeoutMillis();
607 >            List<Callable<String>> tasks = new ArrayList<>();
608 >            tasks.add(new StringTask("0"));
609 >            tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
610 >                                         TEST_STRING));
611 >            tasks.add(new StringTask("2"));
612 >            long startTime = System.nanoTime();
613              List<Future<String>> futures =
614 <                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
615 <            assertEquals(3, futures.size());
616 <            Iterator<Future<String>> it = futures.iterator();
617 <            Future<String> f1 = it.next();
618 <            Future<String> f2 = it.next();
619 <            Future<String> f3 = it.next();
620 <            assertTrue(f1.isDone());
621 <            assertFalse(f1.isCancelled());
660 <            assertTrue(f2.isDone());
661 <            assertTrue(f3.isDone());
662 <            assertTrue(f3.isCancelled());
614 >                e.invokeAll(tasks, timeout, MILLISECONDS);
615 >            assertEquals(tasks.size(), futures.size());
616 >            assertTrue(millisElapsedSince(startTime) >= timeout);
617 >            for (Future future : futures)
618 >                assertTrue(future.isDone());
619 >            assertEquals("0", futures.get(0).get());
620 >            assertEquals(TEST_STRING, futures.get(1).get());
621 >            assertTrue(futures.get(2).isCancelled());
622          } finally {
623              joinPool(e);
624          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines