This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5d492c3382 [ISSUE #7444] Fix testCalculateFileSizeInPath test can not 
rerun in same environment (#7445)
5d492c3382 is described below

commit 5d492c338258d07613103e6ae16df4c6fa5b3838
Author: rongtong <jinrongto...@163.com>
AuthorDate: Fri Oct 13 11:23:30 2023 +0800

    [ISSUE #7444] Fix testCalculateFileSizeInPath test can not rerun in same 
environment (#7445)
    
    * Fix testCalculateFileSizeInPath test can not rerun in same environment
    
    * Ensure that files are always deleted
---
 .../org/apache/rocketmq/common/UtilAllTest.java    | 83 +++++++++++++---------
 1 file changed, 48 insertions(+), 35 deletions(-)

diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java 
b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
index f568a65f4d..a0653d7fc4 100644
--- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
+++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
@@ -238,41 +238,54 @@ public class UtilAllTest {
          */
         String basePath = System.getProperty("java.io.tmpdir") + 
File.separator + "testCalculateFileSizeInPath";
         File baseFile = new File(basePath);
-        // test empty path
-        assertEquals(0, UtilAll.calculateFileSizeInPath(baseFile));
-
-        // create baseDir
-        assertTrue(baseFile.mkdirs());
-
-        File file0 = new File(baseFile, "file_0");
-        assertTrue(file0.createNewFile());
-        writeFixedBytesToFile(file0, 1313);
-
-        assertEquals(1313, UtilAll.calculateFileSizeInPath(baseFile));
-
-        // build a file tree like above
-        File dir1 = new File(baseFile, "dir_1");
-        dir1.mkdirs();
-        File file10 = new File(dir1, "file_1_0");
-        File file11 = new File(dir1, "file_1_1");
-        File dir12 = new File(dir1, "dir_1_2");
-        dir12.mkdirs();
-        File file120 = new File(dir12, "file_1_2_0");
-        File dir2 = new File(baseFile, "dir_2");
-        dir2.mkdirs();
-
-        // write all file with 1313 bytes data
-        assertTrue(file10.createNewFile());
-        writeFixedBytesToFile(file10, 1313);
-        assertTrue(file11.createNewFile());
-        writeFixedBytesToFile(file11, 1313);
-        assertTrue(file120.createNewFile());
-        writeFixedBytesToFile(file120, 1313);
-
-        assertEquals(1313 * 4, UtilAll.calculateFileSizeInPath(baseFile));
-
-        // clear all file
-        baseFile.deleteOnExit();
+        try {
+            // test empty path
+            assertEquals(0, UtilAll.calculateFileSizeInPath(baseFile));
+
+            // create baseDir
+            assertTrue(baseFile.mkdirs());
+
+            File file0 = new File(baseFile, "file_0");
+            assertTrue(file0.createNewFile());
+            writeFixedBytesToFile(file0, 1313);
+
+            assertEquals(1313, UtilAll.calculateFileSizeInPath(baseFile));
+
+            // build a file tree like above
+            File dir1 = new File(baseFile, "dir_1");
+            dir1.mkdirs();
+            File file10 = new File(dir1, "file_1_0");
+            File file11 = new File(dir1, "file_1_1");
+            File dir12 = new File(dir1, "dir_1_2");
+            dir12.mkdirs();
+            File file120 = new File(dir12, "file_1_2_0");
+            File dir2 = new File(baseFile, "dir_2");
+            dir2.mkdirs();
+
+            // write all file with 1313 bytes data
+            assertTrue(file10.createNewFile());
+            writeFixedBytesToFile(file10, 1313);
+            assertTrue(file11.createNewFile());
+            writeFixedBytesToFile(file11, 1313);
+            assertTrue(file120.createNewFile());
+            writeFixedBytesToFile(file120, 1313);
+
+            assertEquals(1313 * 4, UtilAll.calculateFileSizeInPath(baseFile));
+        } finally {
+            deleteFolder(baseFile);
+        }
+    }
+
+    public static void deleteFolder(File folder) {
+        if (folder.isDirectory()) {
+            File[] files = folder.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    deleteFolder(file);
+                }
+            }
+        }
+        folder.delete();
     }
 
     private void writeFixedBytesToFile(File file, int size) throws Exception {

Reply via email to