Skip to content
Permalink
Browse files

8239265: JFR: Test cleanup of jdk.jfr.api.consumer package

Reviewed-by: mgronlun
  • Loading branch information
Erik Gahlin
Erik Gahlin committed Feb 18, 2020
1 parent 6f6b4c0 commit f75f78ae3a24d958088b1086861bb1149f3209ac
@@ -45,6 +45,8 @@
public class TestFieldAccess {

private static class MyEvent extends Event {
byte byteField = 42;
char charField = 'X';
String stringField = "Hello";
int intField = 4711;
long longField = 4712L;
@@ -72,6 +74,12 @@ public static void main(String[] args) throws Throwable {
}

private static void testGetField(RecordedEvent event, MyEvent myEvent) {
char charField = event.getValue("charField");
Asserts.assertEquals(charField, myEvent.charField);

byte byteField = event.getValue("byteField");
Asserts.assertEquals(byteField, myEvent.byteField);

String stringField = event.getValue("stringField");
Asserts.assertEquals(stringField, myEvent.stringField);

@@ -103,7 +111,6 @@ private static void testGetField(RecordedEvent event, MyEvent myEvent) {
String className = event.getValue("classField.name");
Asserts.assertEquals(classField.getName(), className.replace("/", "."));


try {
event.getValue("doesnotexist");
} catch (IllegalArgumentException iae) {
@@ -125,6 +132,8 @@ private static void testGetField(RecordedEvent event, MyEvent myEvent) {

private static void testHasField(RecordedEvent event) {
System.out.println(event);
Asserts.assertTrue(event.hasField("charField"));
Asserts.assertTrue(event.hasField("byteField"));
Asserts.assertTrue(event.hasField("stringField"));
Asserts.assertTrue(event.hasField("intField"));
Asserts.assertTrue(event.hasField("longField"));
@@ -31,7 +31,6 @@
import static jdk.test.lib.Asserts.assertTrue;

import java.util.List;
import java.util.function.Consumer;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
@@ -53,27 +52,26 @@
public class TestGetStackTrace {

public static void main(String[] args) throws Throwable {
testStackTrace(r -> r.enable(SimpleEvent.class), TestGetStackTrace::assertNoStackTrace);
testStackTrace(r -> r.enable(SimpleEvent.class).withoutStackTrace(), TestGetStackTrace::assertStackTrace);
testWithoutStackTrace(recordEvent("stackTrace", "false"));
testWithStackTrace(recordEvent("stackTrace", "true"));
}

private static void testStackTrace(Consumer<Recording> recordingConfigurer, Consumer<RecordedEvent> asserter) throws Throwable {
Recording r = new Recording();
recordingConfigurer.accept(r);
r.start();
SimpleEvent event = new SimpleEvent();
event.commit();
r.stop();
List<RecordedEvent> events = Events.fromRecording(r);
r.close();
Events.hasEvents(events);
private static RecordedEvent recordEvent(String key, String value) throws Throwable {
try (Recording r = new Recording()) {
r.enable(SimpleEvent.class).with(key, value);
r.start();
SimpleEvent event = new SimpleEvent();
event.commit();
r.stop();
return Events.fromRecording(r).get(0);
}
}

private static void assertNoStackTrace(RecordedEvent re) {
private static void testWithoutStackTrace(RecordedEvent re) {
assertNull(re.getStackTrace());
}

private static void assertStackTrace(RecordedEvent re) {
private static void testWithStackTrace(RecordedEvent re) {
assertNotNull(re.getStackTrace());
RecordedStackTrace strace = re.getStackTrace();
assertEquals(strace.isTruncated(), false);
@@ -56,42 +56,43 @@
public final class TestHiddenMethod {

public static void main(String[] args) throws Throwable {
Recording recording = new Recording();
recording.enable(MyEvent.class).withThreshold(Duration.ofMillis(0));
recording.start();
try (Recording recording = new Recording()) {
recording.enable(MyEvent.class).withThreshold(Duration.ofMillis(0));
recording.start();

// Commit event with hidden methods
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.eval(
"function emit() {"
+ " print('About to emit event from Javascript');"
+ " var TestEvent = Java.type(\"jdk.jfr.api.consumer.TestHiddenMethod$MyEvent\");"
+ " var event = new TestEvent;"
+ " event.begin();"
+ " event.end();"
+ " event.commit();"
+ " print('Event emitted from Javascript!');"
+ "}"
+ "emit();");
// Commit event with hidden methods
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.eval(
"function emit() {"
+ " print('About to emit event from Javascript');"
+ " var TestEvent = Java.type(\"jdk.jfr.api.consumer.TestHiddenMethod$MyEvent\");"
+ " var event = new TestEvent;"
+ " event.begin();"
+ " event.end();"
+ " event.commit();"
+ " print('Event emitted from Javascript!');"
+ "}"
+ "emit();");

// Commit event with visible method
MyEvent visible = new MyEvent();
visible.begin();
visible.end();
visible.commit();
recording.stop();
// Commit event with visible method
MyEvent visible = new MyEvent();
visible.begin();
visible.end();
visible.commit();
recording.stop();

List<RecordedEvent> events = Events.fromRecording(recording);
assertEquals(2, events.size(), "Expected two events");
RecordedEvent hiddenEvent = events.get(0);
RecordedEvent visibleEvent = events.get(1);
List<RecordedEvent> events = Events.fromRecording(recording);
assertEquals(2, events.size(), "Expected two events");
RecordedEvent hiddenEvent = events.get(0);
RecordedEvent visibleEvent = events.get(1);

System.out.println("hiddenEvent:" + hiddenEvent);
System.out.println("visibleEvent:" + visibleEvent);
System.out.println("hiddenEvent:" + hiddenEvent);
System.out.println("visibleEvent:" + visibleEvent);

assertTrue(hasHiddenStackFrame(hiddenEvent), "No hidden frame in hidden event: " + hiddenEvent);
assertFalse(hasHiddenStackFrame(visibleEvent), "Hidden frame in visible event: " + visibleEvent);
assertTrue(hasHiddenStackFrame(hiddenEvent), "No hidden frame in hidden event: " + hiddenEvent);
assertFalse(hasHiddenStackFrame(visibleEvent), "Hidden frame in visible event: " + visibleEvent);
}
}

private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable {
@@ -108,5 +109,4 @@ private static boolean hasHiddenStackFrame(RecordedEvent event) throws Throwable

public static class MyEvent extends Event {
}

}
@@ -41,6 +41,7 @@

/**
* @test
* @summary Verifies that a recorded method has the correct modifier
* @key jfr
* @requires vm.hasJFR
* @library /test/lib
@@ -49,33 +50,32 @@
public final class TestMethodGetModifiers {

public static void main(String[] args) throws Throwable {
Recording recording = new Recording();
recording.start();
try (Recording recording = new Recording()) {
recording.start();

SimpleEvent ev = new SimpleEvent();
ev.commit();
recording.stop();
SimpleEvent ev = new SimpleEvent();
ev.commit();
recording.stop();

List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
Events.hasEvents(recordedEvents);
RecordedEvent recordedEvent = recordedEvents.get(0);
List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
Events.hasEvents(recordedEvents);
RecordedEvent recordedEvent = recordedEvents.get(0);

System.out.println("recorded event:" + recordedEvent);
System.out.println(recordedEvent);

RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
List<RecordedFrame> frames = stacktrace.getFrames();
for (RecordedFrame frame : frames) {
RecordedMethod method = frame.getMethod();
if (method.getName().equals("main")) {
System.out.println("'main' method: " + method);
int modifiers = TestMethodGetModifiers.class.getDeclaredMethod("main", (Class<?>)String[].class).getModifiers();
System.out.println("modifiers: " + modifiers);
Asserts.assertEquals(method.getModifiers(), modifiers, "Incorrect method modifier reported");
RecordedClass type = method.getType();
assertNotNull(type, "Recorded class can not be null");
RecordedStackTrace stacktrace = recordedEvent.getStackTrace();
List<RecordedFrame> frames = stacktrace.getFrames();
for (RecordedFrame frame : frames) {
RecordedMethod method = frame.getMethod();
if (method.getName().equals("main")) {
System.out.println("'main' method: " + method);
int modifiers = TestMethodGetModifiers.class.getDeclaredMethod("main", (Class<?>)String[].class).getModifiers();
System.out.println("modifiers: " + modifiers);
Asserts.assertEquals(method.getModifiers(), modifiers, "Incorrect method modifier reported");
RecordedClass type = method.getType();
assertNotNull(type, "Recorded class can not be null");
}
}

}
}

}
@@ -36,7 +36,6 @@
import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;


/**
* @test
* @summary Reads the recorded file two times and verifies that both reads are the same
@@ -51,34 +50,34 @@
}

public static void main(String[] args) throws Throwable {
Recording r = new Recording();
r.enable(MyEvent.class).withoutStackTrace();
r.start();
try (Recording r = new Recording()) {
r.enable(MyEvent.class).withoutStackTrace();
r.start();

// Commit a single event to the recording
MyEvent event = new MyEvent();
event.commit();
// Commit a single event to the recording
MyEvent event = new MyEvent();
event.commit();

r.stop();
r.stop();

// Dump the recording to a file
Path path = Utils.createTempFile("read-twice", ".jfr");
System.out.println("Dumping to " + path);
r.dump(path);
r.close();
// Dump the recording to a file
Path path = Utils.createTempFile("read-twice", ".jfr");
System.out.println("Dumping to " + path);
r.dump(path);

// Read all events from the file in one go
List<RecordedEvent> events = RecordingFile.readAllEvents(path);
// Read all events from the file in one go
List<RecordedEvent> events = RecordingFile.readAllEvents(path);

// Read again the same events one by one
RecordingFile rfile = new RecordingFile(path);
List<RecordedEvent> events2 = new LinkedList<>();
while (rfile.hasMoreEvents()) {
events2.add(rfile.readEvent());
}
// Read again the same events one by one
try (RecordingFile rfile = new RecordingFile(path)) {
List<RecordedEvent> events2 = new LinkedList<>();
while (rfile.hasMoreEvents()) {
events2.add(rfile.readEvent());
}

// Compare sizes
Asserts.assertEquals(events.size(), events2.size());
rfile.close();
// Compare sizes
Asserts.assertEquals(events.size(), events2.size());
}
}
}
}

0 comments on commit f75f78a

Please sign in to comment.