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.25 by jsr166, Thu Sep 16 02:54:10 2010 UTC vs.
Revision 1.34 by jsr166, Wed Sep 25 07:39:17 2013 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
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import java.util.concurrent.atomic.AtomicBoolean;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import java.math.BigInteger;
14   import java.security.*;
15  
16   public class AbstractExecutorServiceTest extends JSR166TestCase {
# Line 29 | Line 28 | public class AbstractExecutorServiceTest
28      static class DirectExecutorService extends AbstractExecutorService {
29          public void execute(Runnable r) { r.run(); }
30          public void shutdown() { shutdown = true; }
31 <        public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
31 >        public List<Runnable> shutdownNow() {
32 >            shutdown = true;
33 >            return Collections.EMPTY_LIST;
34 >        }
35          public boolean isShutdown() { return shutdown; }
36          public boolean isTerminated() { return isShutdown(); }
37 <        public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
37 >        public boolean awaitTermination(long timeout, TimeUnit unit) {
38 >            return isShutdown();
39 >        }
40          private volatile boolean shutdown = false;
41      }
42  
# Line 41 | Line 45 | public class AbstractExecutorServiceTest
45       */
46      public void testExecuteRunnable() throws Exception {
47          ExecutorService e = new DirectExecutorService();
48 <        TrackedShortRunnable task = new TrackedShortRunnable();
49 <        assertFalse(task.done);
50 <        Future<?> future = e.submit(task);
51 <        future.get();
52 <        assertTrue(task.done);
48 >        final AtomicBoolean done = new AtomicBoolean(false);
49 >        Future<?> future = e.submit(new CheckedRunnable() {
50 >            public void realRun() {
51 >                done.set(true);
52 >            }});
53 >        assertNull(future.get());
54 >        assertNull(future.get(0, MILLISECONDS));
55 >        assertTrue(done.get());
56 >        assertTrue(future.isDone());
57 >        assertFalse(future.isCancelled());
58      }
59  
51
60      /**
61       * Completed submit(callable) returns result
62       */
# Line 79 | Line 87 | public class AbstractExecutorServiceTest
87          assertSame(TEST_STRING, result);
88      }
89  
82
90      /**
91       * A submitted privileged action runs to completion
92       */
# Line 152 | Line 159 | public class AbstractExecutorServiceTest
159          } catch (NullPointerException success) {}
160      }
161  
155
162      /**
163       * submit(null callable) throws NPE
164       */
# Line 165 | Line 171 | public class AbstractExecutorServiceTest
171      }
172  
173      /**
168     * 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.
193     */
194    public void testExecute2() {
195        ThreadPoolExecutor p =
196            new ThreadPoolExecutor(1, 1,
197                                   60, TimeUnit.SECONDS,
198                                   new ArrayBlockingQueue<Runnable>(1));
199        try {
200            for (int i = 0; i < 2; ++i)
201                p.submit(new MediumRunnable());
202            for (int i = 0; i < 2; ++i) {
203                try {
204                    p.submit(new SmallCallable());
205                    shouldThrow();
206                } catch (RejectedExecutionException success) {}
207            }
208        } finally {
209            joinPool(p);
210        }
211    }
212
213
214    /**
174       * submit(callable).get() throws InterruptedException if interrupted
175       */
176      public void testInterruptedSubmit() throws InterruptedException {
# Line 243 | Line 202 | public class AbstractExecutorServiceTest
202      }
203  
204      /**
205 <     *  get of submitted callable throws InterruptedException if callable
206 <     *  interrupted
248 <     */
249 <    public void testSubmitIE() throws InterruptedException {
250 <        final ThreadPoolExecutor p =
251 <            new ThreadPoolExecutor(1, 1,
252 <                                   60, TimeUnit.SECONDS,
253 <                                   new ArrayBlockingQueue<Runnable>(10));
254 <
255 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
256 <            public void realRun() throws Exception {
257 <                p.submit(new SmallCallable()).get();
258 <            }});
259 <
260 <        t.start();
261 <        Thread.sleep(SHORT_DELAY_MS);
262 <        t.interrupt();
263 <        t.join();
264 <        joinPool(p);
265 <    }
266 <
267 <    /**
268 <     *  get of submit(callable) throws ExecutionException if callable
269 <     *  throws exception
205 >     * get of submit(callable) throws ExecutionException if callable
206 >     * throws exception
207       */
208      public void testSubmitEE() throws InterruptedException {
209          ThreadPoolExecutor p =
# Line 275 | Line 212 | public class AbstractExecutorServiceTest
212                                     new ArrayBlockingQueue<Runnable>(10));
213  
214          Callable c = new Callable() {
215 <            public Object call() { return 5/0; }};
215 >            public Object call() { throw new ArithmeticException(); }};
216  
217          try {
218              p.submit(c).get();
# Line 289 | Line 226 | public class AbstractExecutorServiceTest
226      /**
227       * invokeAny(null) throws NPE
228       */
229 <    public void testInvokeAny1()
293 <        throws InterruptedException, ExecutionException {
229 >    public void testInvokeAny1() throws Exception {
230          ExecutorService e = new DirectExecutorService();
231          try {
232              e.invokeAny(null);
# Line 304 | Line 240 | public class AbstractExecutorServiceTest
240      /**
241       * invokeAny(empty collection) throws IAE
242       */
243 <    public void testInvokeAny2()
308 <        throws InterruptedException, ExecutionException {
243 >    public void testInvokeAny2() throws Exception {
244          ExecutorService e = new DirectExecutorService();
245          try {
246              e.invokeAny(new ArrayList<Callable<String>>());
# Line 321 | Line 256 | public class AbstractExecutorServiceTest
256       */
257      public void testInvokeAny3() throws Exception {
258          ExecutorService e = new DirectExecutorService();
259 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
260 <        l.add(new Callable<Integer>() {
261 <                  public Integer call() { return 5/0; }});
259 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
260 >        l.add(new Callable<Long>() {
261 >            public Long call() { throw new ArithmeticException(); }});
262          l.add(null);
263          try {
264              e.invokeAny(l);
# Line 450 | Line 385 | public class AbstractExecutorServiceTest
385          }
386      }
387  
453
388      /**
389       * timed invokeAny(null) throws NPE
390       */
# Line 500 | Line 434 | public class AbstractExecutorServiceTest
434       */
435      public void testTimedInvokeAny3() throws Exception {
436          ExecutorService e = new DirectExecutorService();
437 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
438 <        l.add(new Callable<Integer>() {
439 <                  public Integer call() { return 5/0; }});
437 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
438 >        l.add(new Callable<Long>() {
439 >            public Long call() { throw new ArithmeticException(); }});
440          l.add(null);
441          try {
442              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 655 | Line 589 | public class AbstractExecutorServiceTest
589          try {
590              List<Callable<String>> l = new ArrayList<Callable<String>>();
591              l.add(new StringTask());
592 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
592 >            l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
593              l.add(new StringTask());
594              List<Future<String>> futures =
595 <                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
596 <            assertEquals(3, futures.size());
597 <            Iterator<Future<String>> it = futures.iterator();
598 <            Future<String> f1 = it.next();
599 <            Future<String> f2 = it.next();
600 <            Future<String> f3 = it.next();
601 <            assertTrue(f1.isDone());
668 <            assertFalse(f1.isCancelled());
669 <            assertTrue(f2.isDone());
670 <            assertTrue(f3.isDone());
671 <            assertTrue(f3.isCancelled());
595 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
596 >            assertEquals(l.size(), futures.size());
597 >            for (Future future : futures)
598 >                assertTrue(future.isDone());
599 >            assertFalse(futures.get(0).isCancelled());
600 >            assertFalse(futures.get(1).isCancelled());
601 >            assertTrue(futures.get(2).isCancelled());
602          } finally {
603              joinPool(e);
604          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines