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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.flightrecorder;
  27 import static jdk.test.lib.Asserts.assertTrue;
  28 
  29 import java.util.concurrent.TimeUnit;
  30 import java.util.concurrent.locks.Condition;
  31 import java.util.concurrent.locks.Lock;
  32 import java.util.concurrent.locks.ReentrantLock;
  33 
  34 import jdk.jfr.FlightRecorder;
  35 import jdk.jfr.FlightRecorderListener;
  36 
  37 /**
  38  * @test
  39  * @key jfr
  40  * 
  41  * @library /lib /
  42  * @run main/othervm jdk.jfr.api.flightrecorder.TestFlightRecorderListenerRecorderInitialized
  43  */
  44 public class TestFlightRecorderListenerRecorderInitialized {
  45 
  46     /**
  47      * Utility class to wait/notify
  48      */
  49     private static class Signal {
  50 
  51         private static volatile boolean signalled;
  52         private static final Lock lock = new ReentrantLock();
  53         private static final Condition cond = lock.newCondition();
  54 
  55         private static void waitFor(long timeout, TimeUnit timeUnit) throws InterruptedException {
  56             try {
  57                 lock.lock();
  58                 if (!signalled) {
  59                     log("Waiting for FlightRecorder.recorderInitialized notification...");
  60                     cond.await(timeout, timeUnit);
  61                 }
  62             } finally {
  63                 lock.unlock();
  64             }
  65         }
  66 
  67         private static void signal() {
  68             try {
  69                 lock.lock();
  70                 signalled = true;
  71                 cond.signalAll();
  72             } finally {
  73                 lock.unlock();
  74             }
  75         }
  76     };
  77 
  78     public static void main(String[] args) throws Throwable {
  79         FlightRecorder.addListener(new FlightRecorderListener() {
  80 
  81             @Override
  82             public void recorderInitialized(FlightRecorder recorder) {
  83                 log("Recorder initialized");
  84                 Signal.signal();
  85             }
  86 
  87         });
  88         FlightRecorder.getFlightRecorder();
  89 
  90         Signal.waitFor(3, TimeUnit.SECONDS);
  91 
  92         assertTrue(Signal.signalled, "FlightRecorderListener.recorderInitialized has not been called");
  93 
  94     }
  95 
  96     private static void log(String s) {
  97         System.out.println(s);
  98     }
  99 }