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