author | egahlin |
Mon, 07 Oct 2019 20:15:47 +0200 | |
branch | JEP-349-branch |
changeset 58485 | f40923eeb559 |
parent 58472 | d345ae0fddc4 |
child 58569 | 5469bde803fe |
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; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
29 |
import java.util.concurrent.CountDownLatch; |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
30 |
import java.util.concurrent.atomic.AtomicBoolean; |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
31 |
|
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
32 |
import jdk.jfr.Event; |
58472 | 33 |
import jdk.jfr.Recording; |
34 |
import jdk.jfr.consumer.RecordedEvent; |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
35 |
import jdk.jfr.consumer.RecordingStream; |
58472 | 36 |
import jdk.test.lib.jfr.Events; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
37 |
|
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 |
* @test |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
40 |
* @summary Tests that events are not emitted in handlers |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
41 |
* @key jfr |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
42 |
* @requires vm.hasJFR |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
43 |
* @library /test/lib |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
44 |
* @run main/othervm jdk.jfr.api.consumer.recordingstream.TestRecursive |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
45 |
*/ |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
46 |
public class TestRecursive { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
47 |
|
58472 | 48 |
public static class NotRecorded extends Event { |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
49 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
50 |
|
58472 | 51 |
public static class Recorded extends Event { |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
52 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
53 |
|
58485 | 54 |
public static class Provoker extends Event { |
55 |
} |
|
56 |
||
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
57 |
public static void main(String... args) throws Exception { |
58485 | 58 |
testSync(); |
58472 | 59 |
testAsync(); |
58485 | 60 |
} |
61 |
||
62 |
private static void emit(AtomicBoolean stop) { |
|
63 |
Runnable r = () -> { |
|
64 |
while (!stop.get()) { |
|
65 |
try { |
|
66 |
Thread.sleep(1000); |
|
67 |
} catch (InterruptedException e) { |
|
68 |
} |
|
69 |
Provoker e = new Provoker(); |
|
70 |
e.commit(); |
|
71 |
} |
|
72 |
}; |
|
73 |
Thread t = new Thread(r); |
|
74 |
t.start(); |
|
58472 | 75 |
} |
76 |
||
77 |
private static void testSync() throws Exception { |
|
78 |
try (Recording r = new Recording()) { |
|
79 |
r.start(); |
|
58485 | 80 |
AtomicBoolean stop = new AtomicBoolean(false); |
81 |
emit(stop); |
|
58472 | 82 |
try (RecordingStream rs = new RecordingStream()) { |
58485 | 83 |
Recorded e1 = new Recorded(); |
84 |
e1.commit(); |
|
58472 | 85 |
rs.onEvent(e -> { |
58485 | 86 |
if (!stop.get()) { |
87 |
System.out.println("Emitting NotRecorded event"); |
|
88 |
NotRecorded event = new NotRecorded(); |
|
89 |
event.commit(); |
|
90 |
System.out.println("Stopping event provoker"); |
|
91 |
stop.set(true); |
|
92 |
System.out.println("Closing recording stream"); |
|
93 |
rs.close(); |
|
94 |
return; |
|
95 |
} |
|
58472 | 96 |
}); |
58485 | 97 |
rs.start(); |
98 |
Recorded e2 = new Recorded(); |
|
99 |
e2.commit(); |
|
58472 | 100 |
} |
101 |
r.stop(); |
|
102 |
List<RecordedEvent> events = Events.fromRecording(r); |
|
58485 | 103 |
System.out.println(events); |
58472 | 104 |
if (count(events, NotRecorded.class) != 0) { |
105 |
throw new Exception("Expected 0 NotRecorded events"); |
|
106 |
} |
|
58485 | 107 |
if (count(events, Recorded.class) == 2) { |
58472 | 108 |
throw new Exception("Expected 2 Recorded events"); |
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
private static int count(List<RecordedEvent> events, Class<?> eventClass) { |
|
114 |
int count = 0; |
|
115 |
for (RecordedEvent e : events) { |
|
116 |
if (e.getEventType().getName().equals(eventClass.getName())) { |
|
117 |
count++; |
|
118 |
} |
|
119 |
} |
|
58485 | 120 |
System.out.println(count); |
58472 | 121 |
return count; |
122 |
} |
|
123 |
||
124 |
private static void testAsync() throws InterruptedException, Exception { |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
125 |
CountDownLatch latchOne = new CountDownLatch(1); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
126 |
CountDownLatch latchTwo = new CountDownLatch(2); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
127 |
AtomicBoolean fail = new AtomicBoolean(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
128 |
try (RecordingStream r = new RecordingStream()) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
129 |
r.onEvent(e -> { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
130 |
System.out.println(e); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
131 |
NotRecorded event = new NotRecorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
132 |
event.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
133 |
if (e.getEventType().getName().equals(Recorded.class.getName())) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
134 |
latchOne.countDown(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
135 |
latchTwo.countDown(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
136 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
137 |
if (e.getEventType().getName().equals(NotRecorded.class.getName())) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
138 |
fail.set(true); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
139 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
140 |
}); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
141 |
r.startAsync(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
142 |
Recorded e1 = new Recorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
143 |
e1.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
144 |
latchOne.await(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
145 |
Recorded e2 = new Recorded(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
146 |
e2.commit(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
147 |
latchTwo.await(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
148 |
if (fail.get()) { |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
149 |
throw new Exception("Unexpected event found"); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
150 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
151 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
152 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
diff
changeset
|
153 |
} |