|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertFalse; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.mockStatic; |
24 | 27 |
|
25 | 28 | import java.io.File; |
| 29 | +import java.nio.file.FileSystemException; |
| 30 | +import java.nio.file.OpenOption; |
| 31 | +import org.apache.ratis.util.FileUtils; |
26 | 32 | import org.junit.jupiter.api.Test; |
27 | 33 | import org.junit.jupiter.api.io.TempDir; |
| 34 | +import org.junit.jupiter.api.parallel.Execution; |
| 35 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 36 | +import org.mockito.MockedStatic; |
28 | 37 |
|
29 | 38 | /** |
30 | 39 | * Tests {@link DiskCheckUtil} does not incorrectly identify an unhealthy |
31 | 40 | * disk or mount point. |
32 | 41 | * Tests that it identifies an improperly configured directory mount point. |
33 | 42 | * |
34 | 43 | */ |
| 44 | +@Execution(ExecutionMode.SAME_THREAD) |
35 | 45 | public class TestDiskCheckUtil { |
36 | 46 |
|
37 | 47 | @TempDir |
@@ -80,4 +90,21 @@ public void testReadWrite() { |
80 | 90 | assertNotNull(children); |
81 | 91 | assertEquals(0, children.length); |
82 | 92 | } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testCheckReadWriteDiskFull() { |
| 96 | + try (MockedStatic<FileUtils> mockService = mockStatic(FileUtils.class)) { |
| 97 | + // fos.write(writtenBytes) also through FileSystemException with the message |
| 98 | + mockService.when(() -> FileUtils.newOutputStreamForceAtClose(any(File.class), any(OpenOption[].class))) |
| 99 | + .thenThrow(new FileSystemException("No space left on device")); |
| 100 | + |
| 101 | + String path = testDir.getAbsolutePath(); |
| 102 | + assertThrows(FileSystemException.class, |
| 103 | + () -> FileUtils.newOutputStreamForceAtClose(new File(path), new OpenOption[2])); |
| 104 | + |
| 105 | + // Test that checkReadWrite returns true for the disk full case |
| 106 | + boolean result = DiskCheckUtil.checkReadWrite(testDir, testDir, 1024); |
| 107 | + assertTrue(result, "checkReadWrite should return true when disk is full"); |
| 108 | + } |
| 109 | + } |
83 | 110 | } |
0 commit comments