test/jdk/jdk/jfr/api/consumer/streaming/TestChunkGap.java
branchJEP-349-branch
changeset 57784 88316b070ab9
child 58715 038d13489b6c
equal deleted inserted replaced
57781:7758008806d1 57784:88316b070ab9
       
     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 
       
    26 package jdk.jfr.api.consumer.streaming;
       
    27 
       
    28 import java.time.Instant;
       
    29 import java.util.concurrent.CountDownLatch;
       
    30 
       
    31 import jdk.jfr.Event;
       
    32 import jdk.jfr.Recording;
       
    33 import jdk.jfr.consumer.EventStream;
       
    34 
       
    35 /**
       
    36  * @test
       
    37  * @summary Tests that a stream can gracefully handle chunk being removed in the
       
    38  *          middle
       
    39  * @key jfr
       
    40  * @requires vm.hasJFR
       
    41  * @library /test/lib
       
    42  * @run main/othervm jdk.jfr.api.consumer.streaming.TestFilledChunks
       
    43  */
       
    44 public class TestChunkGap {
       
    45 
       
    46     static class StartEvent extends Event {
       
    47     }
       
    48 
       
    49     static class TestGapEvent extends Event {
       
    50     }
       
    51 
       
    52     static class EndEvent extends Event {
       
    53     }
       
    54 
       
    55     static long count;
       
    56 
       
    57     public static void main(String... args) throws Exception {
       
    58 
       
    59         CountDownLatch gap = new CountDownLatch(1);
       
    60         try (EventStream s = EventStream.openRepository()) {
       
    61             try (Recording r1 = new Recording()) {
       
    62                 s.setStartTime(Instant.EPOCH);
       
    63                 s.onEvent(e -> {
       
    64                     System.out.println(e);
       
    65                     try {
       
    66                         gap.await();
       
    67                     } catch (InterruptedException e1) {
       
    68                         e1.printStackTrace();
       
    69                     }
       
    70                     count++;
       
    71                     if (e.getEventType().getName().equals(EndEvent.class.getName())) {
       
    72                         s.close();
       
    73                     }
       
    74                 });
       
    75                 s.startAsync();
       
    76 
       
    77                 r1.enable(StartEvent.class);
       
    78                 r1.start();
       
    79                 StartEvent event1 = new StartEvent();
       
    80                 event1.commit();
       
    81                 r1.stop();
       
    82 
       
    83                 // create chunk that is removed
       
    84                 try (Recording r2 = new Recording()) {
       
    85                     r2.enable(TestGapEvent.class);
       
    86                     r2.start();
       
    87                     TestGapEvent event2 = new TestGapEvent();
       
    88                     event2.commit();
       
    89                     r2.stop();
       
    90                 }
       
    91                 gap.countDown();
       
    92                 try (Recording r3 = new Recording()) {
       
    93                     r3.enable(EndEvent.class);
       
    94                     r3.start();
       
    95                     EndEvent event3 = new EndEvent();
       
    96                     event3.commit();
       
    97                     r3.stop();
       
    98 
       
    99                     s.awaitTermination();
       
   100                     if (count != 2) {
       
   101                         throw new AssertionError("Expected 2 event, but got " + count);
       
   102                     }
       
   103                 }
       
   104             }
       
   105         }
       
   106     }
       
   107 
       
   108 }