Skip to content
Permalink
Browse files
8223396: [TESTBUG] several jfr tests do not clean up files created in…
… /tmp

Using test utils to create temp files and directories

Reviewed-by: sgehwolf
Backport-of: 7d3aebc
  • Loading branch information
zhengyu123 committed Mar 17, 2022
1 parent 9543488 commit 613554035cd50eb4cb056b015c4243b9329e8b16
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 21 deletions.
@@ -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 void run() {
}

public static File createScratchFile() throws IOException {
return File.createTempFile("EvilTransformer", null, new File(".")).getAbsoluteFile();
return Utils.createTempFile("EvilTransformer", null).toFile();
}

class EvilTransformer implements ClassFileTransformer {
@@ -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 @@ public class TestDisabledEvents {
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();
try (Recording recording = new Recording()) {
recording.disable(IOEvent.EVENT_FILE_READ);
recording.disable(IOEvent.EVENT_FILE_WRITE);
@@ -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();
try (Recording recording = new Recording()) {
List<IOEvent> expectedEvents = new ArrayList<>();
try (RandomAccessFile rf = new RandomAccessFile(tmp, "rw"); FileChannel ch = rf.getChannel();) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, 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();
try (Recording recording = new Recording()) {
List<IOEvent> expectedEvents = new ArrayList<>();

@@ -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();
try (Recording recording = new Recording()) {
List<IOEvent> expectedEvents = new ArrayList<>();
try(FileOutputStream fos = new FileOutputStream(tmp); FileInputStream fis = new FileInputStream(tmp);) {
@@ -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();
try (Recording recording = new Recording()) {
List<IOEvent> expectedEvents = new ArrayList<>();

@@ -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,8 @@ public class TestRandomAccessFileThread {
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();

try (Recording recording = new Recording()) {
recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0));
@@ -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 @@ public static void main(String[] args) throws Exception {
// - 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);
@@ -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
@@ -49,6 +49,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;

@@ -136,7 +137,7 @@ public static void logRecordings(List<Recording> recordings) {
}

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);
@@ -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 @@ public static void main(String... args) throws IOException, InterruptedException

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())) {
@@ -762,4 +762,24 @@ public static Path createTempFile(String prefix, String suffix, FileAttribute<?>
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);
}
}

1 comment on commit 6135540

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 6135540 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.