ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool9Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPool9Test.java (file contents):
Revision 1.1 by jsr166, Sun Jan 29 20:19:00 2017 UTC vs.
Revision 1.6 by jsr166, Fri Dec 14 02:48:21 2018 UTC

# Line 7 | Line 7
7  
8   import java.lang.invoke.MethodHandles;
9   import java.lang.invoke.VarHandle;
10 < import java.util.concurrent.CompletableFuture;
10 > import java.util.concurrent.CountDownLatch;
11 > import java.util.concurrent.ForkJoinPool;
12 > import java.util.concurrent.ForkJoinTask;
13 > import java.util.concurrent.Future;
14  
15   import junit.framework.Test;
16   import junit.framework.TestSuite;
# Line 26 | Line 29 | public class ForkJoinPool9Test extends J
29       */
30      public void testCommonPoolThreadContextClassLoader() throws Throwable {
31          if (!testImplementationDetails) return;
32 +
33 +        // Ensure common pool has at least one real thread
34 +        String prop = System.getProperty(
35 +            "java.util.concurrent.ForkJoinPool.common.parallelism");
36 +        if ("0".equals(prop)) return;
37 +
38          VarHandle CCL =
39              MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
40              .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
41          ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
42 <        //if (System.getSecurityManager() == null) return;
43 <        CompletableFuture.runAsync(
44 <            () -> {
45 <                assertSame(systemClassLoader,
46 <                           Thread.currentThread().getContextClassLoader());
47 <                assertSame(systemClassLoader,
48 <                           CCL.get(Thread.currentThread()));
49 <            }).join();
42 >        boolean haveSecurityManager = (System.getSecurityManager() != null);
43 >        CountDownLatch taskStarted = new CountDownLatch(1);
44 >        ClassLoader classLoaderDistinctFromSystemClassLoader
45 >            = ClassLoader.getPlatformClassLoader();
46 >        assertNotSame(classLoaderDistinctFromSystemClassLoader,
47 >                      systemClassLoader);
48 >        Runnable runInCommonPool = () -> {
49 >            taskStarted.countDown();
50 >            assertTrue(ForkJoinTask.inForkJoinPool());
51 >            assertSame(ForkJoinPool.commonPool(),
52 >                       ForkJoinTask.getPool());
53 >            assertSame(systemClassLoader,
54 >                       Thread.currentThread().getContextClassLoader());
55 >            assertSame(systemClassLoader,
56 >                       CCL.get(Thread.currentThread()));
57 >            if (haveSecurityManager)
58 >                assertThrows(
59 >                    SecurityException.class,
60 >                    () -> System.getProperty("foo"),
61 >                    () -> Thread.currentThread().setContextClassLoader(
62 >                        classLoaderDistinctFromSystemClassLoader));
63 >            // TODO ?
64 > //          if (haveSecurityManager
65 > //              && Thread.currentThread().getClass().getSimpleName()
66 > //                 .equals("InnocuousForkJoinWorkerThread"))
67 > //              assertThrows(SecurityException.class, /* ?? */);
68 >        };
69 >        Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool);
70 >        // Ensure runInCommonPool is truly running in the common pool,
71 >        // by giving this thread no opportunity to "help" on get().
72 >        await(taskStarted);
73 >        assertNull(f.get());
74      }
75  
76   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines