Skip to content

Commit 42bcb5a

Browse files
committed
Fix byte size value equals/hash code test (#29643)
This commit fixes two issues with the byte size value equals/hash code test. The first problem is due to a test failure when the original instance is zero bytes and we pick the mutation branch where we preserve the size but change the unit. The mutation should result in a different byte size value but changing the unit on zero bytes still leaves us with zero bytes. During the course of fixing this test I discovered another problem. When we need to randomize size, we could randomly select a size that would lead to an overflow of Long.MAX_VALUE. This commit fixes both of these issues.
1 parent edf37ec commit 42bcb5a

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,36 @@ protected Reader<ByteSizeValue> instanceReader() {
232232
}
233233

234234
@Override
235-
protected ByteSizeValue mutateInstance(ByteSizeValue instance) throws IOException {
236-
long size = instance.getSize();
237-
ByteSizeUnit unit = instance.getUnit();
235+
protected ByteSizeValue mutateInstance(final ByteSizeValue instance) {
236+
final long instanceSize = instance.getSize();
237+
final ByteSizeUnit instanceUnit = instance.getUnit();
238+
final long mutateSize;
239+
final ByteSizeUnit mutateUnit;
238240
switch (between(0, 1)) {
239241
case 0:
240-
long unitBytes = unit.toBytes(1);
241-
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / unitBytes);
242+
final long unitBytes = instanceUnit.toBytes(1);
243+
mutateSize = randomValueOtherThan(instanceSize, () -> randomNonNegativeLong() / unitBytes);
244+
mutateUnit = instanceUnit;
242245
break;
243246
case 1:
244-
unit = randomValueOtherThan(unit, () -> randomFrom(ByteSizeUnit.values()));
245-
long newUnitBytes = unit.toBytes(1);
246-
if (size >= Long.MAX_VALUE / newUnitBytes) {
247-
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / newUnitBytes);
247+
mutateUnit = randomValueOtherThan(instanceUnit, () -> randomFrom(ByteSizeUnit.values()));
248+
final long newUnitBytes = mutateUnit.toBytes(1);
249+
/*
250+
* If size is zero we can not reuse zero because zero with any unit will be equal to zero with any other unit so in this case we
251+
* need to randomize a new size. Additionally, if the size unit pair is such that the representation would be such that the
252+
* number of represented bytes would exceed Long.Max_VALUE, we have to randomize a new size too.
253+
*/
254+
if (instanceSize == 0 || instanceSize >= Long.MAX_VALUE / newUnitBytes) {
255+
mutateSize = randomValueOtherThanMany(
256+
v -> v == instanceSize && v >= Long.MAX_VALUE / newUnitBytes, () -> randomNonNegativeLong() / newUnitBytes);
257+
} else {
258+
mutateSize = instanceSize;
248259
}
249260
break;
250261
default:
251262
throw new AssertionError("Invalid randomisation branch");
252263
}
253-
return new ByteSizeValue(size, unit);
264+
return new ByteSizeValue(mutateSize, mutateUnit);
254265
}
255266

256267
public void testParse() {

0 commit comments

Comments
 (0)