ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool9Test.java
Revision: 1.8
Committed: Tue Aug 13 00:54:51 2019 UTC (4 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
use randomBoolean()

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