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 setting a breakpoint in Continuation.yield() followed by single stepping
  29  * @compile ContYieldBreakPointTest.java
  30  * @run main/othervm/native -agentlib:ContYieldBreakPointTest MyPackage.ContYieldBreakPointTest
  31  */
  32 
  33 public class ContYieldBreakPointTest {
  34     private static final String agentLib = "ContYieldBreakPointTest";
  35     private static ContinuationScope scope = new ContinuationScope("YieldTest") {};
  36     private static Continuation cont;
  37     private static boolean done = false;
  38 
  39     static void log(String str) { System.out.println(str); }
  40 
  41     static native void enableEvents(Thread thread, Class breakpointClass);
  42     static native boolean check();
  43 
  44     public static void main(String[] args) throws Exception {
  45         try {
  46             System.loadLibrary(agentLib);
  47         } catch (UnsatisfiedLinkError ex) {
  48             log("Failed to load " + agentLib + " lib");
  49             log("java.library.path: " + System.getProperty("java.library.path"));
  50             throw ex;
  51         }
  52         log("\n######   main: started   #####\n");
  53 
  54         ContYieldBreakPointTest obj = new ContYieldBreakPointTest();
  55         obj.runTest();
  56 
  57         log("ContYieldBreakPointTest passed\n");
  58         log("\n#####   main: finished  #####\n");
  59     }
  60 
  61     public void runTest() {
  62         log("\n####  runTest: started  ####\n");
  63         yieldTest();
  64         log("\n####  runTest: finished ####\n");
  65     }
  66 
  67     static final Runnable YEILD = () -> {
  68         while (true) {
  69             if (done) return;
  70             Continuation.yield(scope);
  71         }
  72     };
  73 
  74     public static void yieldTest() {
  75         cont = new Continuation(scope, YEILD);
  76         log("\n####  yieldTest: started  ####\n");
  77 
  78         // We first need to warmup before reproducing the assert issue that this test uncovered.
  79         for (int i = 0; i < 500; i++) {
  80             cont.run();
  81         }
  82         log("\n####  yieldTest: done warming up ####\n");
  83 
  84         // Enable the breakpoint on Continuation.yield0(). Once hit, single stepping will be enabled.
  85         enableEvents(Thread.currentThread(), java.lang.Continuation.class);
  86 
  87         cont.run();
  88         cont.run();
  89         done = true;
  90         cont.run();
  91 
  92         try {
  93             cont.run();
  94         } catch (IllegalStateException e) {
  95         }
  96 
  97         check();
  98         log("\n####  yieldTest: finished ####\n");
  99     }
 100 }