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.9 by jsr166, Tue Aug 13 23:06:39 2019 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 > import java.util.stream.Stream;
15  
16   import junit.framework.Test;
17   import junit.framework.TestSuite;
# Line 26 | Line 30 | public class ForkJoinPool9Test extends J
30       */
31      public void testCommonPoolThreadContextClassLoader() throws Throwable {
32          if (!testImplementationDetails) return;
33 +
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          VarHandle CCL =
40              MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
41              .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
42          ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
43 <        //if (System.getSecurityManager() == null) return;
44 <        CompletableFuture.runAsync(
45 <            () -> {
46 <                assertSame(systemClassLoader,
47 <                           Thread.currentThread().getContextClassLoader());
48 <                assertSame(systemClassLoader,
49 <                           CCL.get(Thread.currentThread()));
50 <            }).join();
43 >        boolean haveSecurityManager = (System.getSecurityManager() != null);
44 >        CountDownLatch runInCommonPoolStarted = new CountDownLatch(1);
45 >        ClassLoader classLoaderDistinctFromSystemClassLoader
46 >            = ClassLoader.getPlatformClassLoader();
47 >        assertNotSame(classLoaderDistinctFromSystemClassLoader,
48 >                      systemClassLoader);
49 >        Runnable runInCommonPool = () -> {
50 >            runInCommonPoolStarted.countDown();
51 >            assertTrue(ForkJoinTask.inForkJoinPool());
52 >            assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool());
53 >            Thread currentThread = Thread.currentThread();
54 >
55 >            Stream.of(systemClassLoader, null).forEach(cl -> {
56 >                if (randomBoolean())
57 >                    // 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 >            if (haveSecurityManager)
66 >                assertThrows(
67 >                    SecurityException.class,
68 >                    () -> System.getProperty("foo"),
69 >                    () -> currentThread.setContextClassLoader(
70 >                        classLoaderDistinctFromSystemClassLoader));
71 >            // 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 >        await(runInCommonPoolStarted);
81 >        assertNull(f.get());
82      }
83  
84   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines