1 /*
   2  * Copyright (c) 2019, 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  * @summary Verifies that single stepping into Continuation.doContinue() properly completes in yield0().
  27  * @compile DoContinueSingleStepTest.java
  28  * @run main/othervm/native -XX:+UseContinuationLazyCopy -agentlib:DoContinueSingleStepTest -Djdk.defaultScheduler.parallelism=1 DoContinueSingleStepTest 150
  29  * @run main/othervm/native -XX:+UseContinuationLazyCopy -agentlib:DoContinueSingleStepTest -Djdk.defaultScheduler.parallelism=1 DoContinueSingleStepTest 500
  30  * @run main/othervm/native -XX:+UseContinuationLazyCopy -agentlib:DoContinueSingleStepTest DoContinueSingleStepTest 150
  31  * @run main/othervm/native -XX:+UseContinuationLazyCopy -agentlib:DoContinueSingleStepTest DoContinueSingleStepTest 500
  32  */
  33 
  34 import java.util.concurrent.*;
  35 
  36 public class DoContinueSingleStepTest {
  37     private static final String agentLib = "DoContinueSingleStepTest";
  38 
  39     static native void enableEvents(Thread thread, Class contClass);
  40     static native boolean check();
  41 
  42     static int MSG_COUNT; // Passed as an argument
  43     static final SynchronousQueue<String> QUEUE = new SynchronousQueue<>();
  44 
  45     static final Runnable PRODUCER = () -> {
  46         try {
  47             for (int i = 0; i < MSG_COUNT; i++) {
  48                 QUEUE.put("msg"+i);
  49                 QUEUE.put("msg"+i);
  50                 if (i == MSG_COUNT - 10) {
  51                     // Once we have warmed up, enable the first breakpoint which eventually will
  52                     // lead to enabling single stepping.
  53                     enableEvents(Thread.currentThread(), java.lang.Continuation.class);
  54                 }
  55             }
  56         } catch (InterruptedException e) { }
  57     };
  58 
  59     static final Runnable CONSUMER = () -> {
  60         try {
  61             for (int i = 0; i < MSG_COUNT; i++) {
  62                 String s = QUEUE.take();
  63             }
  64         } catch (InterruptedException e) { }
  65     };
  66 
  67     public static void test1() throws Exception {
  68         try (var scope = FiberScope.open()) {
  69             scope.schedule(PRODUCER);
  70             scope.schedule(CONSUMER);
  71             scope.schedule(CONSUMER);
  72         }
  73     }
  74 
  75     void runTest() throws Exception {
  76         test1();
  77     }
  78 
  79     public static void main(String[] args) throws Exception {
  80         if (args.length != 1) {
  81             throw new RuntimeException("Invalid # of arguments.");
  82         } else {
  83             MSG_COUNT = Integer.valueOf(args[0]);
  84         }
  85 
  86         try {
  87             System.out.println("loading " + agentLib + " lib");
  88             System.loadLibrary(agentLib);
  89         } catch (UnsatisfiedLinkError ex) {
  90             System.err.println("Failed to load " + agentLib + " lib");
  91             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
  92             throw ex;
  93         }
  94 
  95         DoContinueSingleStepTest obj = new DoContinueSingleStepTest();
  96         obj.runTest();
  97         if (!check()) {
  98             throw new RuntimeException("DoContinueSingleStepTest failed!");
  99         }
 100         System.out.println("DoContinueSingleStepTest passed\n");
 101         System.out.println("\n#####   main: finished  #####\n");
 102     }
 103 }