|
16 | 16 | */
|
17 | 17 | package org.apache.lucene.misc.store;
|
18 | 18 |
|
| 19 | +import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 20 | +import java.io.EOFException; |
19 | 21 | import java.io.IOException;
|
20 | 22 | import java.nio.file.Files;
|
21 | 23 | import java.nio.file.Path;
|
@@ -77,10 +79,68 @@ public void testIllegalEOFWithFileSizeMultipleOfBlockSize() throws Exception {
|
77 | 79 | o.close();
|
78 | 80 | IndexInput i = dir.openInput("out", newIOContext(random()));
|
79 | 81 | i.seek(fileSize);
|
| 82 | + |
| 83 | + // Seeking past EOF should always throw EOFException |
| 84 | + expectThrows( |
| 85 | + EOFException.class, () -> i.seek(fileSize + RandomizedTest.randomIntBetween(1, 2048))); |
| 86 | + |
| 87 | + // Reading immediately after seeking past EOF should throw EOFException |
| 88 | + expectThrows(EOFException.class, () -> i.readByte()); |
80 | 89 | i.close();
|
81 | 90 | }
|
82 | 91 | }
|
83 | 92 |
|
| 93 | + public void testReadPastEOFShouldThrowEOFExceptionWithEmptyFile() throws Exception { |
| 94 | + // fileSize needs to be 0 to test this condition. Do not randomized. |
| 95 | + final int fileSize = 0; |
| 96 | + try (Directory dir = getDirectory(createTempDir("testReadPastEOF"))) { |
| 97 | + try (IndexOutput o = dir.createOutput("out", newIOContext(random()))) { |
| 98 | + o.writeBytes(new byte[fileSize], 0, fileSize); |
| 99 | + } |
| 100 | + |
| 101 | + try (IndexInput i = dir.openInput("out", newIOContext(random()))) { |
| 102 | + i.seek(fileSize); |
| 103 | + expectThrows(EOFException.class, () -> i.readByte()); |
| 104 | + expectThrows(EOFException.class, () -> i.readBytes(new byte[1], 0, 1)); |
| 105 | + } |
| 106 | + |
| 107 | + try (IndexInput i = dir.openInput("out", newIOContext(random()))) { |
| 108 | + expectThrows( |
| 109 | + EOFException.class, () -> i.seek(fileSize + RandomizedTest.randomIntBetween(1, 2048))); |
| 110 | + expectThrows(EOFException.class, () -> i.readByte()); |
| 111 | + expectThrows(EOFException.class, () -> i.readBytes(new byte[1], 0, 1)); |
| 112 | + } |
| 113 | + |
| 114 | + try (IndexInput i = dir.openInput("out", newIOContext(random()))) { |
| 115 | + expectThrows(EOFException.class, () -> i.readByte()); |
| 116 | + } |
| 117 | + |
| 118 | + try (IndexInput i = dir.openInput("out", newIOContext(random()))) { |
| 119 | + expectThrows(EOFException.class, () -> i.readBytes(new byte[1], 0, 1)); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void testSeekPastEOFAndRead() throws Exception { |
| 125 | + try (Directory dir = getDirectory(createTempDir("testSeekPastEOF"))) { |
| 126 | + final int len = random().nextInt(2048); |
| 127 | + |
| 128 | + try (IndexOutput o = dir.createOutput("out", newIOContext(random()))) { |
| 129 | + byte[] b = new byte[len]; |
| 130 | + o.writeBytes(b, 0, len); |
| 131 | + } |
| 132 | + |
| 133 | + try (IndexInput i = dir.openInput("out", newIOContext(random()))) { |
| 134 | + // Seeking past EOF should always throw EOFException |
| 135 | + expectThrows( |
| 136 | + EOFException.class, () -> i.seek(len + RandomizedTest.randomIntBetween(1, 2048))); |
| 137 | + |
| 138 | + // Reading immediately after seeking past EOF should throw EOFException |
| 139 | + expectThrows(EOFException.class, () -> i.readByte()); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
84 | 144 | public void testUseDirectIODefaults() throws Exception {
|
85 | 145 | Path path = createTempDir("testUseDirectIODefaults");
|
86 | 146 | try (DirectIODirectory dir = new DirectIODirectory(FSDirectory.open(path))) {
|
|
0 commit comments