Skip to content

Commit

Permalink
Fix unstable UtilAllTest#testCalculateFileSizeInPath on Windows
Browse files Browse the repository at this point in the history
On my Windows laptop, UtilAllTest#testCalculateFileSizeInPath is
unstable, presumably due to leftovers from previous runs that were not
properly cleanup up.

Converting the test to use a proper TemporaryFolder @rule like other
tests that rely on the filesystem instead of trying to implement the
cleanup logic in the test itself seems to solve this.

Closes #7418
  • Loading branch information
mureinik committed Oct 1, 2023
1 parent 4f1b42a commit 076e62b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class UtilAllTest {
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();

@Test
public void testCurrentStackTrace() {
Expand Down Expand Up @@ -236,14 +241,10 @@ public void testCalculateFileSizeInPath() throws Exception {
* - file_1_2_0
* - dir_2
*/
String basePath = System.getProperty("java.io.tmpdir") + File.separator + "testCalculateFileSizeInPath";
File baseFile = new File(basePath);
File baseFile = tempDir.getRoot();
// 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);
Expand All @@ -270,9 +271,6 @@ public void testCalculateFileSizeInPath() throws Exception {
writeFixedBytesToFile(file120, 1313);

assertEquals(1313 * 4, UtilAll.calculateFileSizeInPath(baseFile));

// clear all file
baseFile.deleteOnExit();
}

private void writeFixedBytesToFile(File file, int size) throws Exception {
Expand Down

0 comments on commit 076e62b

Please sign in to comment.