test/jdk/jdk/jfr/api/flightrecorder/TestFlightRecorderListenerRecorderInitialized.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     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  * @library /test/lib
       
    41  * @run main/othervm jdk.jfr.api.flightrecorder.TestFlightRecorderListenerRecorderInitialized
       
    42  */
       
    43 public class TestFlightRecorderListenerRecorderInitialized {
       
    44 
       
    45     /**
       
    46      * Utility class to wait/notify
       
    47      */
       
    48     private static class Signal {
       
    49 
       
    50         private static volatile boolean signalled;
       
    51         private static final Lock lock = new ReentrantLock();
       
    52         private static final Condition cond = lock.newCondition();
       
    53 
       
    54         private static void waitFor(long timeout, TimeUnit timeUnit) throws InterruptedException {
       
    55             try {
       
    56                 lock.lock();
       
    57                 if (!signalled) {
       
    58                     log("Waiting for FlightRecorder.recorderInitialized notification...");
       
    59                     cond.await(timeout, timeUnit);
       
    60                 }
       
    61             } finally {
       
    62                 lock.unlock();
       
    63             }
       
    64         }
       
    65 
       
    66         private static void signal() {
       
    67             try {
       
    68                 lock.lock();
       
    69                 signalled = true;
       
    70                 cond.signalAll();
       
    71             } finally {
       
    72                 lock.unlock();
       
    73             }
       
    74         }
       
    75     };
       
    76 
       
    77     public static void main(String[] args) throws Throwable {
       
    78         FlightRecorder.addListener(new FlightRecorderListener() {
       
    79 
       
    80             @Override
       
    81             public void recorderInitialized(FlightRecorder recorder) {
       
    82                 log("Recorder initialized");
       
    83                 Signal.signal();
       
    84             }
       
    85 
       
    86         });
       
    87         FlightRecorder.getFlightRecorder();
       
    88 
       
    89         Signal.waitFor(3, TimeUnit.SECONDS);
       
    90 
       
    91         assertTrue(Signal.signalled, "FlightRecorderListener.recorderInitialized has not been called");
       
    92 
       
    93     }
       
    94 
       
    95     private static void log(String s) {
       
    96         System.out.println(s);
       
    97     }
       
    98 }