ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool9Test.java
Revision: 1.9
Committed: Tue Aug 13 23:06:39 2019 UTC (4 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +0 -1 lines
Log Message:
fix imports

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.7 import java.util.stream.Stream;
15 jsr166 1.1
16     import junit.framework.Test;
17     import junit.framework.TestSuite;
18    
19     public class ForkJoinPool9Test extends JSR166TestCase {
20     public static void main(String[] args) {
21     main(suite(), args);
22     }
23    
24     public static Test suite() {
25     return new TestSuite(ForkJoinPool9Test.class);
26     }
27    
28     /**
29     * Check handling of common pool thread context class loader
30     */
31     public void testCommonPoolThreadContextClassLoader() throws Throwable {
32     if (!testImplementationDetails) return;
33 jsr166 1.3
34     // Ensure common pool has at least one real thread
35     String prop = System.getProperty(
36     "java.util.concurrent.ForkJoinPool.common.parallelism");
37     if ("0".equals(prop)) return;
38    
39 jsr166 1.1 VarHandle CCL =
40     MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
41     .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
42     ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
43 jsr166 1.2 boolean haveSecurityManager = (System.getSecurityManager() != null);
44 jsr166 1.7 CountDownLatch runInCommonPoolStarted = new CountDownLatch(1);
45 jsr166 1.6 ClassLoader classLoaderDistinctFromSystemClassLoader
46     = ClassLoader.getPlatformClassLoader();
47     assertNotSame(classLoaderDistinctFromSystemClassLoader,
48     systemClassLoader);
49 jsr166 1.3 Runnable runInCommonPool = () -> {
50 jsr166 1.7 runInCommonPoolStarted.countDown();
51 jsr166 1.3 assertTrue(ForkJoinTask.inForkJoinPool());
52 jsr166 1.7 assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool());
53     Thread currentThread = Thread.currentThread();
54    
55     Stream.of(systemClassLoader, null).forEach(cl -> {
56 jsr166 1.8 if (randomBoolean())
57 jsr166 1.7 // should always be permitted, without effect
58     currentThread.setContextClassLoader(cl);
59     });
60    
61     Stream.of(currentThread.getContextClassLoader(),
62     (ClassLoader) CCL.get(currentThread))
63     .forEach(cl -> assertTrue(cl == systemClassLoader || cl == null));
64    
65 jsr166 1.3 if (haveSecurityManager)
66     assertThrows(
67     SecurityException.class,
68     () -> System.getProperty("foo"),
69 jsr166 1.7 () -> currentThread.setContextClassLoader(
70 jsr166 1.6 classLoaderDistinctFromSystemClassLoader));
71 jsr166 1.3 // TODO ?
72     // if (haveSecurityManager
73     // && Thread.currentThread().getClass().getSimpleName()
74     // .equals("InnocuousForkJoinWorkerThread"))
75     // assertThrows(SecurityException.class, /* ?? */);
76     };
77     Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool);
78     // Ensure runInCommonPool is truly running in the common pool,
79     // by giving this thread no opportunity to "help" on get().
80 jsr166 1.7 await(runInCommonPoolStarted);
81 jsr166 1.3 assertNull(f.get());
82 jsr166 1.1 }
83    
84     }