8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
authormseledtsov
Tue, 04 Jun 2019 07:53:12 -0700
changeset 55199 3a09397c147e
parent 55197 0ca8e495bbba
child 55200 9c36f33b0b01
8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp Summary: Using test utils to create temp files and directories Reviewed-by: dholmes, erikj, hseigel, lmesnik
test/jdk/jdk/jfr/event/io/EvilInstrument.java
test/jdk/jdk/jfr/event/io/TestDisabledEvents.java
test/jdk/jdk/jfr/event/io/TestFileChannelEvents.java
test/jdk/jdk/jfr/event/io/TestFileReadOnly.java
test/jdk/jdk/jfr/event/io/TestFileStreamEvents.java
test/jdk/jdk/jfr/event/io/TestRandomAccessFileEvents.java
test/jdk/jdk/jfr/event/io/TestRandomAccessFileThread.java
test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java
test/jdk/jdk/jfr/jmx/JmxHelper.java
test/jdk/jdk/jfr/jvm/TestJavaEvent.java
test/lib/jdk/test/lib/Utils.java
--- a/test/jdk/jdk/jfr/event/io/EvilInstrument.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/EvilInstrument.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
 import java.net.Socket;
 import java.security.ProtectionDomain;
 import java.util.concurrent.CountDownLatch;
+import jdk.test.lib.Utils;
 
 
 /**
@@ -87,7 +88,7 @@
     }
 
     public static File createScratchFile() throws IOException {
-        return File.createTempFile("EvilTransformer", null, new File(".")).getAbsoluteFile();
+        return Utils.createTempFile("EvilTransformer", null).toFile();
     }
 
     class EvilTransformer implements ClassFileTransformer {
--- a/test/jdk/jdk/jfr/event/io/TestDisabledEvents.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestDisabledEvents.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,7 @@
 
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 
 /**
@@ -55,8 +56,7 @@
     private static final byte[] writeBuf = { 'B', 'C', 'D' };
 
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestDisabledEvents", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestDisabledEvents", ".tmp").toFile();
         Recording recording = new Recording();
         recording.disable(IOEvent.EVENT_FILE_READ);
         recording.disable(IOEvent.EVENT_FILE_WRITE);
--- a/test/jdk/jdk/jfr/event/io/TestFileChannelEvents.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestFileChannelEvents.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,7 @@
 
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 
 /**
@@ -48,8 +49,7 @@
  */
 public class TestFileChannelEvents {
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestFileChannelEvents", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestFileChannelEvents", ".tmp").toFile();
         Recording recording = new Recording();
         List<IOEvent> expectedEvents = new ArrayList<>();
 
--- a/test/jdk/jdk/jfr/event/io/TestFileReadOnly.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestFileReadOnly.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,7 @@
 
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 
 /**
@@ -50,8 +51,7 @@
 public class TestFileReadOnly {
 
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestFileReadOnly", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestFileReadOnly", ".tmp").toFile();
         Recording recording = new Recording();
         List<IOEvent> expectedEvents = new ArrayList<>();
 
--- a/test/jdk/jdk/jfr/event/io/TestFileStreamEvents.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestFileStreamEvents.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@
 
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 
 /**
@@ -48,8 +49,7 @@
 
 public class TestFileStreamEvents {
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestFileStreamEvents", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestFileStreamEvents", ".tmp").toFile();
         Recording recording = new Recording();
         List<IOEvent> expectedEvents = new ArrayList<>();
 
--- a/test/jdk/jdk/jfr/event/io/TestRandomAccessFileEvents.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestRandomAccessFileEvents.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
 
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 
 /**
@@ -47,8 +48,7 @@
 public class TestRandomAccessFileEvents {
 
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestRandomAccessFileEvents", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestRandomAccessFileEvents", ".tmp").toFile();
         Recording recording = new Recording();
         List<IOEvent> expectedEvents = new ArrayList<>();
 
--- a/test/jdk/jdk/jfr/event/io/TestRandomAccessFileThread.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/event/io/TestRandomAccessFileThread.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,7 @@
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
 import jdk.test.lib.Asserts;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.Events;
 import jdk.test.lib.thread.TestThread;
 import jdk.test.lib.thread.XRun;
@@ -62,8 +63,7 @@
     private static volatile int writeCount = 0; // Number of writes executed.
 
     public static void main(String[] args) throws Throwable {
-        File tmp = File.createTempFile("TestRandomAccessFileThread", ".tmp", new File("."));
-        tmp.deleteOnExit();
+        File tmp = Utils.createTempFile("TestRandomAccessFileThread", ".tmp").toFile();
 
         Recording recording = new Recording();
         recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
--- a/test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
 
 import jdk.jfr.internal.Options;
 import jdk.test.lib.Asserts;
+import jdk.test.lib.Utils;
 
 /**
  * @test
@@ -62,7 +63,7 @@
         // - where feasible, check if they are respected
         //
 
-        String dumpPath = Files.createTempDirectory("dump-path").toAbsolutePath().toString();
+        String dumpPath = Utils.createTempDirectory("dump-path-").toAbsolutePath().toString();
 
         test(DUMPPATH, dumpPath);
         test(STACK_DEPTH, 15);
--- a/test/jdk/jdk/jfr/jmx/JmxHelper.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/jmx/JmxHelper.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,6 +48,7 @@
 import jdk.management.jfr.RecordingInfo;
 import jdk.management.jfr.SettingDescriptorInfo;
 import jdk.test.lib.Asserts;
+import jdk.test.lib.Utils;
 import jdk.test.lib.jfr.CommonHelper;
 import jdk.test.lib.jfr.Events;
 
@@ -127,7 +128,7 @@
     }
 
     static File dump(long streamId, FlightRecorderMXBean bean) throws IOException {
-        File f = File.createTempFile("stream_" + streamId + "_", ".jfr", new File("."));
+        File f = Utils.createTempFile("stream_" + streamId + "_", ".jfr").toFile();
         try (FileOutputStream fos = new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
             while (true) {
                 byte[] data = bean.readStream(streamId);
--- a/test/jdk/jdk/jfr/jvm/TestJavaEvent.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/jdk/jdk/jfr/jvm/TestJavaEvent.java	Tue Jun 04 07:53:12 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,7 @@
 import jdk.jfr.ValueDescriptor;
 import jdk.jfr.consumer.RecordedEvent;
 import jdk.jfr.consumer.RecordingFile;
+import jdk.test.lib.Utils;
 
 /**
  * @test TestGetThreadId
@@ -117,7 +118,7 @@
 
         r.stop();
         // prettyPrint();
-        File file = File.createTempFile("test", ".jfr");
+        File file = Utils.createTempFile("test", ".jfr").toFile();
         r.dump(file.toPath());
         int eventCount = 0;
         for (RecordedEvent e : RecordingFile.readAllEvents(file.toPath())) {
--- a/test/lib/jdk/test/lib/Utils.java	Tue Jun 04 16:33:37 2019 +0200
+++ b/test/lib/jdk/test/lib/Utils.java	Tue Jun 04 07:53:12 2019 -0700
@@ -815,4 +815,24 @@
         Path dir = Paths.get(System.getProperty("user.dir", "."));
         return Files.createTempFile(dir, prefix, suffix);
     }
+
+    /**
+     * Creates an empty directory in "user.dir" or "."
+     * <p>
+     * This method is meant as a replacement for {@code Files#createTempDirectory(String, String, FileAttribute...)}
+     * that doesn't leave files behind in /tmp directory of the test machine
+     * <p>
+     * If the property "user.dir" is not set, "." will be used.
+     *
+     * @param prefix
+     * @param attrs
+     * @return the path to the newly created directory
+     * @throws IOException
+     *
+     * @see {@link Files#createTempDirectory(String, String, FileAttribute...)}
+     */
+    public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException {
+        Path dir = Paths.get(System.getProperty("user.dir", "."));
+        return Files.createTempDirectory(dir, prefix);
+    }
 }