test/jdk/jdk/jfr/api/flightrecorder/TestAddPeriodicEvent.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 
       
    28 import static jdk.test.lib.Asserts.assertFalse;
       
    29 import static jdk.test.lib.Asserts.assertTrue;
       
    30 
       
    31 import java.time.Duration;
       
    32 import java.util.concurrent.CountDownLatch;
       
    33 
       
    34 import jdk.jfr.Event;
       
    35 import jdk.jfr.FlightRecorder;
       
    36 import jdk.jfr.Recording;
       
    37 
       
    38 /*
       
    39  * @test
       
    40  * @summary
       
    41  * @key jfr
       
    42  * @library /test/lib
       
    43  * @run main/othervm jdk.jfr.api.flightrecorder.TestAddPeriodicEvent
       
    44  */
       
    45 public class TestAddPeriodicEvent {
       
    46 
       
    47     private static class MyEvent extends Event {
       
    48 
       
    49     }
       
    50 
       
    51     CountDownLatch latch = new CountDownLatch(3);
       
    52 
       
    53     class MyHook implements Runnable {
       
    54 
       
    55         private int eventCounter;
       
    56         private long previousTime;
       
    57 
       
    58         @Override
       
    59         public void run() {
       
    60             log("Commiting event " + (++eventCounter));
       
    61             if (previousTime == 0) {
       
    62                 previousTime = System.currentTimeMillis();
       
    63             } else {
       
    64                 long nowTime = System.currentTimeMillis();
       
    65                 long elapsedTime = nowTime - previousTime;
       
    66                 previousTime = nowTime;
       
    67                 log("Elapsed time since the previous event: " + elapsedTime);
       
    68             }
       
    69 
       
    70             commitEvent();
       
    71             latch.countDown();
       
    72         }
       
    73 
       
    74         private void commitEvent() {
       
    75             MyEvent event = new MyEvent();
       
    76             event.commit();
       
    77         }
       
    78     }
       
    79 
       
    80     public static void main(String[] args) throws Exception {
       
    81         new TestAddPeriodicEvent().doTest(1000, 3);
       
    82     }
       
    83 
       
    84     private void doTest(long eventDuration, int numOfEvents) throws Exception {
       
    85         latch = new CountDownLatch(numOfEvents);
       
    86         MyHook hook = new MyHook();
       
    87 
       
    88         Recording r = new Recording();
       
    89         r.enable(MyEvent.class).withPeriod(Duration.ofMillis(eventDuration));
       
    90         r.start();
       
    91 
       
    92         FlightRecorder.addPeriodicEvent(MyEvent.class, hook);
       
    93 
       
    94         latch.await();
       
    95 
       
    96         assertTrue(FlightRecorder.removePeriodicEvent(hook));
       
    97         assertFalse(FlightRecorder.removePeriodicEvent(hook));
       
    98 
       
    99         r.stop();
       
   100         r.close();
       
   101     }
       
   102 
       
   103     private static void log(String text) {
       
   104         System.out.println(text);
       
   105     }
       
   106 }