diff --git a/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java b/test/jdk/jfr/api/consumer/streaming/TestJVMCrash.java index da13840f84ef62c29169ade8afdc5383a7d53662..0ca1fa1643813458dc0cd562a29d074462b69c0c 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 49e438a009ee588379319d2db1285fafdc96d72b..0319b5642abe5f23ba0275e1a7e71bf88f51f204 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 44638495a0d483397f6a209bf68479dd429b6a3e..56005cb5a9f01b25b3e6a11e4910d26eb2112fe8 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 49f2405426d201d2be7856b2452107687bb979cf..dd4dd204a1a21c2c75ff5f414fbb3ff60e22f2b9 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(); + } + } }