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.41 by jsr166, Sun May 29 06:54:23 2011 UTC vs.
Revision 1.44 by jsr166, Sun Feb 22 19:16:12 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.security.*;
10 >
11 > import java.security.AccessControlContext;
12 > import java.security.AccessControlException;
13 > import java.security.AccessController;
14 > import java.security.PrivilegedAction;
15 > import java.security.PrivilegedExceptionAction;
16 > import java.util.ArrayList;
17 > import java.util.List;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.ScheduledExecutorService;
24 > import java.util.concurrent.ThreadPoolExecutor;
25 >
26 > import junit.framework.Test;
27 > import junit.framework.TestSuite;
28  
29   public class ExecutorsTest extends JSR166TestCase {
30      public static void main(String[] args) {
# Line 143 | Line 157 | public class ExecutorsTest extends JSR16
157      /**
158       * An unconfigurable newFixedThreadPool can execute runnables
159       */
160 <    public void testunconfigurableExecutorService() {
160 >    public void testUnconfigurableExecutorService() {
161          ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
162          e.execute(new NoOpRunnable());
163          e.execute(new NoOpRunnable());
# Line 154 | Line 168 | public class ExecutorsTest extends JSR16
168      /**
169       * unconfigurableExecutorService(null) throws NPE
170       */
171 <    public void testunconfigurableExecutorServiceNPE() {
171 >    public void testUnconfigurableExecutorServiceNPE() {
172          try {
173              ExecutorService e = Executors.unconfigurableExecutorService(null);
174              shouldThrow();
# Line 164 | Line 178 | public class ExecutorsTest extends JSR16
178      /**
179       * unconfigurableScheduledExecutorService(null) throws NPE
180       */
181 <    public void testunconfigurableScheduledExecutorServiceNPE() {
181 >    public void testUnconfigurableScheduledExecutorServiceNPE() {
182          try {
183              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
184              shouldThrow();
# Line 177 | Line 191 | public class ExecutorsTest extends JSR16
191      public void testNewSingleThreadScheduledExecutor() throws Exception {
192          ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
193          try {
194 <            final CountDownLatch done = new CountDownLatch(1);
194 >            final CountDownLatch proceed = new CountDownLatch(1);
195              final Runnable task = new CheckedRunnable() {
196                  public void realRun() {
197 <                    done.countDown();
197 >                    await(proceed);
198                  }};
199 +            long startTime = System.nanoTime();
200              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
201 <                                  SHORT_DELAY_MS, MILLISECONDS);
201 >                                  timeoutMillis(), MILLISECONDS);
202              assertFalse(f.isDone());
203 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
204 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
203 >            proceed.countDown();
204 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
205              assertSame(Boolean.TRUE, f.get());
206              assertTrue(f.isDone());
207 +            assertFalse(f.isCancelled());
208 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
209          } finally {
210              joinPool(p);
211          }
# Line 197 | Line 214 | public class ExecutorsTest extends JSR16
214      /**
215       * a newScheduledThreadPool successfully runs delayed task
216       */
217 <    public void testnewScheduledThreadPool() throws Exception {
217 >    public void testNewScheduledThreadPool() throws Exception {
218          ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
219          try {
220 <            final CountDownLatch done = new CountDownLatch(1);
220 >            final CountDownLatch proceed = new CountDownLatch(1);
221              final Runnable task = new CheckedRunnable() {
222                  public void realRun() {
223 <                    done.countDown();
223 >                    await(proceed);
224                  }};
225 +            long startTime = System.nanoTime();
226              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
227 <                                  SHORT_DELAY_MS, MILLISECONDS);
227 >                                  timeoutMillis(), MILLISECONDS);
228              assertFalse(f.isDone());
229 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
230 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
229 >            proceed.countDown();
230 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
231              assertSame(Boolean.TRUE, f.get());
232              assertTrue(f.isDone());
233 +            assertFalse(f.isCancelled());
234 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
235          } finally {
236              joinPool(p);
237          }
# Line 220 | Line 240 | public class ExecutorsTest extends JSR16
240      /**
241       * an unconfigurable newScheduledThreadPool successfully runs delayed task
242       */
243 <    public void testunconfigurableScheduledExecutorService() throws Exception {
243 >    public void testUnconfigurableScheduledExecutorService() throws Exception {
244          ScheduledExecutorService p =
245              Executors.unconfigurableScheduledExecutorService
246              (Executors.newScheduledThreadPool(2));
247          try {
248 <            final CountDownLatch done = new CountDownLatch(1);
248 >            final CountDownLatch proceed = new CountDownLatch(1);
249              final Runnable task = new CheckedRunnable() {
250                  public void realRun() {
251 <                    done.countDown();
251 >                    await(proceed);
252                  }};
253 +            long startTime = System.nanoTime();
254              Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
255 <                                  SHORT_DELAY_MS, MILLISECONDS);
255 >                                  timeoutMillis(), MILLISECONDS);
256              assertFalse(f.isDone());
257 <            assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS));
258 <            assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS));
257 >            proceed.countDown();
258 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
259              assertSame(Boolean.TRUE, f.get());
260              assertTrue(f.isDone());
261 +            assertFalse(f.isCancelled());
262 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
263          } finally {
264              joinPool(p);
265          }
# Line 262 | Line 285 | public class ExecutorsTest extends JSR16
285          for (final ExecutorService executor : executors) {
286              threads.add(newStartedThread(new CheckedRunnable() {
287                  public void realRun() {
265                    long startTime = System.nanoTime();
288                      Future future = executor.submit(sleeper);
289                      assertFutureTimesOut(future);
290                  }}));
# Line 279 | Line 301 | public class ExecutorsTest extends JSR16
301       */
302      public void testDefaultThreadFactory() throws Exception {
303          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
304 +        final CountDownLatch done = new CountDownLatch(1);
305          Runnable r = new CheckedRunnable() {
306              public void realRun() {
307                  try {
# Line 296 | Line 319 | public class ExecutorsTest extends JSR16
319                  } catch (SecurityException ok) {
320                      // Also pass if not allowed to change setting
321                  }
322 +                done.countDown();
323              }};
324          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
325  
326          e.execute(r);
327 +        await(done);
328 +
329          try {
330              e.shutdown();
331          } catch (SecurityException ok) {
332          }
333  
334 <        try {
309 <            delay(SHORT_DELAY_MS);
310 <        } finally {
311 <            joinPool(e);
312 <        }
334 >        joinPool(e);
335      }
336  
337      /**
# Line 318 | Line 340 | public class ExecutorsTest extends JSR16
340       * access control context and context class loader
341       */
342      public void testPrivilegedThreadFactory() throws Exception {
343 +        final CountDownLatch done = new CountDownLatch(1);
344          Runnable r = new CheckedRunnable() {
345              public void realRun() throws Exception {
346                  final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
# Line 338 | Line 361 | public class ExecutorsTest extends JSR16
361                          assertTrue(name.endsWith("thread-1"));
362                          assertSame(thisccl, current.getContextClassLoader());
363                          assertEquals(thisacc, AccessController.getContext());
364 +                        done.countDown();
365                      }};
366                  ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
367                  e.execute(r);
368 +                await(done);
369                  e.shutdown();
345                delay(SHORT_DELAY_MS);
370                  joinPool(e);
371              }};
372  
# Line 402 | Line 426 | public class ExecutorsTest extends JSR16
426       * With class loader permissions, calling
427       * privilegedCallableUsingCurrentClassLoader does not throw ACE
428       */
429 <    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
429 >    public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception {
430          Runnable r = new CheckedRunnable() {
431              public void realRun() throws Exception {
432                  Executors.privilegedCallableUsingCurrentClassLoader
# Line 418 | Line 442 | public class ExecutorsTest extends JSR16
442      /**
443       * Without permissions, calling privilegedCallable throws ACE
444       */
445 <    public void testprivilegedCallableWithNoPrivs() throws Exception {
445 >    public void testPrivilegedCallableWithNoPrivs() throws Exception {
446          // Avoid classloader-related SecurityExceptions in swingui.TestRunner
447          Executors.privilegedCallable(new CheckCCL());
448  
# Line 490 | Line 514 | public class ExecutorsTest extends JSR16
514      /**
515       * With permissions, calling privilegedCallable succeeds
516       */
517 <    public void testprivilegedCallableWithPrivs() throws Exception {
517 >    public void testPrivilegedCallableWithPrivs() throws Exception {
518          Runnable r = new CheckedRunnable() {
519              public void realRun() throws Exception {
520                  Executors.privilegedCallable(new CheckCCL()).call();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines