Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,16 @@ public static double getDiskPartitionSpaceUsedPercent(final String path) {

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) {
Expand Down
13 changes: 13 additions & 0 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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 {
Expand Down Expand Up @@ -78,6 +80,17 @@ public void test_getpid() {
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() {
{
Expand Down