From 64e12c675a3e94b5b31148982d35fac787765e1e Mon Sep 17 00:00:00 2001 From: Denghui Dong Date: Mon, 2 Mar 2020 10:28:11 +0800 Subject: [PATCH] [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 --- .../api/consumer/streaming/TestJVMCrash.java | 33 +++++++++-------- .../api/consumer/streaming/TestJVMExit.java | 23 ++++++------ .../streaming/TestOutOfProcessMigration.java | 37 ++++++++++--------- .../api/consumer/streaming/TestProcess.java | 20 +++++++++- 4 files changed, 67 insertions(+), 46 deletions(-) diff --git a/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java b/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java index da13840f8..0ca1fa164 100644 --- a/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java +++ b/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java @@ -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..."); } } } diff --git a/test/jdk/jfr/api/consumer/streaming/TestJVMExit.java b/test/jdk/jfr/api/consumer/streaming/TestJVMExit.java index 49e438a00..0319b5642 100644 --- a/test/jdk/jfr/api/consumer/streaming/TestJVMExit.java +++ b/test/jdk/jfr/api/consumer/streaming/TestJVMExit.java @@ -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(); + } } } } diff --git a/test/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java b/test/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java index 44638495a..56005cb5a 100644 --- a/test/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java +++ b/test/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java @@ -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(); + } } } } diff --git a/test/jdk/jfr/api/consumer/streaming/TestProcess.java b/test/jdk/jfr/api/consumer/streaming/TestProcess.java index 49f240542..dd4dd204a 100644 --- a/test/jdk/jfr/api/consumer/streaming/TestProcess.java +++ b/test/jdk/jfr/api/consumer/streaming/TestProcess.java @@ -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(); + } + } } -- GitLab