author | egahlin |
Wed, 30 Oct 2019 17:02:26 +0100 | |
branch | JEP-349-branch |
changeset 58862 | abeb87af766e |
parent 58577 | 7e0f81f63890 |
permissions | -rw-r--r-- |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
1 |
/* |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
4 |
* |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
10 |
* |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
15 |
* accompanied this code). |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
16 |
* |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
20 |
* |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
23 |
* questions. |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
24 |
*/ |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
25 |
|
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
26 |
package jdk.jfr.api.consumer.recordingstream; |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
27 |
|
58472 | 28 |
import java.util.List; |
58569 | 29 |
import java.util.concurrent.CompletableFuture; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
30 |
import java.util.concurrent.CountDownLatch; |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
31 |
import java.util.concurrent.atomic.AtomicBoolean; |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
32 |
|
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
33 |
import jdk.jfr.Event; |
58472 | 34 |
import jdk.jfr.Recording; |
35 |
import jdk.jfr.consumer.RecordedEvent; |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
36 |
import jdk.jfr.consumer.RecordingStream; |
58472 | 37 |
import jdk.test.lib.jfr.Events; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
38 |
|
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
39 |
/** |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
40 |
* @test |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
41 |
* @summary Tests that events are not emitted in handlers |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
42 |
* @key jfr |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
43 |
* @requires vm.hasJFR |
58569 | 44 |
* @library /test/lib /test/jdk |
58576 | 45 |
* @build jdk.jfr.api.consumer.recordingstream.EventProducer |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
46 |
* @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
47 |
*/ |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
48 |
public class TestRecursive { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
49 |
|
58472 | 50 |
public static class NotRecorded extends Event { |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
51 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
52 |
|
58472 | 53 |
public static class Recorded extends Event { |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
54 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
55 |
|
58485 | 56 |
public static class Provoker extends Event { |
57 |
} |
|
58 |
||
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
59 |
public static void main(String... args) throws Exception { |
58485 | 60 |
testSync(); |
58472 | 61 |
testAsync(); |
58569 | 62 |
testStreamInStream(); |
58485 | 63 |
} |
64 |
||
58569 | 65 |
private static void testStreamInStream() throws Exception { |
66 |
CountDownLatch latch = new CountDownLatch(1); |
|
67 |
try (Recording r = new Recording()) { |
|
68 |
r.start(); |
|
69 |
Recorded r1 = new Recorded(); // 1 |
|
70 |
r1.commit(); |
|
71 |
try (RecordingStream rs = new RecordingStream()) { |
|
72 |
rs.onEvent(e1 -> { |
|
73 |
streamInStream(); |
|
74 |
latch.countDown(); |
|
75 |
}); |
|
76 |
rs.startAsync(); |
|
77 |
Recorded r2 = new Recorded(); // 2 |
|
78 |
r2.commit(); |
|
79 |
latch.await(); |
|
80 |
} |
|
81 |
Recorded r3 = new Recorded(); // 2 |
|
82 |
r3.commit(); |
|
83 |
r.stop(); |
|
84 |
List<RecordedEvent> events = Events.fromRecording(r); |
|
85 |
if (count(events, NotRecorded.class) != 0) { |
|
86 |
throw new Exception("Expected 0 NotRecorded events"); |
|
87 |
} |
|
88 |
if (count(events, Recorded.class) != 3) { |
|
89 |
throw new Exception("Expected 3 Recorded events"); |
|
58485 | 90 |
} |
58569 | 91 |
} |
92 |
} |
|
93 |
||
94 |
// No events should be recorded in this method |
|
95 |
private static void streamInStream() { |
|
96 |
NotRecorded nr1 = new NotRecorded(); |
|
97 |
nr1.commit(); |
|
98 |
CountDownLatch latch = new CountDownLatch(1); |
|
99 |
try (RecordingStream rs2 = new RecordingStream()) { |
|
100 |
rs2.onEvent(e2 -> { |
|
101 |
NotRecorded nr2 = new NotRecorded(); |
|
102 |
nr2.commit(); |
|
103 |
latch.countDown(); |
|
104 |
}); |
|
105 |
NotRecorded nr3 = new NotRecorded(); |
|
106 |
nr3.commit(); |
|
107 |
rs2.startAsync(); |
|
108 |
// run event in separate thread |
|
109 |
CompletableFuture.runAsync(() -> { |
|
110 |
Provoker p = new Provoker(); |
|
111 |
p.commit(); |
|
112 |
}); |
|
113 |
try { |
|
114 |
latch.await(); |
|
115 |
} catch (InterruptedException e) { |
|
116 |
throw new Error("Unexpected interruption", e); |
|
117 |
} |
|
118 |
} |
|
119 |
NotRecorded nr2 = new NotRecorded(); |
|
120 |
nr2.commit(); |
|
58472 | 121 |
} |
122 |
||
123 |
private static void testSync() throws Exception { |
|
124 |
try (Recording r = new Recording()) { |
|
125 |
r.start(); |
|
58569 | 126 |
EventProducer p = new EventProducer(); |
58472 | 127 |
try (RecordingStream rs = new RecordingStream()) { |
58485 | 128 |
Recorded e1 = new Recorded(); |
129 |
e1.commit(); |
|
58472 | 130 |
rs.onEvent(e -> { |
58577 | 131 |
System.out.println("Emitting NotRecorded event"); |
132 |
NotRecorded event = new NotRecorded(); |
|
133 |
event.commit(); |
|
134 |
System.out.println("Stopping event provoker"); |
|
135 |
p.kill(); |
|
136 |
System.out.println("Closing recording stream"); |
|
137 |
rs.close(); |
|
138 |
return; |
|
58472 | 139 |
}); |
58569 | 140 |
p.start(); |
58485 | 141 |
rs.start(); |
142 |
Recorded e2 = new Recorded(); |
|
143 |
e2.commit(); |
|
58472 | 144 |
} |
145 |
r.stop(); |
|
146 |
List<RecordedEvent> events = Events.fromRecording(r); |
|
58485 | 147 |
System.out.println(events); |
58472 | 148 |
if (count(events, NotRecorded.class) != 0) { |
149 |
throw new Exception("Expected 0 NotRecorded events"); |
|
150 |
} |
|
58569 | 151 |
if (count(events, Recorded.class) != 2) { |
58472 | 152 |
throw new Exception("Expected 2 Recorded events"); |
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
private static int count(List<RecordedEvent> events, Class<?> eventClass) { |
|
158 |
int count = 0; |
|
159 |
for (RecordedEvent e : events) { |
|
160 |
if (e.getEventType().getName().equals(eventClass.getName())) { |
|
161 |
count++; |
|
162 |
} |
|
163 |
} |
|
58485 | 164 |
System.out.println(count); |
58472 | 165 |
return count; |
166 |
} |
|
167 |
||
168 |
private static void testAsync() throws InterruptedException, Exception { |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
169 |
CountDownLatch latchOne = new CountDownLatch(1); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
170 |
CountDownLatch latchTwo = new CountDownLatch(2); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
171 |
AtomicBoolean fail = new AtomicBoolean(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
172 |
try (RecordingStream r = new RecordingStream()) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
173 |
r.onEvent(e -> { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
174 |
System.out.println(e); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
175 |
NotRecorded event = new NotRecorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
176 |
event.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
177 |
if (e.getEventType().getName().equals(Recorded.class.getName())) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
178 |
latchOne.countDown(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
179 |
latchTwo.countDown(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
180 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
181 |
if (e.getEventType().getName().equals(NotRecorded.class.getName())) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
182 |
fail.set(true); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
183 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
184 |
}); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
185 |
r.startAsync(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
186 |
Recorded e1 = new Recorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
187 |
e1.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
188 |
latchOne.await(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
189 |
Recorded e2 = new Recorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
190 |
e2.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
191 |
latchTwo.await(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
192 |
if (fail.get()) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
193 |
throw new Exception("Unexpected event found"); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
194 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
195 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
196 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
197 |
} |