test/jdk/jdk/jfr/api/consumer/recordingstream/TestOnFlush.java
changeset 58863 c16ac7a2eba4
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     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.recordingstream;
       
    27 
       
    28 import java.util.concurrent.CountDownLatch;
       
    29 
       
    30 import jdk.jfr.Event;
       
    31 import jdk.jfr.consumer.RecordingStream;
       
    32 
       
    33 /**
       
    34  * @test
       
    35  * @summary Tests RecordingStream::onFlush(...)
       
    36  * @key jfr
       
    37  * @requires vm.hasJFR
       
    38  * @library /test/lib
       
    39  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestOnFlush
       
    40  */
       
    41 public class TestOnFlush {
       
    42 
       
    43     static class OneEvent extends Event {
       
    44     }
       
    45 
       
    46     public static void main(String... args) throws Exception {
       
    47         testOnFlushNull();
       
    48         testOneEvent();
       
    49         testNoEvent();
       
    50     }
       
    51 
       
    52     private static void testOnFlushNull() {
       
    53         log("Entering testOnFlushNull()");
       
    54         try (RecordingStream rs = new RecordingStream()) {
       
    55            try {
       
    56                rs.onFlush(null);
       
    57                throw new AssertionError("Expected NullPointerException from onFlush(null");
       
    58            } catch (NullPointerException npe) {
       
    59                // OK; as expected
       
    60            }
       
    61         }
       
    62         log("Leaving testOnFlushNull()");
       
    63      }
       
    64 
       
    65     private static void testNoEvent() throws Exception {
       
    66         log("Entering testNoEvent()");
       
    67         CountDownLatch flush = new CountDownLatch(1);
       
    68         try (RecordingStream r = new RecordingStream()) {
       
    69             r.onFlush(() -> {
       
    70                 flush.countDown();
       
    71             });
       
    72             r.startAsync();
       
    73             flush.await();
       
    74         }
       
    75         log("Leaving testNoEvent()");
       
    76     }
       
    77 
       
    78     private static void testOneEvent() throws InterruptedException {
       
    79         log("Entering testOneEvent()");
       
    80         CountDownLatch flush = new CountDownLatch(1);
       
    81         try (RecordingStream r = new RecordingStream()) {
       
    82             r.onEvent(e -> {
       
    83                 // ignore event
       
    84             });
       
    85             r.onFlush(() -> {
       
    86                 flush.countDown();
       
    87             });
       
    88             r.startAsync();
       
    89             OneEvent e = new OneEvent();
       
    90             e.commit();
       
    91             flush.await();
       
    92         }
       
    93         log("Leaving testOneEvent()");
       
    94     }
       
    95 
       
    96     private static void log(String msg) {
       
    97         System.out.println(msg);
       
    98     }
       
    99 }