test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 53779 709631caffa3
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     1
/*
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     3
 *
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     7
 *
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    12
 * accompanied this code).
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    13
 *
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    17
 *
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    20
 * questions.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    21
 */
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    22
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    23
/*
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    27
 * file:
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    28
 *
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    29
 * Written by Doug Lea and Martin Buchholz with assistance from
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    31
 * domain, as explained at
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    33
 */
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    34
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    35
import java.lang.invoke.MethodHandles;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    36
import java.lang.invoke.VarHandle;
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    37
import java.util.concurrent.CountDownLatch;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    38
import java.util.concurrent.ForkJoinPool;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    39
import java.util.concurrent.ForkJoinTask;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    40
import java.util.concurrent.Future;
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    41
import java.util.stream.Stream;
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    42
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    43
import junit.framework.Test;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    44
import junit.framework.TestSuite;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    45
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    46
public class ForkJoinPool9Test extends JSR166TestCase {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    47
    public static void main(String[] args) {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    48
        main(suite(), args);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    49
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    50
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    51
    public static Test suite() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    52
        return new TestSuite(ForkJoinPool9Test.class);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    53
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    54
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    55
    /**
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    56
     * Check handling of common pool thread context class loader
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    57
     */
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    58
    public void testCommonPoolThreadContextClassLoader() throws Throwable {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    59
        if (!testImplementationDetails) return;
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    60
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    61
        // Ensure common pool has at least one real thread
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    62
        String prop = System.getProperty(
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    63
            "java.util.concurrent.ForkJoinPool.common.parallelism");
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    64
        if ("0".equals(prop)) return;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    65
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    66
        VarHandle CCL =
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    67
            MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    68
            .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    69
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    70
        boolean haveSecurityManager = (System.getSecurityManager() != null);
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    71
        CountDownLatch runInCommonPoolStarted = new CountDownLatch(1);
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    72
        ClassLoader classLoaderDistinctFromSystemClassLoader
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    73
            = ClassLoader.getPlatformClassLoader();
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    74
        assertNotSame(classLoaderDistinctFromSystemClassLoader,
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    75
                      systemClassLoader);
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    76
        Runnable runInCommonPool = () -> {
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    77
            runInCommonPoolStarted.countDown();
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    78
            assertTrue(ForkJoinTask.inForkJoinPool());
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    79
            assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool());
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    80
            Thread currentThread = Thread.currentThread();
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    81
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    82
            Stream.of(systemClassLoader, null).forEach(cl -> {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 53779
diff changeset
    83
                if (randomBoolean())
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    84
                    // should always be permitted, without effect
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    85
                    currentThread.setContextClassLoader(cl);
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    86
                });
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    87
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    88
            Stream.of(currentThread.getContextClassLoader(),
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    89
                      (ClassLoader) CCL.get(currentThread))
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    90
            .forEach(cl -> assertTrue(cl == systemClassLoader || cl == null));
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    91
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    92
            if (haveSecurityManager)
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    93
                assertThrows(
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    94
                    SecurityException.class,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    95
                    () -> System.getProperty("foo"),
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    96
                    () -> currentThread.setContextClassLoader(
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
    97
                        classLoaderDistinctFromSystemClassLoader));
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    98
            // TODO ?
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    99
//          if (haveSecurityManager
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   100
//              && Thread.currentThread().getClass().getSimpleName()
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   101
//                 .equals("InnocuousForkJoinWorkerThread"))
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   102
//              assertThrows(SecurityException.class, /* ?? */);
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   103
        };
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   104
        Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool);
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   105
        // Ensure runInCommonPool is truly running in the common pool,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   106
        // by giving this thread no opportunity to "help" on get().
53779
709631caffa3 8215359: InnocuousForkJoinWorkerThread.setContextClassLoader needlessly throws
dl
parents: 47216
diff changeset
   107
        await(runInCommonPoolStarted);
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
   108
        assertNull(f.get());
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
   109
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
   110
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
   111
}