jdk/test/java/util/concurrent/tck/ForkJoinPool9Test.java
author dl
Tue, 14 Mar 2017 07:04:50 -0700
changeset 44251 321026ad5f1a
parent 43545 fdecd1d14d53
child 44589 64d9270bd24c
permissions -rw-r--r--
8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException" Reviewed-by: martin, chegar, dholmes, amlu
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
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    35
import static java.util.concurrent.TimeUnit.MILLISECONDS;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    36
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    37
import java.lang.invoke.MethodHandles;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    38
import java.lang.invoke.VarHandle;
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    39
import java.util.concurrent.CountDownLatch;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    40
import java.util.concurrent.ForkJoinPool;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    41
import java.util.concurrent.ForkJoinTask;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    42
import java.util.concurrent.Future;
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    43
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    44
import junit.framework.Test;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    45
import junit.framework.TestSuite;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    46
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    47
public class ForkJoinPool9Test extends JSR166TestCase {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    48
    public static void main(String[] args) {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    49
        main(suite(), args);
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
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    52
    public static Test suite() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    53
        return new TestSuite(ForkJoinPool9Test.class);
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
    /**
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    57
     * 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
    58
     */
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    59
    public void testCommonPoolThreadContextClassLoader() throws Throwable {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    60
        if (!testImplementationDetails) return;
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    61
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    62
        // Ensure common pool has at least one real thread
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    63
        String prop = System.getProperty(
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    64
            "java.util.concurrent.ForkJoinPool.common.parallelism");
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    65
        if ("0".equals(prop)) return;
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    66
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    67
        VarHandle CCL =
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    68
            MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    69
            .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    70
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    71
        boolean haveSecurityManager = (System.getSecurityManager() != null);
44251
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    72
        CountDownLatch taskStarted = new CountDownLatch(1);
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    73
        Runnable runInCommonPool = () -> {
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    74
            taskStarted.countDown();
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    75
            assertTrue(ForkJoinTask.inForkJoinPool());
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    76
            assertSame(ForkJoinPool.commonPool(),
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    77
                       ForkJoinTask.getPool());
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    78
            assertSame(systemClassLoader,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    79
                       Thread.currentThread().getContextClassLoader());
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    80
            assertSame(systemClassLoader,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    81
                       CCL.get(Thread.currentThread()));
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    82
            if (haveSecurityManager)
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    83
                assertThrows(
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    84
                    SecurityException.class,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    85
                    () -> System.getProperty("foo"),
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    86
                    () -> Thread.currentThread().setContextClassLoader(null));
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    87
            // TODO ?
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    88
//          if (haveSecurityManager
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    89
//              && Thread.currentThread().getClass().getSimpleName()
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    90
//                 .equals("InnocuousForkJoinWorkerThread"))
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    91
//              assertThrows(SecurityException.class, /* ?? */);
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    92
        };
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    93
        Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool);
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    94
        // Ensure runInCommonPool is truly running in the common pool,
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    95
        // by giving this thread no opportunity to "help" on get().
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    96
        assertTrue(taskStarted.await(LONG_DELAY_MS, MILLISECONDS));
321026ad5f1a 8176551: testCommonPoolThreadContextClassLoader fails with "Should throw SecurityException"
dl
parents: 43545
diff changeset
    97
        assertNull(f.get());
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    98
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
    99
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
diff changeset
   100
}