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

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.36 by jsr166, Tue Nov 9 15:29:11 2010 UTC vs.
Revision 1.43 by jsr166, Wed Dec 31 19:05:42 2014 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.AccessControlContext;
12 > import java.security.AccessControlException;
13 > import java.security.AccessController;
14 > import java.security.PrivilegedAction;
15 > import java.security.PrivilegedExceptionAction;
16 > import java.util.ArrayList;
17 > import java.util.List;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.ScheduledExecutorService;
24 > import java.util.concurrent.ThreadPoolExecutor;
25 >
26 > import junit.framework.Test;
27 > import junit.framework.TestSuite;
28  
29   public class ExecutorsTest extends JSR166TestCase {
30      public static void main(String[] args) {
# Line 54 | Line 66 | public class ExecutorsTest extends JSR16
66          } catch (NullPointerException success) {}
67      }
68  
57
69      /**
70       * A new SingleThreadExecutor can execute runnables
71       */
# Line 101 | Line 112 | public class ExecutorsTest extends JSR16
112          }
113      }
114  
104
115      /**
116       * A new newFixedThreadPool can execute runnables
117       */
# Line 144 | Line 154 | public class ExecutorsTest extends JSR16
154          } catch (IllegalArgumentException success) {}
155      }
156  
147
157      /**
158       * An unconfigurable newFixedThreadPool can execute runnables
159       */
160 <    public void testunconfigurableExecutorService() {
160 >    public void testUnconfigurableExecutorService() {
161          ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
162          e.execute(new NoOpRunnable());
163          e.execute(new NoOpRunnable());
# Line 159 | Line 168 | public class ExecutorsTest extends JSR16
168      /**
169       * unconfigurableExecutorService(null) throws NPE
170       */
171 <    public void testunconfigurableExecutorServiceNPE() {
171 >    public void testUnconfigurableExecutorServiceNPE() {
172          try {
173              ExecutorService e = Executors.unconfigurableExecutorService(null);
174              shouldThrow();
# Line 169 | Line 178 | public class ExecutorsTest extends JSR16
178      /**
179       * unconfigurableScheduledExecutorService(null) throws NPE
180       */
181 <    public void testunconfigurableScheduledExecutorServiceNPE() {
181 >    public void testUnconfigurableScheduledExecutorServiceNPE() {
182          try {
183              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
184              shouldThrow();
185          } catch (NullPointerException success) {}
186      }
187  
179
188      /**
189       * a newSingleThreadScheduledExecutor successfully runs delayed task
190       */
191      public void testNewSingleThreadScheduledExecutor() throws Exception {
192          ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
193          try {
194 <            final CountDownLatch done = new CountDownLatch(1);
194 >            final CountDownLatch proceed = new CountDownLatch(1);
195              final Runnable task = new CheckedRunnable() {
196                  public void realRun() {
197 <                    done.countDown();
197 >                    await(proceed);
198                  }};
199 +            long startTime = System.nanoTime();
200              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
201 <                                  SHORT_DELAY_MS, MILLISECONDS);
201 >                                  timeoutMillis(), MILLISECONDS);
202              assertFalse(f.isDone());
203 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
204 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
203 >            proceed.countDown();
204 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
205              assertSame(Boolean.TRUE, f.get());
206              assertTrue(f.isDone());
207 +            assertFalse(f.isCancelled());
208 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
209          } finally {
210              joinPool(p);
211          }
# Line 203 | Line 214 | public class ExecutorsTest extends JSR16
214      /**
215       * a newScheduledThreadPool successfully runs delayed task
216       */
217 <    public void testnewScheduledThreadPool() throws Exception {
217 >    public void testNewScheduledThreadPool() throws Exception {
218          ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
219          try {
220 <            final CountDownLatch done = new CountDownLatch(1);
220 >            final CountDownLatch proceed = new CountDownLatch(1);
221              final Runnable task = new CheckedRunnable() {
222                  public void realRun() {
223 <                    done.countDown();
223 >                    await(proceed);
224                  }};
225 +            long startTime = System.nanoTime();
226              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
227 <                                  SHORT_DELAY_MS, MILLISECONDS);
227 >                                  timeoutMillis(), MILLISECONDS);
228              assertFalse(f.isDone());
229 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
230 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
229 >            proceed.countDown();
230 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
231              assertSame(Boolean.TRUE, f.get());
232              assertTrue(f.isDone());
233 +            assertFalse(f.isCancelled());
234 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
235          } finally {
236              joinPool(p);
237          }
# Line 226 | Line 240 | public class ExecutorsTest extends JSR16
240      /**
241       * an unconfigurable newScheduledThreadPool successfully runs delayed task
242       */
243 <    public void testunconfigurableScheduledExecutorService() throws Exception {
243 >    public void testUnconfigurableScheduledExecutorService() throws Exception {
244          ScheduledExecutorService p =
245              Executors.unconfigurableScheduledExecutorService
246              (Executors.newScheduledThreadPool(2));
247          try {
248 <            final CountDownLatch done = new CountDownLatch(1);
248 >            final CountDownLatch proceed = new CountDownLatch(1);
249              final Runnable task = new CheckedRunnable() {
250                  public void realRun() {
251 <                    done.countDown();
251 >                    await(proceed);
252                  }};
253 +            long startTime = System.nanoTime();
254              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
255 <                                  SHORT_DELAY_MS, MILLISECONDS);
255 >                                  timeoutMillis(), MILLISECONDS);
256              assertFalse(f.isDone());
257 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
258 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
257 >            proceed.countDown();
258 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
259              assertSame(Boolean.TRUE, f.get());
260              assertTrue(f.isDone());
261 +            assertFalse(f.isCancelled());
262 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
263          } finally {
264              joinPool(p);
265          }
# Line 252 | Line 269 | public class ExecutorsTest extends JSR16
269       * Future.get on submitted tasks will time out if they compute too long.
270       */
271      public void testTimedCallable() throws Exception {
272 +        final ExecutorService[] executors = {
273 +            Executors.newSingleThreadExecutor(),
274 +            Executors.newCachedThreadPool(),
275 +            Executors.newFixedThreadPool(2),
276 +            Executors.newScheduledThreadPool(2),
277 +        };
278 +
279          final Runnable sleeper = new CheckedInterruptedRunnable() {
280              public void realRun() throws InterruptedException {
281 <                Thread.sleep(LONG_DELAY_MS);
281 >                delay(LONG_DELAY_MS);
282              }};
283 <        for (ExecutorService executor :
284 <                 new ExecutorService[] {
285 <                     Executors.newSingleThreadExecutor(),
286 <                     Executors.newCachedThreadPool(),
287 <                     Executors.newFixedThreadPool(2),
288 <                     Executors.newScheduledThreadPool(2),
289 <                 }) {
290 <            try {
291 <                Future future = executor.submit(sleeper);
268 <                try {
269 <                    future.get(SHORT_DELAY_MS, MILLISECONDS);
270 <                    shouldThrow();
271 <                } catch (TimeoutException success) {
272 <                } finally {
273 <                    future.cancel(true);
274 <                }
275 <            }
276 <            finally {
277 <                joinPool(executor);
278 <            }
283 >
284 >        List<Thread> threads = new ArrayList<Thread>();
285 >        for (final ExecutorService executor : executors) {
286 >            threads.add(newStartedThread(new CheckedRunnable() {
287 >                public void realRun() {
288 >                    long startTime = System.nanoTime();
289 >                    Future future = executor.submit(sleeper);
290 >                    assertFutureTimesOut(future);
291 >                }}));
292          }
293 +        for (Thread thread : threads)
294 +            awaitTermination(thread);
295 +        for (ExecutorService executor : executors)
296 +            joinPool(executor);
297      }
298  
282
299      /**
300       * ThreadPoolExecutor using defaultThreadFactory has
301       * specified group, priority, daemon status, and name
302       */
303      public void testDefaultThreadFactory() throws Exception {
304          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
305 +        final CountDownLatch done = new CountDownLatch(1);
306          Runnable r = new CheckedRunnable() {
307              public void realRun() {
308                  try {
# Line 303 | Line 320 | public class ExecutorsTest extends JSR16
320                  } catch (SecurityException ok) {
321                      // Also pass if not allowed to change setting
322                  }
323 +                done.countDown();
324              }};
325          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
326  
327          e.execute(r);
328 +        await(done);
329 +
330          try {
331              e.shutdown();
332          } catch (SecurityException ok) {
333          }
334  
335 <        try {
316 <            Thread.sleep(SHORT_DELAY_MS);
317 <        } finally {
318 <            joinPool(e);
319 <        }
335 >        joinPool(e);
336      }
337  
338      /**
# Line 325 | Line 341 | public class ExecutorsTest extends JSR16
341       * access control context and context class loader
342       */
343      public void testPrivilegedThreadFactory() throws Exception {
344 +        final CountDownLatch done = new CountDownLatch(1);
345          Runnable r = new CheckedRunnable() {
346              public void realRun() throws Exception {
347                  final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
# Line 345 | Line 362 | public class ExecutorsTest extends JSR16
362                          assertTrue(name.endsWith("thread-1"));
363                          assertSame(thisccl, current.getContextClassLoader());
364                          assertEquals(thisacc, AccessController.getContext());
365 +                        done.countDown();
366                      }};
367                  ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
368                  e.execute(r);
369 +                await(done);
370                  e.shutdown();
352                Thread.sleep(SHORT_DELAY_MS);
371                  joinPool(e);
372              }};
373  
# Line 387 | Line 405 | public class ExecutorsTest extends JSR16
405          }
406      }
407  
390
408      /**
409       * Without class loader permissions, creating
410       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 410 | Line 427 | public class ExecutorsTest extends JSR16
427       * With class loader permissions, calling
428       * privilegedCallableUsingCurrentClassLoader does not throw ACE
429       */
430 <    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
430 >    public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception {
431          Runnable r = new CheckedRunnable() {
432              public void realRun() throws Exception {
433                  Executors.privilegedCallableUsingCurrentClassLoader
# Line 426 | Line 443 | public class ExecutorsTest extends JSR16
443      /**
444       * Without permissions, calling privilegedCallable throws ACE
445       */
446 <    public void testprivilegedCallableWithNoPrivs() throws Exception {
446 >    public void testPrivilegedCallableWithNoPrivs() throws Exception {
447          // Avoid classloader-related SecurityExceptions in swingui.TestRunner
448          Executors.privilegedCallable(new CheckCCL());
449  
# Line 498 | Line 515 | public class ExecutorsTest extends JSR16
515      /**
516       * With permissions, calling privilegedCallable succeeds
517       */
518 <    public void testprivilegedCallableWithPrivs() throws Exception {
518 >    public void testPrivilegedCallableWithPrivs() throws Exception {
519          Runnable r = new CheckedRunnable() {
520              public void realRun() throws Exception {
521                  Executors.privilegedCallable(new CheckCCL()).call();
522              }};
523  
524 <         runWithPermissions(r,
524 >        runWithPermissions(r,
525                             new RuntimePermission("getClassLoader"),
526                             new RuntimePermission("setContextClassLoader"));
527      }
# Line 543 | Line 560 | public class ExecutorsTest extends JSR16
560          assertSame(one, c.call());
561      }
562  
546
563      /**
564       * callable(null Runnable) throws NPE
565       */
# Line 584 | Line 600 | public class ExecutorsTest extends JSR16
600          } catch (NullPointerException success) {}
601      }
602  
587
603   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines