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.27 by jsr166, Sat Oct 9 19:30:34 2010 UTC vs.
Revision 1.36 by jsr166, Sat Feb 28 18:12:58 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.CountDownLatch;
20 > import java.util.concurrent.ExecutionException;
21 > import java.util.concurrent.Executors;
22 > import java.util.concurrent.ExecutorService;
23 > import java.util.concurrent.Future;
24 > import java.util.concurrent.ThreadPoolExecutor;
25 > import java.util.concurrent.TimeUnit;
26 > import java.util.concurrent.atomic.AtomicBoolean;
27 >
28 > import junit.framework.Test;
29 > import junit.framework.TestSuite;
30  
31   public class AbstractExecutorServiceTest extends JSR166TestCase {
32      public static void main(String[] args) {
# Line 46 | Line 60 | public class AbstractExecutorServiceTest
60       */
61      public void testExecuteRunnable() throws Exception {
62          ExecutorService e = new DirectExecutorService();
63 <        TrackedShortRunnable task = new TrackedShortRunnable();
64 <        assertFalse(task.done);
65 <        Future<?> future = e.submit(task);
66 <        future.get();
67 <        assertTrue(task.done);
63 >        final AtomicBoolean done = new AtomicBoolean(false);
64 >        Future<?> future = e.submit(new CheckedRunnable() {
65 >            public void realRun() {
66 >                done.set(true);
67 >            }});
68 >        assertNull(future.get());
69 >        assertNull(future.get(0, MILLISECONDS));
70 >        assertTrue(done.get());
71 >        assertTrue(future.isDone());
72 >        assertFalse(future.isCancelled());
73      }
74  
56
75      /**
76       * Completed submit(callable) returns result
77       */
# Line 84 | Line 102 | public class AbstractExecutorServiceTest
102          assertSame(TEST_STRING, result);
103      }
104  
87
105      /**
106       * A submitted privileged action runs to completion
107       */
# Line 150 | Line 167 | public class AbstractExecutorServiceTest
167       * execute(null runnable) throws NPE
168       */
169      public void testExecuteNullRunnable() {
170 +        ExecutorService e = new DirectExecutorService();
171          try {
154            ExecutorService e = new DirectExecutorService();
172              e.submit((Runnable) null);
173              shouldThrow();
174          } catch (NullPointerException success) {}
175      }
176  
160
177      /**
178       * submit(null callable) throws NPE
179       */
180      public void testSubmitNullCallable() {
181 +        ExecutorService e = new DirectExecutorService();
182          try {
166            ExecutorService e = new DirectExecutorService();
183              e.submit((Callable) null);
184              shouldThrow();
185          } catch (NullPointerException success) {}
186      }
187  
188      /**
173     * submit(runnable) throws RejectedExecutionException if
174     * executor is saturated.
175     */
176    public void testExecute1() {
177        ThreadPoolExecutor p =
178            new ThreadPoolExecutor(1, 1,
179                                   60, TimeUnit.SECONDS,
180                                   new ArrayBlockingQueue<Runnable>(1));
181        try {
182            for (int i = 0; i < 2; ++i)
183                p.submit(new MediumRunnable());
184            for (int i = 0; i < 2; ++i) {
185                try {
186                    p.submit(new MediumRunnable());
187                    shouldThrow();
188                } catch (RejectedExecutionException success) {}
189            }
190        } finally {
191            joinPool(p);
192        }
193    }
194
195    /**
196     * submit(callable) throws RejectedExecutionException
197     * if executor is saturated.
198     */
199    public void testExecute2() {
200        ThreadPoolExecutor p =
201            new ThreadPoolExecutor(1, 1,
202                                   60, TimeUnit.SECONDS,
203                                   new ArrayBlockingQueue<Runnable>(1));
204        try {
205            for (int i = 0; i < 2; ++i)
206                p.submit(new MediumRunnable());
207            for (int i = 0; i < 2; ++i) {
208                try {
209                    p.submit(new SmallCallable());
210                    shouldThrow();
211                } catch (RejectedExecutionException success) {}
212            }
213        } finally {
214            joinPool(p);
215        }
216    }
217
218
219    /**
189       * submit(callable).get() throws InterruptedException if interrupted
190       */
191      public void testInterruptedSubmit() throws InterruptedException {
# Line 248 | Line 217 | public class AbstractExecutorServiceTest
217      }
218  
219      /**
251     * get of submitted callable throws InterruptedException if callable
252     * interrupted
253     */
254    public void testSubmitIE() throws InterruptedException {
255        final ThreadPoolExecutor p =
256            new ThreadPoolExecutor(1, 1,
257                                   60, TimeUnit.SECONDS,
258                                   new ArrayBlockingQueue<Runnable>(10));
259
260        Thread t = new Thread(new CheckedInterruptedRunnable() {
261            public void realRun() throws Exception {
262                p.submit(new SmallCallable()).get();
263            }});
264
265        t.start();
266        Thread.sleep(SHORT_DELAY_MS);
267        t.interrupt();
268        t.join();
269        joinPool(p);
270    }
271
272    /**
220       * get of submit(callable) throws ExecutionException if callable
221       * throws exception
222       */
# Line 280 | Line 227 | public class AbstractExecutorServiceTest
227                                     new ArrayBlockingQueue<Runnable>(10));
228  
229          Callable c = new Callable() {
230 <            public Object call() { return 5/0; }};
230 >            public Object call() { throw new ArithmeticException(); }};
231  
232          try {
233              p.submit(c).get();
# Line 294 | Line 241 | public class AbstractExecutorServiceTest
241      /**
242       * invokeAny(null) throws NPE
243       */
244 <    public void testInvokeAny1()
298 <        throws InterruptedException, ExecutionException {
244 >    public void testInvokeAny1() throws Exception {
245          ExecutorService e = new DirectExecutorService();
246          try {
247              e.invokeAny(null);
# Line 309 | Line 255 | public class AbstractExecutorServiceTest
255      /**
256       * invokeAny(empty collection) throws IAE
257       */
258 <    public void testInvokeAny2()
313 <        throws InterruptedException, ExecutionException {
258 >    public void testInvokeAny2() throws Exception {
259          ExecutorService e = new DirectExecutorService();
260          try {
261              e.invokeAny(new ArrayList<Callable<String>>());
# Line 326 | Line 271 | public class AbstractExecutorServiceTest
271       */
272      public void testInvokeAny3() throws Exception {
273          ExecutorService e = new DirectExecutorService();
274 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
275 <        l.add(new Callable<Integer>() {
276 <                  public Integer call() { return 5/0; }});
274 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
275 >        l.add(new Callable<Long>() {
276 >            public Long call() { throw new ArithmeticException(); }});
277          l.add(null);
278          try {
279              e.invokeAny(l);
# Line 455 | Line 400 | public class AbstractExecutorServiceTest
400          }
401      }
402  
458
403      /**
404       * timed invokeAny(null) throws NPE
405       */
# Line 505 | Line 449 | public class AbstractExecutorServiceTest
449       */
450      public void testTimedInvokeAny3() throws Exception {
451          ExecutorService e = new DirectExecutorService();
452 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
453 <        l.add(new Callable<Integer>() {
454 <                  public Integer call() { return 5/0; }});
452 >        List<Callable<Long>> l = new ArrayList<Callable<Long>>();
453 >        l.add(new Callable<Long>() {
454 >            public Long call() { throw new ArithmeticException(); }});
455          l.add(null);
456          try {
457              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 660 | Line 604 | public class AbstractExecutorServiceTest
604          try {
605              List<Callable<String>> l = new ArrayList<Callable<String>>();
606              l.add(new StringTask());
607 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
607 >            l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
608              l.add(new StringTask());
609              List<Future<String>> futures =
610 <                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
611 <            assertEquals(3, futures.size());
612 <            Iterator<Future<String>> it = futures.iterator();
613 <            Future<String> f1 = it.next();
614 <            Future<String> f2 = it.next();
615 <            Future<String> f3 = it.next();
616 <            assertTrue(f1.isDone());
673 <            assertFalse(f1.isCancelled());
674 <            assertTrue(f2.isDone());
675 <            assertTrue(f3.isDone());
676 <            assertTrue(f3.isCancelled());
610 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
611 >            assertEquals(l.size(), futures.size());
612 >            for (Future future : futures)
613 >                assertTrue(future.isDone());
614 >            assertFalse(futures.get(0).isCancelled());
615 >            assertFalse(futures.get(1).isCancelled());
616 >            assertTrue(futures.get(2).isCancelled());
617          } finally {
618              joinPool(e);
619          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines