MainGC.java revision 12723:afedee84773e
1/*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package test;
25
26import java.lang.module.Configuration;
27import java.lang.module.ModuleFinder;
28import java.lang.reflect.Method;
29import java.nio.file.Path;
30import java.nio.file.Paths;
31import java.util.*;
32import java.util.concurrent.Callable;
33import java.util.concurrent.ExecutorService;
34import java.util.concurrent.Executors;
35import java.util.concurrent.Future;
36
37public class MainGC {
38
39    private static final Path MODS_DIR = Paths.get(System.getProperty("jdk.module.path"));
40    static final String MODULE_NAME = "jdk.translet";
41
42    public static void main(String[] args) throws Exception {
43
44        ModuleFinder finder = ModuleFinder.of(MODS_DIR);
45        ModuleLayer layerBoot = ModuleLayer.boot();
46
47        Configuration cf = layerBoot
48                .configuration()
49                .resolve(ModuleFinder.of(), finder, Set.of(MODULE_NAME));
50
51        Module testModule = MainGC.class.getModule();
52        ClassLoader scl = ClassLoader.getSystemClassLoader();
53
54        // Create an unique module/class loader in a layer above the boot layer.
55        // Export this module to the jdk.test/test package.
56        // Add a read edge from module jdk.test to this module.
57        Callable<Void> task = new Callable<Void>() {
58            @Override
59            public Void call() throws Exception {
60                ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
61                Module transletModule = layer.findModule(MODULE_NAME).get();
62                testModule.addExports("test", transletModule);
63                testModule.addReads(transletModule);
64                Class<?> c = layer.findLoader(MODULE_NAME).loadClass("translet.MainGC");
65                Method method = c.getDeclaredMethod("go");
66                method.invoke(null);
67                return null;
68            }
69        };
70
71        List<Future<Void>> results = new ArrayList<>();
72
73        // Repeatedly create the layer above stressing the exportation of
74        // package jdk.test/test to several different modules.
75        ExecutorService pool = Executors.newFixedThreadPool(Math.min(100, Runtime.getRuntime().availableProcessors()*10));
76        try {
77            for (int i = 0; i < 10000; i++) {
78                results.add(pool.submit(task));
79                // At specified intervals, force a GC. This provides an
80                // opportunity to verify that both the module jdk.test's reads
81                // and the package test's, which is defined to jdk.test, exports
82                // lists are being walked.
83                if (i == 3000 || i == 6000 || i == 9000) {
84                    System.gc();
85                }
86            }
87        } finally {
88            pool.shutdown();
89        }
90
91        int passed = 0;
92        int failed = 0;
93
94        // The failed state should be 0, the created modules in layers above the
95        // boot layer should be allowed access to the contents of the jdk.test/test
96        // package since that package was exported to the transletModule above.
97        for (Future<Void> result : results) {
98            try {
99                result.get();
100                passed++;
101            } catch (Throwable x) {
102                x.printStackTrace();
103                failed++;
104            }
105        }
106
107        System.out.println("passed: " + passed);
108        System.out.println("failed: " + failed);
109    }
110
111    public static void callback() { }
112}
113