Skip to content

Commit 34f9146

Browse files
authored
Improve InputStreamIndexInput testSkipBytes (#118485) (#118522)
Relates ES-10234
1 parent f25b06d commit 34f9146

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

server/src/test/java/org/elasticsearch/common/lucene/store/InputStreamIndexInputTests.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.lucene.store.ByteBuffersDirectory;
1313
import org.apache.lucene.store.Directory;
14+
import org.apache.lucene.store.FilterIndexInput;
1415
import org.apache.lucene.store.IOContext;
1516
import org.apache.lucene.store.IndexInput;
1617
import org.apache.lucene.store.IndexOutput;
@@ -267,17 +268,47 @@ public void testSkipBytes() throws Exception {
267268
skipBytesExpected
268269
);
269270

270-
IndexInput input = dir.openInput("test", IOContext.DEFAULT);
271-
InputStreamIndexInput is = new InputStreamIndexInput(input, limit);
271+
var countingInput = new CountingReadBytesIndexInput("test", dir.openInput("test", IOContext.DEFAULT));
272+
InputStreamIndexInput is = new InputStreamIndexInput(countingInput, limit);
272273
is.readNBytes(initialReadBytes);
273274
assertThat(is.skip(skipBytes), equalTo((long) skipBytesExpected));
275+
long expectedActualInitialBytesRead = Math.min(Math.min(initialReadBytes, limit), bytes);
276+
assertThat(countingInput.getBytesRead(), equalTo(expectedActualInitialBytesRead));
274277

275278
int remainingBytes = Math.min(bytes, limit) - seekExpected;
276279
for (int i = seekExpected; i < seekExpected + remainingBytes; i++) {
277280
assertThat(is.read(), equalTo(i));
278281
}
282+
assertThat(countingInput.getBytesRead(), equalTo(expectedActualInitialBytesRead + remainingBytes));
279283
}
280284

285+
protected static class CountingReadBytesIndexInput extends FilterIndexInput {
286+
private long bytesRead = 0;
287+
288+
public CountingReadBytesIndexInput(String resourceDescription, IndexInput in) {
289+
super(resourceDescription, in);
290+
}
291+
292+
@Override
293+
public byte readByte() throws IOException {
294+
long filePointerBefore = getFilePointer();
295+
byte b = super.readByte();
296+
bytesRead += getFilePointer() - filePointerBefore;
297+
return b;
298+
}
299+
300+
@Override
301+
public void readBytes(byte[] b, int offset, int len) throws IOException {
302+
long filePointerBefore = getFilePointer();
303+
super.readBytes(b, offset, len);
304+
bytesRead += getFilePointer() - filePointerBefore;
305+
}
306+
307+
public long getBytesRead() {
308+
return bytesRead;
309+
}
310+
};
311+
281312
public void testReadZeroShouldReturnZero() throws IOException {
282313
try (Directory dir = new ByteBuffersDirectory()) {
283314
try (IndexOutput output = dir.createOutput("test", IOContext.DEFAULT)) {

0 commit comments

Comments
 (0)