Skip to content

Commit b6058b2

Browse files
committed
Fix mutateInstance
1 parent ffa7476 commit b6058b2

File tree

1 file changed

+4
-31
lines changed

1 file changed

+4
-31
lines changed

server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.ElasticsearchParseException;
1313
import org.elasticsearch.common.io.stream.Writeable.Reader;
1414
import org.elasticsearch.test.AbstractWireSerializingTestCase;
15+
import org.elasticsearch.test.ESTestCase;
1516
import org.hamcrest.MatcherAssert;
1617

1718
import java.io.IOException;
@@ -263,37 +264,9 @@ protected Reader<ByteSizeValue> instanceReader() {
263264

264265
@Override
265266
protected ByteSizeValue mutateInstance(final ByteSizeValue instance) {
266-
final long originalBytes = instance.sizeInBytes;
267-
final ByteSizeUnit originalUnit = instance.preferredUnit;
268-
final long mutateSize;
269-
final ByteSizeUnit mutateUnit;
270-
switch (between(0, 1)) {
271-
case 0 -> {
272-
final long unitBytes = originalUnit.toBytes(1);
273-
mutateSize = randomValueOtherThan(originalBytes, () -> randomNonNegativeLong() / unitBytes);
274-
mutateUnit = originalUnit;
275-
}
276-
case 1 -> {
277-
mutateUnit = randomValueOtherThan(originalUnit, () -> randomFrom(ByteSizeUnit.values()));
278-
final long newUnitBytes = mutateUnit.toBytes(1);
279-
/*
280-
* If size is zero we can not reuse zero because zero with any unit will be equal to zero with any other
281-
* unit so in this case we need to randomize a new size. Additionally, if the size unit pair is such that
282-
* the representation would be such that the number of represented bytes would exceed Long.Max_VALUE, we
283-
* have to randomize a new size too.
284-
*/
285-
if (originalBytes == 0 || originalBytes >= Long.MAX_VALUE / newUnitBytes) {
286-
mutateSize = randomValueOtherThanMany(
287-
v -> v == originalBytes && v >= Long.MAX_VALUE / newUnitBytes,
288-
() -> randomNonNegativeLong() / newUnitBytes
289-
);
290-
} else {
291-
mutateSize = originalBytes;
292-
}
293-
}
294-
default -> throw new AssertionError("Invalid randomisation branch");
295-
}
296-
return ByteSizeValue.of(mutateSize, mutateUnit);
267+
// There's just one way to create an unequal ByteSizeValue: change the size in bytes.
268+
// Changing only the unit does not make the ByteSizeValue unequal.
269+
return new ByteSizeValue(randomValueOtherThan(instance.sizeInBytes, ESTestCase::randomNonNegativeLong), instance.preferredUnit);
297270
}
298271

299272
public void testParse() {

0 commit comments

Comments
 (0)