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.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.
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);
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 <            }
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  
253
276      /**
277       * ThreadPoolExecutor using defaultThreadFactory has
278       * specified group, priority, daemon status, and name
# Line 284 | 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 320 | 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 358 | Line 380 | public class ExecutorsTest extends JSR16
380          }
381      }
382  
361
383      /**
384       * Without class loader permissions, creating
385       * privilegedCallableUsingCurrentClassLoader throws ACE
# Line 475 | 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 514 | Line 535 | public class ExecutorsTest extends JSR16
535          assertSame(one, c.call());
536      }
537  
517
538      /**
539       * callable(null Runnable) throws NPE
540       */
# Line 555 | Line 575 | public class ExecutorsTest extends JSR16
575          } catch (NullPointerException success) {}
576      }
577  
558
578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines