diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index 56015b36d56a5ebbfda2901ad446b0918b938e66..016da0b44c542575c957e2e30760438e71b41b2f 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -183,17 +183,16 @@ public class UtilAll { try { File file = new File(path); - if (!file.exists()) { - boolean result = file.mkdirs(); - if (!result) { - //TO DO - } - } + + if (!file.exists()) + return -1; long totalSpace = file.getTotalSpace(); - long freeSpace = file.getFreeSpace(); - long usedSpace = totalSpace - freeSpace; + if (totalSpace > 0) { + long freeSpace = file.getFreeSpace(); + long usedSpace = totalSpace - freeSpace; + return usedSpace / (double) totalSpace; } } catch (Exception e) { 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 8d8cf79600b583ba782d8d93bf9f274a48f847c7..0db84fe461f4c378dff4bf6b5e00d6cef3cd59d1 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -21,6 +21,8 @@ import java.net.URL; import java.util.Properties; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; public class UtilAllTest { @@ -78,6 +80,17 @@ public class UtilAllTest { assertTrue(pid > 0); } + @Test + public void test_getDiskPartitionSpaceUsedPercent() { + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(null), 0); + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(""), 0); + + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath"), 0); + + String tmpDir = System.getProperty("java.io.tmpdir"); + assertNotEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir), 0); + } + @Test public void test_isBlank() { {