Skip to content

Commit f4bc112

Browse files
committed
Improve test introduced in #11918 to also check that reported invalid position is transformed back original position by slicing code (#11926)
1 parent 8fc62ec commit f4bc112

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lucene/core/src/test/org/apache/lucene/store/TestMultiMMap.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717
package org.apache.lucene.store;
1818

19+
import java.io.EOFException;
1920
import java.io.IOException;
2021
import java.nio.file.Path;
2122
import java.util.List;
23+
import java.util.Locale;
2224
import org.apache.lucene.tests.store.BaseChunkedDirectoryTestCase;
2325
import org.apache.lucene.util.BytesRef;
2426
import org.junit.BeforeClass;
@@ -41,22 +43,39 @@ public static void beforeClass() throws Exception {
4143
assertTrue(MMapDirectory.UNMAP_NOT_SUPPORTED_REASON, MMapDirectory.UNMAP_SUPPORTED);
4244
}
4345

44-
public void testSeekNegative() throws IOException {
45-
try (Directory dir = getDirectory(createTempDir())) {
46+
public void testSeekingExceptions() throws IOException {
47+
final int sliceSize = 128;
48+
try (Directory dir = getDirectory(createTempDir(), sliceSize)) {
49+
final int size = 128 + 63;
4650
try (IndexOutput out = dir.createOutput("a", IOContext.DEFAULT)) {
47-
for (int i = 0; i < 2048; ++i) {
51+
for (int i = 0; i < size; ++i) {
4852
out.writeByte((byte) 0);
4953
}
5054
}
5155
try (IndexInput in = dir.openInput("a", IOContext.DEFAULT)) {
52-
in.seek(1234);
53-
assertEquals(1234, in.getFilePointer());
56+
final long negativePos = -1234;
5457
var e =
5558
expectThrowsAnyOf(
5659
List.of(IllegalArgumentException.class, AssertionError.class),
57-
() -> in.seek(-1234));
60+
() -> in.seek(negativePos));
5861
assertTrue(
5962
"does not mention negative position", e.getMessage().contains("negative position"));
63+
64+
final long posAfterEOF = size + 123;
65+
var eof = expectThrows(EOFException.class, () -> in.seek(posAfterEOF));
66+
assertTrue(
67+
"wrong position in error message: " + eof,
68+
eof.getMessage().contains(String.format(Locale.ROOT, "(pos=%d)", posAfterEOF)));
69+
70+
// this test verifies that the invalid position is transformed back to original one for
71+
// exception by slicing:
72+
IndexInput slice = in.slice("slice", 33, sliceSize + 15);
73+
// ensure that the slice uses multi-mmap:
74+
assertCorrectImpl(false, slice);
75+
eof = expectThrows(EOFException.class, () -> slice.seek(posAfterEOF));
76+
assertTrue(
77+
"wrong position in error message: " + eof,
78+
eof.getMessage().contains(String.format(Locale.ROOT, "(pos=%d)", posAfterEOF)));
6079
}
6180
}
6281
}

0 commit comments

Comments
 (0)