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.37 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.42 by jsr166, Sun May 29 13:45:35 2011 UTC

# Line 6 | Line 6
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          ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
179          try {
180 <            final CountDownLatch done = new CountDownLatch(1);
180 >            final CountDownLatch proceed = new CountDownLatch(1);
181              final Runnable task = new CheckedRunnable() {
182                  public void realRun() {
183 <                    done.countDown();
183 >                    await(proceed);
184                  }};
185 +            long startTime = System.nanoTime();
186              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
187 <                                  SHORT_DELAY_MS, MILLISECONDS);
187 >                                  timeoutMillis(), MILLISECONDS);
188              assertFalse(f.isDone());
189 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
190 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
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          }
# Line 203 | Line 200 | public class ExecutorsTest extends JSR16
200      /**
201       * a newScheduledThreadPool successfully runs delayed task
202       */
203 <    public void testnewScheduledThreadPool() throws Exception {
203 >    public void testNewScheduledThreadPool() throws Exception {
204          ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
205          try {
206 <            final CountDownLatch done = new CountDownLatch(1);
206 >            final CountDownLatch proceed = new CountDownLatch(1);
207              final Runnable task = new CheckedRunnable() {
208                  public void realRun() {
209 <                    done.countDown();
209 >                    await(proceed);
210                  }};
211 +            long startTime = System.nanoTime();
212              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
213 <                                  SHORT_DELAY_MS, MILLISECONDS);
213 >                                  timeoutMillis(), MILLISECONDS);
214              assertFalse(f.isDone());
215 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
216 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
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          }
# Line 226 | Line 226 | public class ExecutorsTest extends JSR16
226      /**
227       * an unconfigurable newScheduledThreadPool successfully runs delayed task
228       */
229 <    public void testunconfigurableScheduledExecutorService() throws Exception {
229 >    public void testUnconfigurableScheduledExecutorService() throws Exception {
230          ScheduledExecutorService p =
231              Executors.unconfigurableScheduledExecutorService
232              (Executors.newScheduledThreadPool(2));
233          try {
234 <            final CountDownLatch done = new CountDownLatch(1);
234 >            final CountDownLatch proceed = new CountDownLatch(1);
235              final Runnable task = new CheckedRunnable() {
236                  public void realRun() {
237 <                    done.countDown();
237 >                    await(proceed);
238                  }};
239 +            long startTime = System.nanoTime();
240              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
241 <                                  SHORT_DELAY_MS, MILLISECONDS);
241 >                                  timeoutMillis(), MILLISECONDS);
242              assertFalse(f.isDone());
243 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
244 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
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          }
# Line 252 | Line 255 | public class ExecutorsTest extends JSR16
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);
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 <            }
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  
282
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 303 | 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 {
316 <            Thread.sleep(SHORT_DELAY_MS);
317 <        } finally {
318 <            joinPool(e);
319 <        }
321 >        joinPool(e);
322      }
323  
324      /**
# Line 325 | 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 345 | 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();
352                Thread.sleep(SHORT_DELAY_MS);
357                  joinPool(e);
358              }};
359  
# Line 387 | Line 391 | public class ExecutorsTest extends JSR16
391          }
392      }
393  
390
394      /**
395       * Without class loader permissions, creating
396       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 410 | 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 426 | 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 498 | 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 543 | Line 546 | public class ExecutorsTest extends JSR16
546          assertSame(one, c.call());
547      }
548  
546
549      /**
550       * callable(null Runnable) throws NPE
551       */
# Line 584 | Line 586 | public class ExecutorsTest extends JSR16
586          } catch (NullPointerException success) {}
587      }
588  
587
589   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines