提交 64e12c67 编写于 作者: D Denghui Dong 提交者: D-D-H

[Backport] 8234703: JFR TestOutOfProcessMigration.java should clean up files

Summary:

Test Plan: jdk/jfr

Reviewed-by: yuleil

Issue: https://github.com/alibaba/dragonwell8/issues/112
上级 bc43f3fe
......@@ -41,26 +41,27 @@ import jdk.jfr.consumer.EventStream;
*/
public class TestJVMCrash {
public static void main(String... args) throws Exception {
public static void main(String... args) throws Exception {
int id = 1;
while (true) {
TestProcess process = new TestProcess("crash-application-" + id++);
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.crash();
try (TestProcess process = new TestProcess("crash-application-" + id++)) {
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.crash();
}
});
es.startAsync();
// If crash corrupts chunk in repository, retry in 30 seconds
es.awaitTermination(Duration.ofSeconds(30));
if (eventCounter.get() == TestProcess.NUMBER_OF_EVENTS) {
return;
}
});
es.startAsync();
// If crash corrupts chunk in repository, retry in 30 seconds
es.awaitTermination(Duration.ofSeconds(30));
if (eventCounter.get() == TestProcess.NUMBER_OF_EVENTS) {
return;
System.out.println("Incorrect event count. Retrying...");
}
System.out.println("Incorrect event count. Retrying...");
}
}
}
......
......@@ -41,17 +41,18 @@ import jdk.jfr.consumer.EventStream;
public class TestJVMExit {
public static void main(String... args) throws Exception {
TestProcess process = new TestProcess("exit-application");
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.exit();
}
});
es.start();
try (TestProcess process = new TestProcess("exit-application")) {
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.exit();
}
});
es.start();
}
}
}
}
......@@ -33,7 +33,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import jdk.jfr.consumer.EventStream;
import jdk.test.lib.dcmd.CommandExecutor;
import jdk.test.lib.dcmd.PidJcmdExecutor;
import jdk.test.lib.process.OutputAnalyzer;
/**
* @test
......@@ -46,23 +45,25 @@ import jdk.test.lib.process.OutputAnalyzer;
*/
public class TestOutOfProcessMigration {
public static void main(String... args) throws Exception {
Path newRepo = Paths.get("new-repository").toAbsolutePath();
TestProcess process = new TestProcess("application");
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
System.out.println("Changing repository to " + newRepo + " ...");
CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid()));
// This should close stream
OutputAnalyzer oa = executor.execute("JFR.configure repositorypath=" + newRepo);
System.out.println(oa);
}
});
es.start();
try (TestProcess process = new TestProcess("application")) {
AtomicInteger eventCounter = new AtomicInteger();
Path newRepo = Paths.get("new-repository").toAbsolutePath();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
System.out.println("Changing repository to " + newRepo + " ...");
CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid()));
// This should close stream
executor.execute("JFR.configure repositorypath=" + newRepo);
}
});
es.start();
process.exit();
// Wait for process to die, so files are cleaned up
process.awaitDeath();
}
}
}
}
......@@ -33,6 +33,7 @@ import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
......@@ -49,7 +50,7 @@ import com.sun.tools.attach.VirtualMachine;
* Requires jdk.attach module.
*
*/
public final class TestProcess {
public final class TestProcess implements AutoCloseable {
private static class TestEvent extends Event {
}
......@@ -173,4 +174,21 @@ public final class TestProcess {
public long pid() {
return pid;
}
@Override
public void close() throws Exception {
try {
if (path != null) {
Files.delete(path);
}
} catch(NoSuchFileException nfe) {
// ignore
}
}
public void awaitDeath() {
while (process.isAlive()) {
takeNap();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册