8234703: JFR TestOutOfProcessMigration.java should clean up files
authoregahlin
Mon, 25 Nov 2019 14:06:13 +0100
changeset 59246 fcad92f425c5
parent 59245 de882051f7a5
child 59247 56bf71d64d51
8234703: JFR TestOutOfProcessMigration.java should clean up files Reviewed-by: mgronlun
test/jdk/jdk/jfr/api/consumer/streaming/TestJVMCrash.java
test/jdk/jdk/jfr/api/consumer/streaming/TestJVMExit.java
test/jdk/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java
test/jdk/jdk/jfr/api/consumer/streaming/TestProcess.java
--- a/test/jdk/jdk/jfr/api/consumer/streaming/TestJVMCrash.java	Mon Nov 25 12:34:43 2019 +0100
+++ b/test/jdk/jdk/jfr/api/consumer/streaming/TestJVMCrash.java	Mon Nov 25 14:06:13 2019 +0100
@@ -42,26 +42,27 @@
  */
 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...");
             }
         }
     }
--- a/test/jdk/jdk/jfr/api/consumer/streaming/TestJVMExit.java	Mon Nov 25 12:34:43 2019 +0100
+++ b/test/jdk/jdk/jfr/api/consumer/streaming/TestJVMExit.java	Mon Nov 25 14:06:13 2019 +0100
@@ -42,17 +42,18 @@
 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();
+            }
         }
     }
 }
--- a/test/jdk/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java	Mon Nov 25 12:34:43 2019 +0100
+++ b/test/jdk/jdk/jfr/api/consumer/streaming/TestOutOfProcessMigration.java	Mon Nov 25 14:06:13 2019 +0100
@@ -33,7 +33,6 @@
 import jdk.jfr.consumer.EventStream;
 import jdk.test.lib.dcmd.CommandExecutor;
 import jdk.test.lib.dcmd.PidJcmdExecutor;
-import jdk.test.lib.process.OutputAnalyzer;
 
 /**
  * @test
@@ -47,23 +46,25 @@
  */
 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();
+            }
         }
     }
 }
--- a/test/jdk/jdk/jfr/api/consumer/streaming/TestProcess.java	Mon Nov 25 12:34:43 2019 +0100
+++ b/test/jdk/jdk/jfr/api/consumer/streaming/TestProcess.java	Mon Nov 25 14:06:13 2019 +0100
@@ -27,6 +27,7 @@
 import java.io.IOException;
 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;
@@ -43,7 +44,7 @@
  * Requires jdk.attach module.
  *
  */
-public final class TestProcess {
+public final class TestProcess implements AutoCloseable {
 
     private static class TestEvent extends Event {
     }
@@ -135,4 +136,21 @@
     public long pid() {
         return process.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();
+        }
+    }
 }