hotspot/test/runtime/RedefineTests/RedefinePreviousVersions.java
changeset 40927 59f3c8a69541
child 43426 f949081d8be8
equal deleted inserted replaced
40925:04b19236aa98 40927:59f3c8a69541
       
     1 /*
       
     2  * Copyright (c) 2016, 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 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8165246
       
    27  * @summary Test has_previous_versions flag and processing during class unloading.
       
    28  * @library /test/lib
       
    29  * @modules java.base/jdk.internal.misc
       
    30  * @modules java.compiler
       
    31  *          java.instrument
       
    32  *          jdk.jartool/sun.tools.jar
       
    33  * @run main RedefineClassHelper
       
    34  * @run main/othervm RedefinePreviousVersions test
       
    35  */
       
    36 
       
    37 import jdk.test.lib.process.ProcessTools;
       
    38 import jdk.test.lib.process.OutputAnalyzer;
       
    39 
       
    40 public class RedefinePreviousVersions {
       
    41 
       
    42     public static String newB =
       
    43                 "class RedefinePreviousVersions$B {" +
       
    44                 "}";
       
    45 
       
    46     static class B { }
       
    47 
       
    48     public static String newRunning =
       
    49         "class RedefinePreviousVersions$Running {" +
       
    50         "    public static volatile boolean stop = true;" +
       
    51         "    static void localSleep() { }" +
       
    52         "    public static void infinite() { }" +
       
    53         "}";
       
    54 
       
    55     static class Running {
       
    56         public static volatile boolean stop = false;
       
    57         static void localSleep() {
       
    58           try{
       
    59             Thread.currentThread().sleep(10);//sleep for 10 ms
       
    60           } catch(InterruptedException ie) {
       
    61           }
       
    62         }
       
    63 
       
    64         public static void infinite() {
       
    65             while (!stop) { localSleep(); }
       
    66         }
       
    67     }
       
    68 
       
    69     public static void main(String[] args) throws Exception {
       
    70 
       
    71         if (args.length > 0) {
       
    72 
       
    73             String jarFile = System.getProperty("test.src") + "/testcase.jar";
       
    74 
       
    75             // java -javaagent:redefineagent.jar -Xlog:stuff RedefinePreviousVersions
       
    76             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-javaagent:redefineagent.jar",
       
    77                "-Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace",
       
    78                "RedefinePreviousVersions");
       
    79             new OutputAnalyzer(pb.start())
       
    80               .shouldContain("Class unloading: has_previous_versions = false")
       
    81               .shouldContain("Class unloading: has_previous_versions = true")
       
    82               .shouldHaveExitValue(0);
       
    83             return;
       
    84         }
       
    85 
       
    86         // Redefine a class and create some garbage
       
    87         // Since there are no methods running, the previous version is never added to the
       
    88         // previous_version_list and the flag _has_previous_versions should stay false
       
    89         RedefineClassHelper.redefineClass(B.class, newB);
       
    90 
       
    91         for (int i = 0; i < 10 ; i++) {
       
    92             String s = new String("some garbage");
       
    93             System.gc();
       
    94         }
       
    95 
       
    96         // Start a class that has a method running
       
    97         new Thread() {
       
    98             public void run() {
       
    99                 Running.infinite();
       
   100             }
       
   101         }.start();
       
   102 
       
   103         // Since a method of newRunning is running, this class should be added to the previous_version_list
       
   104         // of Running, and _has_previous_versions should return true at class unloading.
       
   105         RedefineClassHelper.redefineClass(Running.class, newRunning);
       
   106 
       
   107         for (int i = 0; i < 10 ; i++) {
       
   108             String s = new String("some garbage");
       
   109             System.gc();
       
   110         }
       
   111 
       
   112         // purge should clean everything up, except Xcomp it might not.
       
   113         Running.stop = true;
       
   114 
       
   115         for (int i = 0; i < 10 ; i++) {
       
   116             String s = new String("some garbage");
       
   117             System.gc();
       
   118         }
       
   119     }
       
   120 }