Skip to content

Commit d7b0758

Browse files
add test to verify mirror events
1 parent 802c40d commit d7b0758

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2023, 2023, Red Hat Inc. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
27+
package com.oracle.svm.test.jfr;
28+
29+
import jdk.jfr.EventType;
30+
import jdk.jfr.Recording;
31+
import jdk.jfr.consumer.RecordedEvent;
32+
import jdk.jfr.consumer.RecordingFile;
33+
import org.junit.Test;
34+
35+
import java.nio.file.Path;
36+
import java.util.List;
37+
38+
import com.oracle.svm.test.jfr.utils.JfrFileParser;
39+
40+
import static org.junit.Assert.assertTrue;
41+
42+
/**
43+
* This test checks that JFR mirror events have been set up correctly. If mirror events have not
44+
* been set up correctly, then mirrored events will have incorrect metadata. This test verifies that
45+
* metadata.
46+
*/
47+
public class TestMirrorEvents extends JfrRecordingTest {
48+
49+
@Test
50+
public void test() throws Throwable {
51+
Recording recording = new Recording();
52+
53+
// Disable all events except Mirror Events.
54+
recording.enable("jdk.ThreadSleep");
55+
recording.enable("jdk.VirtualThreadStart");
56+
recording.enable("jdk.VirtualThreadEnd");
57+
58+
Path path = createTempJfrFile();
59+
recording.setDestination(path);
60+
recording.start();
61+
62+
// Generate some mirror event emissions.
63+
Runnable eventEmitter = () -> {
64+
try {
65+
Thread.sleep(100);
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
};
70+
71+
VirtualStressor.execute(1, eventEmitter);
72+
73+
recording.stop();
74+
recording.close();
75+
76+
// Some standard checks
77+
JfrFileParser parser = new JfrFileParser(path);
78+
parser.verify();
79+
80+
boolean foundSleepEvent = false;
81+
boolean foundVthreadStartEvent = false;
82+
boolean foundVthreadEndEvent = false;
83+
84+
List<RecordedEvent> recordedEvents = RecordingFile.readAllEvents(path);
85+
assertTrue("No events were recorded.", recordedEvents.size() > 0);
86+
87+
for (RecordedEvent event : recordedEvents) {
88+
EventType eventType = event.getEventType();
89+
90+
assertTrue("Mirror event metadata not applied correctly.", !eventType.getName().equals("jdk.internal.event.ThreadSleepEvent") &&
91+
!eventType.getName().equals("jdk.internal.event.VirtualThreadStartEvent") && !eventType.getName().equals("jdk.internal.event.VirtualThreadEndEvent"));
92+
93+
System.out.println(eventType.getName());
94+
if (eventType.getName().equals("jdk.ThreadSleep") && eventType.getCategoryNames().contains("Java Application") && eventType.getLabel().equals("Java Thread Sleep")) {
95+
foundSleepEvent = true;
96+
}
97+
if (eventType.getName().equals("jdk.VirtualThreadStart") && eventType.getCategoryNames().contains("Java Application") && eventType.getLabel().equals("Virtual Thread Start")) {
98+
foundVthreadStartEvent = true;
99+
}
100+
if (eventType.getName().equals("jdk.VirtualThreadEnd") && eventType.getCategoryNames().contains("Java Application") && eventType.getLabel().equals("Virtual Thread End")) {
101+
foundVthreadEndEvent = true;
102+
}
103+
}
104+
105+
// The only events recorded should be the mirror events we explicitly enabled. If they were
106+
// not found, it means their metadata is incorrect.
107+
assertTrue("Mirror events containing correct metadata not found.", foundSleepEvent && foundVthreadStartEvent && foundVthreadEndEvent);
108+
}
109+
110+
}

0 commit comments

Comments
 (0)