-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MyPerf4J-90] Fix SysGenProfilingFile rename fail #90 #91
base: develop
Are you sure you want to change the base?
Changes from all commits
c5cab11
c41c1aa
8cf7cd1
3f2ba93
17c9823
b8ba3b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package cn.myperf4j.core; | ||
|
||
import cn.myperf4j.base.util.Logger; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Objects; | ||
|
||
/** | ||
* 文件测试 | ||
*/ | ||
public class FileTest { | ||
|
||
private static final File baseDir = new File(System.getProperty("user.dir")); | ||
|
||
private static final File testDir = new File(baseDir, "temp"); | ||
|
||
@BeforeClass | ||
public static void prepare() { | ||
getDir(FileTest.class.getSimpleName()); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() { | ||
deleteDir(FileTest.class.getSimpleName()); | ||
} | ||
|
||
LinShunKang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Test | ||
public void testFileRename() { | ||
String testFile = getTestDir().getPath() + "/testFileRename"; | ||
final Path testFilePath1 = Paths.get(testFile + 1); | ||
final Path testFilePath2 = Paths.get(testFile + 2); | ||
final Path tmpFilePath = Paths.get(testFile + "_tmp"); | ||
|
||
for (int i = 0; i < 2; i++) { | ||
try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(tmpFilePath.toFile(), false), 8192)) { | ||
fileWriter.write("#This is a file automatically generated by MyPerf4J, please do not edit!\n"); | ||
fileWriter.flush(); | ||
// 流还没释放导致 | ||
Assert.assertFalse(tmpFilePath.toFile().renameTo(testFilePath1.toFile())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 MacOS 里这行代码执行失败 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你之前的代码在 MacOS 里能成功吗? |
||
Assert.assertFalse(testFilePath1.toFile().setReadOnly()); | ||
} catch (Exception e) { | ||
Logger.error("testFileRename error", e); | ||
} | ||
|
||
if (i == 0) { | ||
// 第一次rename能成功 文件存在的情况setReadOnly始终能成功 | ||
Assert.assertTrue(tmpFilePath.toFile().renameTo(testFilePath2.toFile())); | ||
Assert.assertTrue(testFilePath2.toFile().setReadOnly()); | ||
} else { | ||
// 第一次rename能成功,后续就失败 | ||
Assert.assertFalse(tmpFilePath.toFile().renameTo(testFilePath2.toFile())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 MacOS 里这行代码执行失败 |
||
Assert.assertTrue(testFilePath2.toFile().setReadOnly()); | ||
} | ||
} | ||
// tmp文件存在 | ||
Assert.assertTrue(tmpFilePath.toFile().exists()); | ||
// BufferedWriter没释放时rename的文件不存在 | ||
Assert.assertFalse(testFilePath1.toFile().exists()); | ||
// BufferedWriter释放后rename的文件存在 | ||
Assert.assertTrue(testFilePath2.toFile().exists()); | ||
} | ||
|
||
@Test | ||
public void testFilesMove() throws IOException { | ||
String testFile = getTestDir().getPath() + "/testFilesMove"; | ||
final Path testFilePath = Paths.get(testFile); | ||
final Path tmpFilePath = Paths.get(testFile + "_tmp"); | ||
if (!tmpFilePath.toFile().exists()) { | ||
Files.createFile(tmpFilePath); | ||
tmpFilePath.toFile().setReadOnly(); | ||
} | ||
Assert.assertFalse(tmpFilePath.toFile().canWrite()); | ||
LinShunKang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final Path moved = Files.move(tmpFilePath, testFilePath, StandardCopyOption.REPLACE_EXISTING); | ||
moved.toFile().setReadOnly(); | ||
Assert.assertFalse(moved.toFile().canWrite()); | ||
|
||
Files.move(moved, tmpFilePath, StandardCopyOption.REPLACE_EXISTING); | ||
Assert.assertFalse(tmpFilePath.toFile().canWrite()); | ||
} | ||
|
||
private File getTestDir() { | ||
return getDir(FileTest.class.getSimpleName()); | ||
} | ||
|
||
private static File getDir(String dir) { | ||
File file = new File(testDir, dir); | ||
if (!file.isDirectory()) { | ||
if (!file.mkdirs()) { | ||
throw new IllegalStateException("Can't create dir: " + file); | ||
} | ||
} | ||
return file; | ||
} | ||
|
||
private static void deleteDir(String dir) { | ||
deleteDir(testDir, dir); | ||
} | ||
|
||
private static void deleteDir(final File parent, String dir) { | ||
final File dirFile = new File(parent, dir); | ||
if (dirFile.isDirectory()) { | ||
for (final File f: Objects.requireNonNull(dirFile.listFiles())) { | ||
if (f.isDirectory()) { | ||
deleteDir(dirFile, f.getName()); | ||
continue; | ||
} | ||
LinShunKang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (f.isFile()) { | ||
if (!f.delete()) { | ||
throw new IllegalStateException("Can't delete file: " + f); | ||
} | ||
Logger.info("Delete file: " + f); | ||
} | ||
} | ||
|
||
if (!dirFile.delete()) { | ||
throw new IllegalStateException("Can't delete directory: " + dirFile); | ||
} | ||
Logger.info("Delete directory: " + dirFile); | ||
} | ||
|
||
testDir.deleteOnExit(); | ||
Logger.info("Delete testDir: " + testDir); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果 destFile.setReadOnly() 之后再次 move 会报错的话,那 FileTest 的第 89 行就会抛异常
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不一样,FileTest 能跑成功,但是 agent 加载到 tomcat 里面就会报错