16
16
*/
17
17
package org .apache .lucene .store ;
18
18
19
+ import java .io .EOFException ;
19
20
import java .io .IOException ;
20
21
import java .nio .file .Path ;
21
22
import java .util .List ;
23
+ import java .util .Locale ;
22
24
import org .apache .lucene .tests .store .BaseChunkedDirectoryTestCase ;
23
25
import org .apache .lucene .util .BytesRef ;
24
26
import org .junit .BeforeClass ;
@@ -41,22 +43,39 @@ public static void beforeClass() throws Exception {
41
43
assertTrue (MMapDirectory .UNMAP_NOT_SUPPORTED_REASON , MMapDirectory .UNMAP_SUPPORTED );
42
44
}
43
45
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 ;
46
50
try (IndexOutput out = dir .createOutput ("a" , IOContext .DEFAULT )) {
47
- for (int i = 0 ; i < 2048 ; ++i ) {
51
+ for (int i = 0 ; i < size ; ++i ) {
48
52
out .writeByte ((byte ) 0 );
49
53
}
50
54
}
51
55
try (IndexInput in = dir .openInput ("a" , IOContext .DEFAULT )) {
52
- in .seek (1234 );
53
- assertEquals (1234 , in .getFilePointer ());
56
+ final long negativePos = -1234 ;
54
57
var e =
55
58
expectThrowsAnyOf (
56
59
List .of (IllegalArgumentException .class , AssertionError .class ),
57
- () -> in .seek (- 1234 ));
60
+ () -> in .seek (negativePos ));
58
61
assertTrue (
59
62
"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 )));
60
79
}
61
80
}
62
81
}
0 commit comments