author | egahlin |
Fri, 29 Nov 2019 17:31:01 +0100 | |
changeset 59327 | 2c3578aa0bdf |
parent 58863 | c16ac7a2eba4 |
permissions | -rw-r--r-- |
58863 | 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 |
|
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
39 |
* @library /test/lib /test/jdk |
58863 | 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(); |
|
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
61 |
testOnEventAfterStart(); |
58863 | 62 |
} |
63 |
||
64 |
private static void testOnEventNull() { |
|
65 |
log("Entering testOnEventNull()"); |
|
66 |
try (RecordingStream rs = new RecordingStream()) { |
|
67 |
try { |
|
68 |
rs.onEvent(null); |
|
69 |
throw new AssertionError("Expected NullPointerException from onEvent(null)"); |
|
70 |
} catch (NullPointerException npe) { |
|
71 |
// OK; as expected |
|
72 |
} |
|
73 |
try { |
|
74 |
rs.onEvent("A", null); |
|
75 |
throw new AssertionError("Expected NullPointerException from onEvent(\"A\", null)"); |
|
76 |
||
77 |
} catch (NullPointerException npe) { |
|
78 |
// OK; as expected |
|
79 |
} |
|
80 |
try { |
|
81 |
String s = null; |
|
82 |
rs.onEvent(s, null); |
|
83 |
throw new AssertionError("Expected NullPointerException from onEvent(null, null)"); |
|
84 |
} catch (NullPointerException npe) { |
|
85 |
// OK; as expected |
|
86 |
} |
|
87 |
} |
|
88 |
log("Leaving testOnEventNull()"); |
|
89 |
} |
|
90 |
||
91 |
private static void testTwoEventWithSameName() throws Exception { |
|
92 |
log("Entering testTwoEventWithSameName()"); |
|
93 |
CountDownLatch eventA = new CountDownLatch(2); |
|
94 |
try (RecordingStream r = new RecordingStream()) { |
|
95 |
r.onEvent("A", e -> { |
|
96 |
System.out.println("testTwoEventWithSameName" + e); |
|
97 |
eventA.countDown(); |
|
98 |
}); |
|
99 |
r.startAsync(); |
|
100 |
EventA a1 = new EventA(); |
|
101 |
a1.commit(); |
|
102 |
EventAlsoA a2 = new EventAlsoA(); |
|
103 |
a2.commit(); |
|
104 |
eventA.await(); |
|
105 |
} |
|
106 |
log("Leaving testTwoEventWithSameName()"); |
|
107 |
} |
|
108 |
||
109 |
private static void testNamedEvent() throws Exception { |
|
110 |
log("Entering testNamedEvent()"); |
|
111 |
try (RecordingStream r = new RecordingStream()) { |
|
112 |
CountDownLatch eventA = new CountDownLatch(1); |
|
113 |
CountDownLatch eventC = new CountDownLatch(1); |
|
114 |
r.onEvent("A", e -> { |
|
115 |
System.out.println("TestNamedEvent:" + e); |
|
116 |
if (e.getEventType().getName().equals("A")) { |
|
117 |
eventA.countDown(); |
|
118 |
} |
|
119 |
}); |
|
120 |
r.onEvent("C", e -> { |
|
121 |
System.out.println("TestNamedEvent:" + e); |
|
122 |
if (e.getEventType().getName().equals("C")) { |
|
123 |
eventC.countDown(); |
|
124 |
} |
|
125 |
}); |
|
126 |
||
127 |
r.startAsync(); |
|
128 |
EventA a = new EventA(); |
|
129 |
a.commit(); |
|
130 |
EventC c = new EventC(); |
|
131 |
c.commit(); |
|
132 |
eventA.await(); |
|
133 |
eventC.await(); |
|
134 |
} |
|
135 |
log("Leaving testNamedEvent()"); |
|
136 |
} |
|
137 |
||
138 |
private static void testOnEvent() throws Exception { |
|
139 |
log("Entering testOnEvent()"); |
|
140 |
try (RecordingStream r = new RecordingStream()) { |
|
141 |
CountDownLatch event = new CountDownLatch(1); |
|
142 |
r.onEvent(e -> { |
|
143 |
event.countDown(); |
|
144 |
}); |
|
145 |
r.startAsync(); |
|
146 |
EventA a = new EventA(); |
|
147 |
a.commit(); |
|
148 |
event.await(); |
|
149 |
} |
|
150 |
log("Leaving testOnEvent()"); |
|
151 |
} |
|
152 |
||
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
153 |
private static void testOnEventAfterStart() { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
154 |
try (RecordingStream r = new RecordingStream()) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
155 |
EventProducer p = new EventProducer(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
156 |
p.start(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
157 |
Thread addHandler = new Thread(() -> { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
158 |
r.onEvent(e -> { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
159 |
// Got event, close stream |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
160 |
r.close(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
161 |
}); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
162 |
}); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
163 |
r.onFlush(() -> { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
164 |
// Only add handler once |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
165 |
if (!"started".equals(addHandler.getName())) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
166 |
addHandler.setName("started"); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
167 |
addHandler.start(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
168 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
169 |
}); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
170 |
r.start(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
171 |
p.kill(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
172 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
173 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
174 |
|
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
58863
diff
changeset
|
175 |
|
58863 | 176 |
private static void log(String msg) { |
177 |
System.out.println(msg); |
|
178 |
} |
|
179 |
} |