test/jdk/jdk/jfr/api/consumer/streaming/TestStartMultiChunk.java
branchJEP-349-branch
changeset 57361 53dccc90a5be
child 58076 ca625d28c580
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     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.  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 package jdk.jfr.api.consumer.streaming;
       
    26 
       
    27 import java.util.concurrent.CountDownLatch;
       
    28 
       
    29 import jdk.jfr.Event;
       
    30 import jdk.jfr.FlightRecorder;
       
    31 import jdk.jfr.Name;
       
    32 import jdk.jfr.Period;
       
    33 import jdk.jfr.Recording;
       
    34 import jdk.jfr.consumer.RecordingStream;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @summary Verifies that it is possible to stream contents of ongoing
       
    39  *          recordings
       
    40  * @key jfr
       
    41  * @requires vm.hasJFR
       
    42  * @library /test/lib
       
    43  * @run main/othervm -Xlog:jfr+system+streaming=trace
       
    44  *      jdk.jfr.api.consumer.streaming.TestStartMultiChunk
       
    45  */
       
    46 public class TestStartMultiChunk {
       
    47 
       
    48     @Period("10 s")
       
    49     @Name("Zebra")
       
    50     static class ZebraEvent extends Event {
       
    51     }
       
    52 
       
    53     @Name("Cat")
       
    54     static class CatEvent extends Event {
       
    55     }
       
    56 
       
    57     @Name("Dog")
       
    58     static class DogEvent extends Event {
       
    59     }
       
    60 
       
    61     @Name("Mouse")
       
    62     static class MouseEvent extends Event {
       
    63     }
       
    64 
       
    65     public static void main(String... args) throws Exception {
       
    66         CountDownLatch dogLatch = new CountDownLatch(1);
       
    67         CountDownLatch catLatch = new CountDownLatch(1);
       
    68         CountDownLatch mouseLatch = new CountDownLatch(1);
       
    69         CountDownLatch zebraLatch = new CountDownLatch(3);
       
    70 
       
    71         FlightRecorder.addPeriodicEvent(ZebraEvent.class, () -> {
       
    72             ZebraEvent ze = new ZebraEvent();
       
    73             ze.commit();
       
    74             System.out.println("Zebra emitted");
       
    75         });
       
    76 
       
    77         try (RecordingStream s = new RecordingStream()) {
       
    78             s.onEvent("Cat", e -> {
       
    79                 System.out.println("Found cat!");
       
    80                 catLatch.countDown();
       
    81             });
       
    82             s.onEvent("Dog", e -> {
       
    83                 System.out.println("Found dog!");
       
    84                 dogLatch.countDown();
       
    85             });
       
    86             s.onEvent("Zebra", e -> {
       
    87                 System.out.println("Found zebra!");
       
    88                 zebraLatch.countDown();
       
    89             });
       
    90             s.onEvent("Mouse", e -> {
       
    91                 System.out.println("Found mouse!");
       
    92                 mouseLatch.countDown();
       
    93             });
       
    94             s.startAsync();
       
    95             System.out.println("Stream recoding started");
       
    96 
       
    97             try (Recording r1 = new Recording()) {
       
    98                 r1.start();
       
    99                 System.out.println("r1.start()");
       
   100                 MouseEvent me = new MouseEvent();
       
   101                 me.commit();
       
   102                 System.out.println("Mouse emitted");
       
   103                 mouseLatch.await();
       
   104                 try (Recording r2 = new Recording()) { // force chunk rotation
       
   105                                                        // in stream
       
   106                     r2.start();
       
   107                     System.out.println("r2.start()");
       
   108                     DogEvent de = new DogEvent();
       
   109                     de.commit();
       
   110                     System.out.println("Dog emitted");
       
   111                     dogLatch.await();
       
   112                     CatEvent ce = new CatEvent();
       
   113                     ce.commit();
       
   114                     System.out.println("Cat emitted");
       
   115                     catLatch.await();
       
   116                     zebraLatch.await();
       
   117                 }
       
   118             }
       
   119         }
       
   120     }
       
   121 }