ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool9Test.java
Revision: 1.6
Committed: Fri Dec 14 02:48:21 2018 UTC (5 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +6 -1 lines
Log Message:
8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Doug Lea and Martin Buchholz with assistance from
3     * members of JCP JSR-166 Expert Group and released to the public
4     * domain, as explained at
5     * http://creativecommons.org/publicdomain/zero/1.0/
6     */
7    
8     import java.lang.invoke.MethodHandles;
9     import java.lang.invoke.VarHandle;
10 jsr166 1.3 import java.util.concurrent.CountDownLatch;
11     import java.util.concurrent.ForkJoinPool;
12     import java.util.concurrent.ForkJoinTask;
13     import java.util.concurrent.Future;
14 jsr166 1.1
15     import junit.framework.Test;
16     import junit.framework.TestSuite;
17    
18     public class ForkJoinPool9Test extends JSR166TestCase {
19     public static void main(String[] args) {
20     main(suite(), args);
21     }
22    
23     public static Test suite() {
24     return new TestSuite(ForkJoinPool9Test.class);
25     }
26    
27     /**
28     * Check handling of common pool thread context class loader
29     */
30     public void testCommonPoolThreadContextClassLoader() throws Throwable {
31     if (!testImplementationDetails) return;
32 jsr166 1.3
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 jsr166 1.1 VarHandle CCL =
39     MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
40     .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
41     ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
42 jsr166 1.2 boolean haveSecurityManager = (System.getSecurityManager() != null);
43 jsr166 1.3 CountDownLatch taskStarted = new CountDownLatch(1);
44 jsr166 1.6 ClassLoader classLoaderDistinctFromSystemClassLoader
45     = ClassLoader.getPlatformClassLoader();
46     assertNotSame(classLoaderDistinctFromSystemClassLoader,
47     systemClassLoader);
48 jsr166 1.3 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 jsr166 1.6 () -> Thread.currentThread().setContextClassLoader(
62     classLoaderDistinctFromSystemClassLoader));
63 jsr166 1.3 // 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 jsr166 1.4 await(taskStarted);
73 jsr166 1.3 assertNull(f.get());
74 jsr166 1.1 }
75    
76     }