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 Fibers.
  29  * @compile FiberTest.java
  30  * @run main/othervm/native -agentlib:FiberTest=EnableContinuationEvents MyPackage.FiberTest
  31  * @run main/othervm/native -agentlib:FiberTest=DisableContinuationEvents MyPackage.FiberTest
  32  */
  33 
  34 import java.util.concurrent.*;
  35 
  36 public class FiberTest {
  37     private static final String agentLib = "FiberTest";
  38 
  39     static final int MSG_COUNT = 10*1000;
  40     static final SynchronousQueue<String> QUEUE = new SynchronousQueue<>();
  41 
  42     static final Runnable PRODUCER = () -> {
  43         try {
  44             for (int i = 0; i < MSG_COUNT; i++) {
  45                 QUEUE.put("msg");
  46             }
  47         } catch (InterruptedException e) { }
  48     };
  49 
  50     static final Runnable CONSUMER = () -> {
  51         try {
  52             for (int i = 0; i < MSG_COUNT; i++) {
  53                 String s = QUEUE.take();
  54             }
  55         } catch (InterruptedException e) { }
  56     };
  57 
  58     public static void test1() throws Exception {
  59         try (var scope = FiberScope.open()) {
  60             scope.schedule(PRODUCER);
  61             scope.schedule(CONSUMER);
  62         }
  63     }
  64 
  65     void runTest() throws Exception {
  66         test1();
  67     }
  68 
  69     public static void main(String[] args) throws Exception {
  70         try {
  71             System.loadLibrary(agentLib);
  72         } catch (UnsatisfiedLinkError ex) {
  73             System.err.println("Failed to load " + agentLib + " lib");
  74             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
  75             throw ex;
  76         }
  77 
  78         FiberTest obj = new FiberTest();
  79         obj.runTest();
  80     }
  81 }