Improve test introduced in #11918 to also check that reported invalid position is transformed back original position by slicing code (#11926)

This commit is contained in:
Uwe Schindler 2022-11-13 15:34:10 +01:00 committed by GitHub
parent 98b26e0885
commit 0741a354c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 6 deletions

View File

@ -16,9 +16,11 @@
*/
package org.apache.lucene.store;
import java.io.EOFException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import org.apache.lucene.tests.store.BaseChunkedDirectoryTestCase;
import org.apache.lucene.util.BytesRef;
import org.junit.BeforeClass;
@ -41,22 +43,39 @@ public class TestMultiMMap extends BaseChunkedDirectoryTestCase {
assertTrue(MMapDirectory.UNMAP_NOT_SUPPORTED_REASON, MMapDirectory.UNMAP_SUPPORTED);
}
public void testSeekNegative() throws IOException {
try (Directory dir = getDirectory(createTempDir())) {
public void testSeekingExceptions() throws IOException {
final int sliceSize = 128;
try (Directory dir = getDirectory(createTempDir(), sliceSize)) {
final int size = 128 + 63;
try (IndexOutput out = dir.createOutput("a", IOContext.DEFAULT)) {
for (int i = 0; i < 2048; ++i) {
for (int i = 0; i < size; ++i) {
out.writeByte((byte) 0);
}
}
try (IndexInput in = dir.openInput("a", IOContext.DEFAULT)) {
in.seek(1234);
assertEquals(1234, in.getFilePointer());
final long negativePos = -1234;
var e =
expectThrowsAnyOf(
List.of(IllegalArgumentException.class, AssertionError.class),
() -> in.seek(-1234));
() -> in.seek(negativePos));
assertTrue(
"does not mention negative position", e.getMessage().contains("negative position"));
final long posAfterEOF = size + 123;
var eof = expectThrows(EOFException.class, () -> in.seek(posAfterEOF));
assertTrue(
"wrong position in error message: " + eof,
eof.getMessage().contains(String.format(Locale.ROOT, "(pos=%d)", posAfterEOF)));
// this test verifies that the invalid position is transformed back to original one for
// exception by slicing:
IndexInput slice = in.slice("slice", 33, sliceSize + 15);
// ensure that the slice uses multi-mmap:
assertCorrectImpl(false, slice);
eof = expectThrows(EOFException.class, () -> slice.seek(posAfterEOF));
assertTrue(
"wrong position in error message: " + eof,
eof.getMessage().contains(String.format(Locale.ROOT, "(pos=%d)", posAfterEOF)));
}
}
}