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.34 by jsr166, Sat Oct 9 19:30:35 2010 UTC vs.
Revision 1.42 by jsr166, Sun May 29 13:45:35 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       */
146 <    public void testunconfigurableExecutorService() {
146 >    public void testUnconfigurableExecutorService() {
147          ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
148          e.execute(new NoOpRunnable());
149          e.execute(new NoOpRunnable());
# Line 159 | Line 154 | public class ExecutorsTest extends JSR16
154      /**
155       * unconfigurableExecutorService(null) throws NPE
156       */
157 <    public void testunconfigurableExecutorServiceNPE() {
157 >    public void testUnconfigurableExecutorServiceNPE() {
158          try {
159              ExecutorService e = Executors.unconfigurableExecutorService(null);
160              shouldThrow();
# Line 169 | Line 164 | public class ExecutorsTest extends JSR16
164      /**
165       * unconfigurableScheduledExecutorService(null) throws NPE
166       */
167 <    public void testunconfigurableScheduledExecutorServiceNPE() {
167 >    public void testUnconfigurableScheduledExecutorServiceNPE() {
168          try {
169              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
170              shouldThrow();
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 proceed = new CountDownLatch(1);
181 >            final Runnable task = new CheckedRunnable() {
182 >                public void realRun() {
183 >                    await(proceed);
184 >                }};
185 >            long startTime = System.nanoTime();
186 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
187 >                                  timeoutMillis(), MILLISECONDS);
188 >            assertFalse(f.isDone());
189 >            proceed.countDown();
190 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
191 >            assertSame(Boolean.TRUE, f.get());
192 >            assertTrue(f.isDone());
193 >            assertFalse(f.isCancelled());
194 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
195 >        } finally {
196 >            joinPool(p);
197 >        }
198      }
199  
200      /**
201       * a newScheduledThreadPool successfully runs delayed task
202       */
203 <    public void testnewScheduledThreadPool() throws Exception {
204 <        TrackedCallable callable = new TrackedCallable();
205 <        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
206 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
207 <        assertFalse(callable.done);
208 <        Thread.sleep(MEDIUM_DELAY_MS);
209 <        assertTrue(callable.done);
210 <        assertEquals(Boolean.TRUE, f.get());
211 <        joinPool(p1);
203 >    public void testNewScheduledThreadPool() throws Exception {
204 >        ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
205 >        try {
206 >            final CountDownLatch proceed = new CountDownLatch(1);
207 >            final Runnable task = new CheckedRunnable() {
208 >                public void realRun() {
209 >                    await(proceed);
210 >                }};
211 >            long startTime = System.nanoTime();
212 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
213 >                                  timeoutMillis(), MILLISECONDS);
214 >            assertFalse(f.isDone());
215 >            proceed.countDown();
216 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
217 >            assertSame(Boolean.TRUE, f.get());
218 >            assertTrue(f.isDone());
219 >            assertFalse(f.isCancelled());
220 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
221 >        } finally {
222 >            joinPool(p);
223 >        }
224      }
225  
226      /**
227       * an unconfigurable newScheduledThreadPool successfully runs delayed task
228       */
229 <    public void testunconfigurableScheduledExecutorService() throws Exception {
230 <        TrackedCallable callable = new TrackedCallable();
231 <        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
232 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
233 <        assertFalse(callable.done);
234 <        Thread.sleep(MEDIUM_DELAY_MS);
235 <        assertTrue(callable.done);
236 <        assertEquals(Boolean.TRUE, f.get());
237 <        joinPool(p1);
229 >    public void testUnconfigurableScheduledExecutorService() throws Exception {
230 >        ScheduledExecutorService p =
231 >            Executors.unconfigurableScheduledExecutorService
232 >            (Executors.newScheduledThreadPool(2));
233 >        try {
234 >            final CountDownLatch proceed = new CountDownLatch(1);
235 >            final Runnable task = new CheckedRunnable() {
236 >                public void realRun() {
237 >                    await(proceed);
238 >                }};
239 >            long startTime = System.nanoTime();
240 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
241 >                                  timeoutMillis(), MILLISECONDS);
242 >            assertFalse(f.isDone());
243 >            proceed.countDown();
244 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
245 >            assertSame(Boolean.TRUE, f.get());
246 >            assertTrue(f.isDone());
247 >            assertFalse(f.isCancelled());
248 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
249 >        } finally {
250 >            joinPool(p);
251 >        }
252      }
253  
254      /**
255       * Future.get on submitted tasks will time out if they compute too long.
256       */
257      public void testTimedCallable() throws Exception {
258 +        final ExecutorService[] executors = {
259 +            Executors.newSingleThreadExecutor(),
260 +            Executors.newCachedThreadPool(),
261 +            Executors.newFixedThreadPool(2),
262 +            Executors.newScheduledThreadPool(2),
263 +        };
264 +
265          final Runnable sleeper = new CheckedInterruptedRunnable() {
266              public void realRun() throws InterruptedException {
267 <                Thread.sleep(LONG_DELAY_MS);
267 >                delay(LONG_DELAY_MS);
268              }};
269 <        for (ExecutorService executor :
270 <                 new ExecutorService[] {
271 <                     Executors.newSingleThreadExecutor(),
272 <                     Executors.newCachedThreadPool(),
273 <                     Executors.newFixedThreadPool(2),
274 <                     Executors.newScheduledThreadPool(2),
275 <                 }) {
276 <            try {
277 <                Future future = executor.submit(sleeper);
239 <                try {
240 <                    future.get(SHORT_DELAY_MS, MILLISECONDS);
241 <                    shouldThrow();
242 <                } catch (TimeoutException success) {
243 <                } finally {
244 <                    future.cancel(true);
245 <                }
246 <            }
247 <            finally {
248 <                joinPool(executor);
249 <            }
269 >
270 >        List<Thread> threads = new ArrayList<Thread>();
271 >        for (final ExecutorService executor : executors) {
272 >            threads.add(newStartedThread(new CheckedRunnable() {
273 >                public void realRun() {
274 >                    long startTime = System.nanoTime();
275 >                    Future future = executor.submit(sleeper);
276 >                    assertFutureTimesOut(future);
277 >                }}));
278          }
279 +        for (Thread thread : threads)
280 +            awaitTermination(thread);
281 +        for (ExecutorService executor : executors)
282 +            joinPool(executor);
283      }
284  
253
285      /**
286       * ThreadPoolExecutor using defaultThreadFactory has
287       * specified group, priority, daemon status, and name
288       */
289      public void testDefaultThreadFactory() throws Exception {
290          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
291 +        final CountDownLatch done = new CountDownLatch(1);
292          Runnable r = new CheckedRunnable() {
293              public void realRun() {
294                  try {
# Line 274 | Line 306 | public class ExecutorsTest extends JSR16
306                  } catch (SecurityException ok) {
307                      // Also pass if not allowed to change setting
308                  }
309 +                done.countDown();
310              }};
311          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
312  
313          e.execute(r);
314 +        await(done);
315 +
316          try {
317              e.shutdown();
318          } catch (SecurityException ok) {
319          }
320  
321 <        try {
287 <            Thread.sleep(SHORT_DELAY_MS);
288 <        } finally {
289 <            joinPool(e);
290 <        }
321 >        joinPool(e);
322      }
323  
324      /**
# Line 296 | Line 327 | public class ExecutorsTest extends JSR16
327       * access control context and context class loader
328       */
329      public void testPrivilegedThreadFactory() throws Exception {
330 +        final CountDownLatch done = new CountDownLatch(1);
331          Runnable r = new CheckedRunnable() {
332              public void realRun() throws Exception {
333                  final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
# Line 316 | Line 348 | public class ExecutorsTest extends JSR16
348                          assertTrue(name.endsWith("thread-1"));
349                          assertSame(thisccl, current.getContextClassLoader());
350                          assertEquals(thisacc, AccessController.getContext());
351 +                        done.countDown();
352                      }};
353                  ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
354                  e.execute(r);
355 +                await(done);
356                  e.shutdown();
323                Thread.sleep(SHORT_DELAY_MS);
357                  joinPool(e);
358              }};
359  
# Line 358 | Line 391 | public class ExecutorsTest extends JSR16
391          }
392      }
393  
361
394      /**
395       * Without class loader permissions, creating
396       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 381 | Line 413 | public class ExecutorsTest extends JSR16
413       * With class loader permissions, calling
414       * privilegedCallableUsingCurrentClassLoader does not throw ACE
415       */
416 <    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
416 >    public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception {
417          Runnable r = new CheckedRunnable() {
418              public void realRun() throws Exception {
419                  Executors.privilegedCallableUsingCurrentClassLoader
# Line 397 | Line 429 | public class ExecutorsTest extends JSR16
429      /**
430       * Without permissions, calling privilegedCallable throws ACE
431       */
432 <    public void testprivilegedCallableWithNoPrivs() throws Exception {
432 >    public void testPrivilegedCallableWithNoPrivs() throws Exception {
433          // Avoid classloader-related SecurityExceptions in swingui.TestRunner
434          Executors.privilegedCallable(new CheckCCL());
435  
# Line 469 | Line 501 | public class ExecutorsTest extends JSR16
501      /**
502       * With permissions, calling privilegedCallable succeeds
503       */
504 <    public void testprivilegedCallableWithPrivs() throws Exception {
504 >    public void testPrivilegedCallableWithPrivs() throws Exception {
505          Runnable r = new CheckedRunnable() {
506              public void realRun() throws Exception {
507                  Executors.privilegedCallable(new CheckCCL()).call();
508              }};
509  
510 <         runWithPermissions(r,
510 >        runWithPermissions(r,
511                             new RuntimePermission("getClassLoader"),
512                             new RuntimePermission("setContextClassLoader"));
513      }
# Line 514 | Line 546 | public class ExecutorsTest extends JSR16
546          assertSame(one, c.call());
547      }
548  
517
549      /**
550       * callable(null Runnable) throws NPE
551       */
# Line 555 | Line 586 | public class ExecutorsTest extends JSR16
586          } catch (NullPointerException success) {}
587      }
588  
558
589   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines