Skip to content

Commit

Permalink
Add functional test for java.lang.Thread instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Nov 11, 2024
1 parent d745a29 commit 6291a2c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
43 changes: 43 additions & 0 deletions integration-tests/src/test/btrace/ThreadStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the Classpath exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package traces;

import org.openjdk.btrace.core.annotations.BTrace;
import org.openjdk.btrace.core.annotations.OnMethod;
import org.openjdk.btrace.core.annotations.Self;

import static org.openjdk.btrace.core.BTraceUtils.*;

@BTrace
public class ThreadStart {
@OnMethod(
clazz = "java.lang.Thread",
method = "start"
)
public static void onnewThread(@Self Thread t) {
println("starting " + Threads.name(t));
}
}
6 changes: 3 additions & 3 deletions integration-tests/src/test/java/resources/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
public class Main extends TestApp {
private String id = "xxx";
private String field;
private static String sField;
private String field;
private static String sField;

public static void main(String[] args) throws Exception {
Main i = new Main();
Expand All @@ -51,7 +51,7 @@ protected void startWork() {

private void callA() {
field = "AAA";
sField = "BBB";print("i=" + callB(1, "Hello World"));
sField = "BBB";print("i=" + callB(1, "Hello World"));
}

private int callB(int i, String s) {
Expand Down
32 changes: 32 additions & 0 deletions integration-tests/src/test/java/resources/ThreadSpawner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package resources;

public class ThreadSpawner extends TestApp {
public static void main(String[] args) throws Exception {
ThreadSpawner i = new ThreadSpawner();
i.start();
}
@Override
protected void startWork() {
while (!Thread.currentThread().isInterrupted()) {
try {
spawnThread();
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

private void spawnThread() throws InterruptedException {
Thread t = new Thread( () -> print("thread started"));
t.setName("testThread");
t.start();
t.join();
}

@Override
public void print(String msg) {
System.out.println(msg);
System.out.flush();
}
}
40 changes: 38 additions & 2 deletions integration-tests/src/test/java/tests/BTraceFunctionalTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* A set of end-to-end functional tests.
Expand Down Expand Up @@ -176,7 +179,6 @@ public void validate(String stdout, String stderr, int retcode, String jfrFile)

@Test
public void testTraceAll() throws Exception {
String rtVersion = System.getProperty("java.runtime.version", "");
String testJavaHome = System.getenv().get("TEST_JAVA_HOME");
if (testJavaHome == null) {
testJavaHome = System.getenv("JAVA_HOME");
Expand All @@ -190,7 +192,7 @@ public void testTraceAll() throws Exception {
Properties releaseProps = new Properties();
releaseProps.load(
Files.newInputStream(new File(testJavaHome + File.separator + "release").toPath()));
rtVersion = releaseProps.getProperty("JAVA_VERSION").replace("\"", "");
String rtVersion = releaseProps.getProperty("JAVA_VERSION").replace("\"", "");
if (!isVersionSafeForTraceAll(rtVersion)) {
System.err.println("Skipping test for JDK " + rtVersion);
return;
Expand Down Expand Up @@ -497,6 +499,40 @@ public boolean onStderr(int lineno, String line) {
});
}

@ParameterizedTest(name = "testThreadStart: dynamic={0}")
@ValueSource(booleans = {true, false})
public void testThreadStart(boolean dynamic) throws Exception {
if (dynamic) {
testDynamic(
"resources.ThreadSpawner",
"traces/ThreadStart.class",
null,
10,
new ResultValidator() {
@Override
public void validate(String stdout, String stderr, int retcode, String jfrFile) {
assertFalse(stdout.contains("FAILED"), "Script should not have failed");
assertTrue(stderr.isEmpty(), "Non-empty stderr");
assertTrue(stdout.contains("starting testThread"));
}
});
} else {
testStartup(
"resources.ThreadSpawner",
"traces/ThreadStart.class",
null,
10,
new ResultValidator() {
@Override
public void validate(String stdout, String stderr, int retcode, String jfrFile) {
assertFalse(stdout.contains("FAILED"), "Script should not have failed");
assertTrue(stderr.isEmpty(), "Non-empty stderr");
assertTrue(stdout.contains("starting testThread"));
}
});
}
}

private static boolean isVersionSafeForJfr(String rtVersion) {
System.out.println("===> version: " + rtVersion);
String[] versionParts = rtVersion.split("\\+")[0].split("\\.");
Expand Down

0 comments on commit 6291a2c

Please sign in to comment.