-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Add BytesStreamOutputTests #134788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joshua-adams-1
merged 8 commits into
elastic:main
from
joshua-adams-1:bytes-stream-output-tests
Oct 2, 2025
Merged
Add BytesStreamOutputTests #134788
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfae38d
Add BytesStreamOutputTests
joshua-adams-1 fa82411
Extend skip and seek tests
joshua-adams-1 a9249b9
Remove testSeekAndSkip
joshua-adams-1 8949103
[CI] Update transport version definitions
53119fd
[CI] Auto commit changes from spotless
a9358db
Merge branch 'main' into bytes-stream-output-tests
joshua-adams-1 210508b
Merge branch 'main' into bytes-stream-output-tests
joshua-adams-1 53a66a0
Merge branch 'main' into bytes-stream-output-tests
joshua-adams-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
179 changes: 179 additions & 0 deletions
179
server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamOutputTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.common.io.stream; | ||
|
||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.junit.Before; | ||
|
||
public class BytesStreamOutputTests extends ESTestCase { | ||
|
||
private BytesStreamOutput stream; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
stream = new BytesStreamOutput(); | ||
} | ||
|
||
public void testDefaultConstructor() { | ||
assertEquals(0, stream.size()); | ||
assertEquals(0, stream.position()); | ||
assertEquals(BytesArray.EMPTY, stream.bytes()); | ||
} | ||
|
||
public void testConstructorWithExpectedSize() { | ||
BytesStreamOutput s = new BytesStreamOutput(randomNonNegativeInt()); | ||
assertEquals(0, s.size()); | ||
assertEquals(0, s.position()); | ||
} | ||
|
||
public void testWriteByte() { | ||
byte i1 = randomByte(); | ||
byte i2 = randomByte(); | ||
stream.writeByte(i1); | ||
stream.writeByte(i2); | ||
assertEquals(2, stream.size()); | ||
assertEquals(2, stream.position()); | ||
assertEquals(i1, stream.bytes().get(0)); | ||
assertEquals(i2, stream.bytes().get(1)); | ||
} | ||
|
||
public void testWriteBytes() { | ||
int maxSize = randomIntBetween(10, 100); | ||
byte[] data = randomByteArrayOfLength(maxSize); | ||
int length = randomIntBetween(0, maxSize); | ||
stream.writeBytes(data, 0, length); | ||
assertEquals(length, stream.size()); | ||
for (int i = 0; i < length; i++) { | ||
assertEquals(data[i], stream.bytes().get(i)); | ||
} | ||
} | ||
|
||
public void testWriteBytesWithOffset() { | ||
int maxSize = randomIntBetween(10, 100); | ||
byte[] data = randomByteArrayOfLength(maxSize); | ||
int offset = randomIntBetween(0, maxSize); | ||
int length = randomIntBetween(0, maxSize - offset); | ||
stream.writeBytes(data, offset, length); | ||
assertEquals(length, stream.size()); | ||
for (int i = 0; i < stream.size(); i++) { | ||
assertEquals(stream.bytes().get(i), data[i + offset]); | ||
} | ||
} | ||
|
||
public void testWriteBytesWithLengthGreaterThanData() { | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int size = randomIntBetween(0, 100); | ||
assertThrows(IndexOutOfBoundsException.class, () -> s.writeBytes(randomByteArrayOfLength(size), 0, size + 1)); | ||
} | ||
|
||
public void testResetShrinksAndResetsCount() { | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
stream.writeBytes(randomByteArrayOfLength(byteArrayLength), 0, byteArrayLength); | ||
assertEquals(byteArrayLength, stream.size()); | ||
stream.reset(); | ||
assertEquals(0, stream.size()); | ||
// After reset, should be able to write again | ||
byte b = randomByte(); | ||
stream.writeByte(b); | ||
assertEquals(1, stream.size()); | ||
assertEquals(b, stream.bytes().get(0)); | ||
} | ||
|
||
public void testSeek() { | ||
// Do some writing | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
s.writeBytes(byteArray, 0, byteArrayLength); | ||
|
||
// Seek either over what we've already written, or what we have yet to write | ||
int seekPosition = randomIntBetween(0, 200); | ||
s.seek(seekPosition); | ||
assertEquals(seekPosition, s.size()); | ||
} | ||
|
||
public void testSeekAtBoundaries() { | ||
// Do some arbitrary writing | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
s.writeBytes(byteArray, 0, byteArrayLength); | ||
|
||
s.seek(0); | ||
assertEquals(0, s.size()); | ||
s.seek(Integer.MAX_VALUE); | ||
assertEquals(Integer.MAX_VALUE, s.size()); | ||
} | ||
|
||
public void testSeekWithOverflow() { | ||
// Do some arbitrary writing and then overflow | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
s.writeBytes(byteArray, 0, byteArrayLength); | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> s.seek(randomLongBetween((long) Integer.MAX_VALUE + 1, (long) Integer.MAX_VALUE + 100)) | ||
); | ||
} | ||
|
||
public void testSkip() { | ||
// Do some arbitrary writing | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
s.writeBytes(byteArray, 0, byteArrayLength); | ||
|
||
int skipValue = randomIntBetween(0, 100); | ||
s.skip(skipValue); | ||
assertEquals(byteArrayLength + skipValue, s.size()); | ||
} | ||
|
||
public void testSkipAtBoundaries() { | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
s.skip(0); | ||
assertEquals(0, s.size()); | ||
s.skip(Integer.MAX_VALUE); | ||
assertEquals(Integer.MAX_VALUE, s.size()); | ||
} | ||
|
||
public void testSkipWithOverflow() { | ||
// Do some arbitrary writing and then overflow | ||
BytesStreamOutput s = new BytesStreamOutput(0, BigArrays.NON_RECYCLING_INSTANCE); | ||
int byteArrayLength = randomIntBetween(1, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
s.writeBytes(byteArray, 0, byteArrayLength); | ||
|
||
int skipPosition = randomIntBetween(Integer.MAX_VALUE - byteArrayLength + 1, Integer.MAX_VALUE); | ||
assertThrows(IllegalArgumentException.class, () -> s.skip(skipPosition)); | ||
} | ||
|
||
public void testCopyBytes() { | ||
int byteArrayLength = randomIntBetween(0, 100); | ||
byte[] byteArray = randomByteArrayOfLength(byteArrayLength); | ||
stream.writeBytes(byteArray, 0, byteArrayLength); | ||
BytesReference copy = stream.copyBytes(); | ||
assertEquals(byteArrayLength, copy.length()); | ||
for (int i = 0; i < byteArrayLength; i++) { | ||
assertEquals(byteArray[i], copy.get(i)); | ||
} | ||
} | ||
|
||
public void testFlushAndCloseNoop() { | ||
stream.writeByte(randomByte()); | ||
stream.flush(); // should do nothing | ||
stream.close(); // should do nothing | ||
assertEquals(1, stream.size()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.