From 0741a354c059251dc8e77b27cd20e1df4e915413 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 13 Nov 2022 15:34:10 +0100 Subject: [PATCH] Improve test introduced in #11918 to also check that reported invalid position is transformed back original position by slicing code (#11926) --- .../apache/lucene/store/TestMultiMMap.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lucene/core/src/test/org/apache/lucene/store/TestMultiMMap.java b/lucene/core/src/test/org/apache/lucene/store/TestMultiMMap.java index 9cdd6e2b54e..a0c5a29ccf8 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestMultiMMap.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestMultiMMap.java @@ -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))); } } }