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.35 by jsr166, Mon Oct 11 08:47:04 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       */
# Line 191 | Line 185 | public class ExecutorsTest extends JSR16
185              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
186                                    SHORT_DELAY_MS, MILLISECONDS);
187              assertFalse(f.isDone());
188 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
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());
196            assertEquals(Boolean.TRUE, f.get());
192          } finally {
193              joinPool(p);
194          }
# Line 213 | Line 208 | public class ExecutorsTest extends JSR16
208              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
209                                    SHORT_DELAY_MS, MILLISECONDS);
210              assertFalse(f.isDone());
211 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
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());
218            assertEquals(Boolean.TRUE, f.get());
215          } finally {
216              joinPool(p);
217          }
# Line 237 | Line 233 | public class ExecutorsTest extends JSR16
233              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
234                                    SHORT_DELAY_MS, MILLISECONDS);
235              assertFalse(f.isDone());
236 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
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());
242            assertEquals(Boolean.TRUE, f.get());
240          } finally {
241              joinPool(p);
242          }
# Line 249 | Line 246 | public class ExecutorsTest extends JSR16
246       * Future.get on submitted tasks will time out if they compute too long.
247       */
248      public void testTimedCallable() throws Exception {
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 <                Thread.sleep(LONG_DELAY_MS);
258 >                delay(LONG_DELAY_MS);
259              }};
260 <        for (ExecutorService executor :
261 <                 new ExecutorService[] {
262 <                     Executors.newSingleThreadExecutor(),
263 <                     Executors.newCachedThreadPool(),
264 <                     Executors.newFixedThreadPool(2),
265 <                     Executors.newScheduledThreadPool(2),
266 <                 }) {
267 <            try {
268 <                Future future = executor.submit(sleeper);
265 <                try {
266 <                    future.get(SHORT_DELAY_MS, MILLISECONDS);
267 <                    shouldThrow();
268 <                } catch (TimeoutException success) {
269 <                } finally {
270 <                    future.cancel(true);
271 <                }
272 <            }
273 <            finally {
274 <                joinPool(executor);
275 <            }
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  
279
276      /**
277       * ThreadPoolExecutor using defaultThreadFactory has
278       * specified group, priority, daemon status, and name
# Line 310 | 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 346 | Line 342 | public class ExecutorsTest extends JSR16
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 384 | Line 380 | public class ExecutorsTest extends JSR16
380          }
381      }
382  
387
383      /**
384       * Without class loader permissions, creating
385       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 501 | 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 540 | Line 535 | public class ExecutorsTest extends JSR16
535          assertSame(one, c.call());
536      }
537  
543
538      /**
539       * callable(null Runnable) throws NPE
540       */
# Line 581 | Line 575 | public class ExecutorsTest extends JSR16
575          } catch (NullPointerException success) {}
576      }
577  
584
578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines