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.30 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.41 by jsr166, Sun May 29 06:54:23 2011 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 static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import java.math.BigInteger;
13   import java.security.*;
14  
15   public class ExecutorsTest extends JSR166TestCase {
# Line 54 | Line 52 | public class ExecutorsTest extends JSR16
52          } catch (NullPointerException success) {}
53      }
54  
57
55      /**
56       * A new SingleThreadExecutor can execute runnables
57       */
# Line 101 | Line 98 | public class ExecutorsTest extends JSR16
98          }
99      }
100  
104
101      /**
102       * A new newFixedThreadPool can execute runnables
103       */
# Line 144 | Line 140 | public class ExecutorsTest extends JSR16
140          } catch (IllegalArgumentException success) {}
141      }
142  
147
143      /**
144       * An unconfigurable newFixedThreadPool can execute runnables
145       */
# Line 176 | Line 171 | public class ExecutorsTest extends JSR16
171          } catch (NullPointerException success) {}
172      }
173  
179
174      /**
175       * a newSingleThreadScheduledExecutor successfully runs delayed task
176       */
177      public void testNewSingleThreadScheduledExecutor() throws Exception {
178 <        TrackedCallable callable = new TrackedCallable();
179 <        ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
180 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
181 <        assertFalse(callable.done);
182 <        Thread.sleep(MEDIUM_DELAY_MS);
183 <        assertTrue(callable.done);
184 <        assertEquals(Boolean.TRUE, f.get());
185 <        joinPool(p1);
178 >        ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
179 >        try {
180 >            final CountDownLatch done = new CountDownLatch(1);
181 >            final Runnable task = new CheckedRunnable() {
182 >                public void realRun() {
183 >                    done.countDown();
184 >                }};
185 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
186 >                                  SHORT_DELAY_MS, MILLISECONDS);
187 >            assertFalse(f.isDone());
188 >            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
189 >            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
190 >            assertSame(Boolean.TRUE, f.get());
191 >            assertTrue(f.isDone());
192 >        } finally {
193 >            joinPool(p);
194 >        }
195      }
196  
197      /**
198       * a newScheduledThreadPool successfully runs delayed task
199       */
200      public void testnewScheduledThreadPool() throws Exception {
201 <        TrackedCallable callable = new TrackedCallable();
202 <        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
203 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
204 <        assertFalse(callable.done);
205 <        Thread.sleep(MEDIUM_DELAY_MS);
206 <        assertTrue(callable.done);
207 <        assertEquals(Boolean.TRUE, f.get());
208 <        joinPool(p1);
201 >        ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
202 >        try {
203 >            final CountDownLatch done = new CountDownLatch(1);
204 >            final Runnable task = new CheckedRunnable() {
205 >                public void realRun() {
206 >                    done.countDown();
207 >                }};
208 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
209 >                                  SHORT_DELAY_MS, MILLISECONDS);
210 >            assertFalse(f.isDone());
211 >            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
212 >            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
213 >            assertSame(Boolean.TRUE, f.get());
214 >            assertTrue(f.isDone());
215 >        } finally {
216 >            joinPool(p);
217 >        }
218      }
219  
220      /**
221       * an unconfigurable newScheduledThreadPool successfully runs delayed task
222       */
223      public void testunconfigurableScheduledExecutorService() throws Exception {
224 <        TrackedCallable callable = new TrackedCallable();
225 <        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
226 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
227 <        assertFalse(callable.done);
228 <        Thread.sleep(MEDIUM_DELAY_MS);
229 <        assertTrue(callable.done);
230 <        assertEquals(Boolean.TRUE, f.get());
231 <        joinPool(p1);
224 >        ScheduledExecutorService p =
225 >            Executors.unconfigurableScheduledExecutorService
226 >            (Executors.newScheduledThreadPool(2));
227 >        try {
228 >            final CountDownLatch done = new CountDownLatch(1);
229 >            final Runnable task = new CheckedRunnable() {
230 >                public void realRun() {
231 >                    done.countDown();
232 >                }};
233 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
234 >                                  SHORT_DELAY_MS, MILLISECONDS);
235 >            assertFalse(f.isDone());
236 >            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
237 >            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
238 >            assertSame(Boolean.TRUE, f.get());
239 >            assertTrue(f.isDone());
240 >        } finally {
241 >            joinPool(p);
242 >        }
243      }
244  
245      /**
246 <     *  Future.get on submitted tasks will time out if they compute too long.
246 >     * Future.get on submitted tasks will time out if they compute too long.
247       */
248      public void testTimedCallable() throws Exception {
249 <        final Runnable sleeper =
250 <            new RunnableShouldThrow(InterruptedException.class) {
251 <                public void realRun() throws InterruptedException {
252 <                    Thread.sleep(LONG_DELAY_MS);
253 <                }};
254 <        for (ExecutorService executor :
255 <                 new ExecutorService[] {
256 <                     Executors.newSingleThreadExecutor(),
257 <                     Executors.newCachedThreadPool(),
258 <                     Executors.newFixedThreadPool(2),
259 <                     Executors.newScheduledThreadPool(2),
260 <                 }) {
261 <            try {
262 <                Future future = executor.submit(sleeper);
263 <                try {
264 <                    future.get(SHORT_DELAY_MS, MILLISECONDS);
265 <                    shouldThrow();
266 <                } catch (TimeoutException success) {
267 <                } finally {
268 <                    future.cancel(true);
246 <                }
247 <            }
248 <            finally {
249 <                joinPool(executor);
250 <            }
249 >        final ExecutorService[] executors = {
250 >            Executors.newSingleThreadExecutor(),
251 >            Executors.newCachedThreadPool(),
252 >            Executors.newFixedThreadPool(2),
253 >            Executors.newScheduledThreadPool(2),
254 >        };
255 >
256 >        final Runnable sleeper = new CheckedInterruptedRunnable() {
257 >            public void realRun() throws InterruptedException {
258 >                delay(LONG_DELAY_MS);
259 >            }};
260 >
261 >        List<Thread> threads = new ArrayList<Thread>();
262 >        for (final ExecutorService executor : executors) {
263 >            threads.add(newStartedThread(new CheckedRunnable() {
264 >                public void realRun() {
265 >                    long startTime = System.nanoTime();
266 >                    Future future = executor.submit(sleeper);
267 >                    assertFutureTimesOut(future);
268 >                }}));
269          }
270 +        for (Thread thread : threads)
271 +            awaitTermination(thread);
272 +        for (ExecutorService executor : executors)
273 +            joinPool(executor);
274      }
275  
254
276      /**
277       * ThreadPoolExecutor using defaultThreadFactory has
278       * specified group, priority, daemon status, and name
279       */
280      public void testDefaultThreadFactory() throws Exception {
281          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
282 <        Runnable r = new Runnable() {
283 <                public void run() {
284 <                    try {
285 <                        Thread current = Thread.currentThread();
286 <                        threadAssertTrue(!current.isDaemon());
287 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
288 <                        ThreadGroup g = current.getThreadGroup();
289 <                        SecurityManager s = System.getSecurityManager();
290 <                        if (s != null)
291 <                            threadAssertTrue(g == s.getThreadGroup());
292 <                        else
293 <                            threadAssertTrue(g == egroup);
294 <                        String name = current.getName();
295 <                        threadAssertTrue(name.endsWith("thread-1"));
296 <                    } catch (SecurityException ok) {
297 <                        // Also pass if not allowed to change setting
277 <                    }
282 >        Runnable r = new CheckedRunnable() {
283 >            public void realRun() {
284 >                try {
285 >                    Thread current = Thread.currentThread();
286 >                    assertTrue(!current.isDaemon());
287 >                    assertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
288 >                    ThreadGroup g = current.getThreadGroup();
289 >                    SecurityManager s = System.getSecurityManager();
290 >                    if (s != null)
291 >                        assertTrue(g == s.getThreadGroup());
292 >                    else
293 >                        assertTrue(g == egroup);
294 >                    String name = current.getName();
295 >                    assertTrue(name.endsWith("thread-1"));
296 >                } catch (SecurityException ok) {
297 >                    // Also pass if not allowed to change setting
298                  }
299 <            };
299 >            }};
300          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
301  
302          e.execute(r);
# Line 286 | Line 306 | public class ExecutorsTest extends JSR16
306          }
307  
308          try {
309 <            Thread.sleep(SHORT_DELAY_MS);
309 >            delay(SHORT_DELAY_MS);
310          } finally {
311              joinPool(e);
312          }
# Line 316 | Line 336 | public class ExecutorsTest extends JSR16
336                              assertTrue(g == egroup);
337                          String name = current.getName();
338                          assertTrue(name.endsWith("thread-1"));
339 <                        assertTrue(thisccl == current.getContextClassLoader());
340 <                        assertTrue(thisacc.equals(AccessController.getContext()));
339 >                        assertSame(thisccl, current.getContextClassLoader());
340 >                        assertEquals(thisacc, AccessController.getContext());
341                      }};
342                  ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
343                  e.execute(r);
344                  e.shutdown();
345 <                Thread.sleep(SHORT_DELAY_MS);
345 >                delay(SHORT_DELAY_MS);
346                  joinPool(e);
347              }};
348  
# Line 360 | Line 380 | public class ExecutorsTest extends JSR16
380          }
381      }
382  
363
383      /**
384       * Without class loader permissions, creating
385       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 400 | Line 419 | public class ExecutorsTest extends JSR16
419       * Without permissions, calling privilegedCallable throws ACE
420       */
421      public void testprivilegedCallableWithNoPrivs() throws Exception {
422 +        // Avoid classloader-related SecurityExceptions in swingui.TestRunner
423 +        Executors.privilegedCallable(new CheckCCL());
424 +
425          Runnable r = new CheckedRunnable() {
426              public void realRun() throws Exception {
427                  if (System.getSecurityManager() == null)
# Line 474 | Line 496 | public class ExecutorsTest extends JSR16
496                  Executors.privilegedCallable(new CheckCCL()).call();
497              }};
498  
499 <         runWithPermissions(r,
499 >        runWithPermissions(r,
500                             new RuntimePermission("getClassLoader"),
501                             new RuntimePermission("setContextClassLoader"));
502      }
# Line 513 | Line 535 | public class ExecutorsTest extends JSR16
535          assertSame(one, c.call());
536      }
537  
516
538      /**
539       * callable(null Runnable) throws NPE
540       */
# Line 554 | Line 575 | public class ExecutorsTest extends JSR16
575          } catch (NullPointerException success) {}
576      }
577  
557
578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines