1 /*
   2  * Copyright (c) 2018, 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 package MyPackage;
  25 
  26 /**
  27  * @test
  28  * @summary Verifies JVMTI support for Continuations
  29  * @compile ContinuationTest.java
  30  * @run main/othervm/native -agentlib:ContinuationTest MyPackage.ContinuationTest
  31  */
  32 
  33 public class ContinuationTest {
  34     private static final String agentLib = "ContinuationTest";
  35     private static final ContinuationScope FOO = new ContinuationScope() {};
  36 
  37     static void log(String str) { System.out.println(str); }
  38     static native void enableEvents(Thread thread);
  39     static native boolean check();
  40 
  41     public static void main(String[] args) throws Exception {
  42         try {
  43             System.loadLibrary(agentLib);
  44         } catch (UnsatisfiedLinkError ex) {
  45             log("Failed to load " + agentLib + " lib");
  46             log("java.library.path: " + System.getProperty("java.library.path"));
  47             throw ex;
  48         }
  49         log("\n######   main: started   #####\n");
  50         enableEvents(Thread.currentThread());
  51 
  52         ContinuationTest obj = new ContinuationTest();
  53         obj.runTest();
  54 
  55         if (!check()) {
  56             throw new RuntimeException("ContinuationTest failed: expected no FramePop events!");
  57         }
  58         log("ContinuationTest passed\n");
  59         log("\n#####   main: finished  #####\n");
  60     }
  61 
  62     public void runTest() {
  63         log("\n####  runTest: started  ####\n");
  64         Continuation cont = new Continuation(FOO, ()-> { 
  65             double dval = 0;
  66 
  67             log("\n##    cont: started     ##\n");
  68             for (int k = 1; k < 3; k++) {
  69                 int ival = 3;
  70                 String str = "abc";
  71 
  72                 log("\n cont: iteration #" + (k - 1));
  73 
  74                 log("\n<<<< runTest: before foo(): " + ival + ", " + str + ", " + dval + " <<<<"); 
  75                 dval += foo(k);
  76                 log(  ">>>> runTest:  after foo(): " + ival + ", " + str + ", " + dval + " >>>>"); 
  77             }
  78             log("\n##    cont: finished    ##\n");
  79         });
  80         int i = 0;
  81         while (!cont.isDone()) {
  82             log("\n##   runTest: iteration #" + (i++));
  83             cont.run();
  84             System.gc();
  85         }
  86         log("\n####  runTest: finished ####\n");
  87     }
  88 
  89     static double foo(int iarg) {
  90         long lval = 8;
  91         String str1 = "yyy";
  92 
  93         log("\n####   foo: started  ####\n");
  94         log("foo: before bar(): " + lval + ", " + str1 + ", " + iarg);
  95         String str2 = bar(iarg + 1);
  96         log("foo:  after bar(): " + lval + ", " + str1 + ", " + str2); 
  97 
  98         log("\n####   foo: finished ####\n");
  99         return Integer.parseInt(str2) + 1;
 100     }
 101 
 102     static String bar(long larg) {
 103         double dval = 9.99;
 104         String str = "zzz";
 105 
 106         log("\n####   bar: started  ####\n");
 107         log("bar: before yield(): " + dval + ", " + str + ", " + larg); 
 108         Continuation.yield(FOO);
 109 
 110         long lval = larg + 1;
 111         log("bar:  after yield(): " + dval + ", " + str + ", " + lval); 
 112 
 113         log("\n####   bar: finished ####\n");
 114         return "" + lval;
 115     }
 116 }