hotspot/test/runtime/modules/ModuleStress/src/jdk.test/test/Main.java
author alanb
Fri, 10 Feb 2017 09:03:55 +0000
changeset 43665 4bb003cad9b9
parent 39290 0cc9f5028562
child 44520 0553e129e0ec
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: lfoltan, acorn, mchung Contributed-by: alan.bateman@oracle.com, george.triantafillou@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39290
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     1
/*
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     4
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     8
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    13
 * accompanied this code).
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    14
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    18
 *
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    21
 * questions.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    22
 */
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    23
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    24
package test;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    25
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    26
import java.lang.module.Configuration;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    27
import java.lang.module.ModuleFinder;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    28
import java.lang.reflect.Layer;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    29
import java.lang.reflect.Method;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    30
import java.lang.reflect.Module;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    31
import java.nio.file.Path;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    32
import java.nio.file.Paths;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    33
import java.util.*;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    34
import java.util.concurrent.Callable;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    35
import java.util.concurrent.ExecutorService;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    36
import java.util.concurrent.Executors;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    37
import java.util.concurrent.Future;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    38
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    39
public class Main {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    40
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    41
    private static final Path MODS_DIR = Paths.get(System.getProperty("jdk.module.path"));
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    42
    static final String MODULE_NAME = "jdk.translet";
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    43
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    45
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    46
        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    47
        Layer layerBoot = Layer.boot();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    48
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    49
        Configuration cf = layerBoot
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    50
                .configuration()
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 39290
diff changeset
    51
                .resolve(ModuleFinder.of(), finder, Set.of(MODULE_NAME));
39290
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    52
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    53
        Module testModule = Main.class.getModule();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    54
        ClassLoader scl = ClassLoader.getSystemClassLoader();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    55
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    56
        // Create an unique module/class loader in a layer above the boot layer.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    57
        // Export this module to the jdk.test/test package.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    58
        Callable<Void> task = new Callable<Void>() {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    59
            @Override
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    60
            public Void call() throws Exception {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    61
                Layer layer = Layer.boot().defineModulesWithOneLoader(cf, scl);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    62
                Module transletModule = layer.findModule(MODULE_NAME).get();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    63
                testModule.addExports("test", transletModule);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    64
                Class<?> c = layer.findLoader(MODULE_NAME).loadClass("translet.Main");
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    65
                Method method = c.getDeclaredMethod("go");
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    66
                method.invoke(null);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    67
                return null;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    68
            }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    69
        };
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    70
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    71
        List<Future<Void>> results = new ArrayList<>();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    72
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    73
        // Repeatedly create the layer above stressing the exportation of
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    74
        // package jdk.test/test to several different modules.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    75
        ExecutorService pool = Executors.newFixedThreadPool(Math.min(100, Runtime.getRuntime().availableProcessors()*10));
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    76
        try {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    77
            for (int i = 0; i < 10000; i++) {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    78
                results.add(pool.submit(task));
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    79
            }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    80
        } finally {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    81
            pool.shutdown();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    82
        }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    83
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    84
        int passed = 0;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    85
        int failed = 0;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    86
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    87
        // The failed state should be 0, the created modules in layers above the
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    88
        // boot layer should be allowed access to the contents of the jdk.test/test
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    89
        // package since that package was exported to the transletModule above.
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    90
        for (Future<Void> result : results) {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    91
            try {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    92
                result.get();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    93
                passed++;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    94
            } catch (Throwable x) {
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    95
                x.printStackTrace();
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    96
                failed++;
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    97
            }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    98
        }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
    99
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   100
        System.out.println("passed: " + passed);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   101
        System.out.println("failed: " + failed);
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   102
    }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   103
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   104
    public static void callback() { }
0cc9f5028562 8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
diff changeset
   105
}