test/jdk/jdk/jfr/api/consumer/recordingstream/TestOnEvent.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.recordingstream;
       
    26 
       
    27 import java.util.concurrent.CountDownLatch;
       
    28 
       
    29 import jdk.jfr.Event;
       
    30 import jdk.jfr.Name;
       
    31 import jdk.jfr.consumer.RecordingStream;
       
    32 
       
    33 /**
       
    34  * @test
       
    35  * @summary Tests RecordingStream::onEvent(...)
       
    36  * @key jfr
       
    37  * @requires vm.hasJFR
       
    38  * @library /test/lib
       
    39  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestOnEvent
       
    40  */
       
    41 public class TestOnEvent {
       
    42 
       
    43     @Name("A")
       
    44     static class EventA extends Event {
       
    45     }
       
    46 
       
    47     @Name("A")
       
    48     static class EventAlsoA extends Event {
       
    49     }
       
    50 
       
    51     @Name("C")
       
    52     static class EventC extends Event {
       
    53     }
       
    54 
       
    55     public static void main(String... args) throws Exception {
       
    56         testOnEventNull();
       
    57         testOnEvent();
       
    58         testNamedEvent();
       
    59         testTwoEventWithSameName();
       
    60     }
       
    61 
       
    62     private static void testOnEventNull() {
       
    63         try (RecordingStream rs = new RecordingStream()) {
       
    64            try {
       
    65                rs.onEvent(null);
       
    66                throw new AssertionError("Expected NullPointerException from onEvent(null)");
       
    67            } catch (NullPointerException npe) {
       
    68                // OK; as expected
       
    69            }
       
    70            try {
       
    71                rs.onEvent("A", null);
       
    72                throw new AssertionError("Expected NullPointerException from onEvent(\"A\", null)");
       
    73 
       
    74            } catch (NullPointerException npe) {
       
    75                // OK; as expected
       
    76            }
       
    77            try {
       
    78                String s = null;
       
    79                rs.onEvent(s, null);
       
    80                throw new AssertionError("Expected NullPointerException from onEvent(null, null)");
       
    81            } catch (NullPointerException npe) {
       
    82                // OK; as expected
       
    83            }
       
    84         }
       
    85      }
       
    86 
       
    87     private static void testTwoEventWithSameName() throws Exception {
       
    88         CountDownLatch eventA = new CountDownLatch(2);
       
    89         try (RecordingStream r = new RecordingStream()) {
       
    90             r.onEvent("A", e -> {
       
    91                 System.out.println("testTwoEventWithSameName" +  e);
       
    92                 eventA.countDown();
       
    93             });
       
    94             r.startAsync();
       
    95             EventA a1 = new EventA();
       
    96             a1.commit();
       
    97             EventAlsoA a2 = new EventAlsoA();
       
    98             a2.commit();
       
    99             eventA.await();
       
   100         }
       
   101     }
       
   102 
       
   103     private static void testNamedEvent() throws Exception {
       
   104         try (RecordingStream r = new RecordingStream()) {
       
   105             CountDownLatch eventA = new CountDownLatch(1);
       
   106             CountDownLatch eventC = new CountDownLatch(1);
       
   107             r.onEvent("A", e -> {
       
   108                 System.out.println("TestNamedEvent:" + e);
       
   109                 if (e.getEventType().getName().equals("A")) {
       
   110                     eventA.countDown();
       
   111                 }
       
   112             });
       
   113             r.onEvent("C", e -> {
       
   114                 System.out.println("TestNamedEvent:" + e);
       
   115                 if (e.getEventType().getName().equals("C")) {
       
   116                     eventC.countDown();
       
   117                 }
       
   118             });
       
   119 
       
   120             r.startAsync();
       
   121             EventA a = new EventA();
       
   122             a.commit();
       
   123             EventC c = new EventC();
       
   124             c.commit();
       
   125             eventA.await();
       
   126             eventC.await();
       
   127         }
       
   128     }
       
   129 
       
   130     private static void testOnEvent() throws Exception {
       
   131         try (RecordingStream r = new RecordingStream()) {
       
   132             CountDownLatch event = new CountDownLatch(1);
       
   133             r.onEvent(e -> {
       
   134                 event.countDown();
       
   135             });
       
   136             r.startAsync();
       
   137             EventA a = new EventA();
       
   138             a.commit();
       
   139             event.await();
       
   140         }
       
   141     }
       
   142 }