提交 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; ...@@ -41,26 +41,27 @@ import jdk.jfr.consumer.EventStream;
*/ */
public class TestJVMCrash { public class TestJVMCrash {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
int id = 1; int id = 1;
while (true) { while (true) {
TestProcess process = new TestProcess("crash-application-" + id++); try (TestProcess process = new TestProcess("crash-application-" + id++)) {
AtomicInteger eventCounter = new AtomicInteger(); AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) { try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository // Start from first event in repository
es.setStartTime(Instant.EPOCH); es.setStartTime(Instant.EPOCH);
es.onEvent(e -> { es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) { if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.crash(); 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;
} }
}); System.out.println("Incorrect event count. Retrying...");
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...");
} }
} }
} }
......
...@@ -41,17 +41,18 @@ import jdk.jfr.consumer.EventStream; ...@@ -41,17 +41,18 @@ import jdk.jfr.consumer.EventStream;
public class TestJVMExit { public class TestJVMExit {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
TestProcess process = new TestProcess("exit-application"); try (TestProcess process = new TestProcess("exit-application")) {
AtomicInteger eventCounter = new AtomicInteger(); AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) { try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository // Start from first event in repository
es.setStartTime(Instant.EPOCH); es.setStartTime(Instant.EPOCH);
es.onEvent(e -> { es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) { if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.exit(); process.exit();
} }
}); });
es.start(); es.start();
}
} }
} }
} }
...@@ -33,7 +33,6 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -33,7 +33,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import jdk.jfr.consumer.EventStream; import jdk.jfr.consumer.EventStream;
import jdk.test.lib.dcmd.CommandExecutor; import jdk.test.lib.dcmd.CommandExecutor;
import jdk.test.lib.dcmd.PidJcmdExecutor; import jdk.test.lib.dcmd.PidJcmdExecutor;
import jdk.test.lib.process.OutputAnalyzer;
/** /**
* @test * @test
...@@ -46,23 +45,25 @@ import jdk.test.lib.process.OutputAnalyzer; ...@@ -46,23 +45,25 @@ import jdk.test.lib.process.OutputAnalyzer;
*/ */
public class TestOutOfProcessMigration { public class TestOutOfProcessMigration {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
Path newRepo = Paths.get("new-repository").toAbsolutePath(); try (TestProcess process = new TestProcess("application")) {
AtomicInteger eventCounter = new AtomicInteger();
TestProcess process = new TestProcess("application"); Path newRepo = Paths.get("new-repository").toAbsolutePath();
AtomicInteger eventCounter = new AtomicInteger(); try (EventStream es = EventStream.openRepository(process.getRepository())) {
try (EventStream es = EventStream.openRepository(process.getRepository())) { // Start from first event in repository
// Start from first event in repository es.setStartTime(Instant.EPOCH);
es.setStartTime(Instant.EPOCH); es.onEvent(e -> {
es.onEvent(e -> { if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) { System.out.println("Changing repository to " + newRepo + " ...");
System.out.println("Changing repository to " + newRepo + " ..."); CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid()));
CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid())); // This should close stream
// This should close stream executor.execute("JFR.configure repositorypath=" + newRepo);
OutputAnalyzer oa = executor.execute("JFR.configure repositorypath=" + newRepo); }
System.out.println(oa); });
} es.start();
}); process.exit();
es.start(); // Wait for process to die, so files are cleaned up
process.awaitDeath();
}
} }
} }
} }
...@@ -33,6 +33,7 @@ import java.io.FileOutputStream; ...@@ -33,6 +33,7 @@ import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Properties; import java.util.Properties;
...@@ -49,7 +50,7 @@ import com.sun.tools.attach.VirtualMachine; ...@@ -49,7 +50,7 @@ import com.sun.tools.attach.VirtualMachine;
* Requires jdk.attach module. * Requires jdk.attach module.
* *
*/ */
public final class TestProcess { public final class TestProcess implements AutoCloseable {
private static class TestEvent extends Event { private static class TestEvent extends Event {
} }
...@@ -173,4 +174,21 @@ public final class TestProcess { ...@@ -173,4 +174,21 @@ public final class TestProcess {
public long pid() { public long pid() {
return 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.
先完成此消息的编辑!
想要评论请 注册