Skip to content

Commit 25e45a7

Browse files
authored
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 286f4ce commit 25e45a7

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
@@ -223,25 +223,36 @@ protected Reader<ByteSizeValue> instanceReader() {
223223
}
224224

225225
@Override
226-
protected ByteSizeValue mutateInstance(ByteSizeValue instance) throws IOException {
227-
long size = instance.getSize();
228-
ByteSizeUnit unit = instance.getUnit();
226+
protected ByteSizeValue mutateInstance(final ByteSizeValue instance) {
227+
final long instanceSize = instance.getSize();
228+
final ByteSizeUnit instanceUnit = instance.getUnit();
229+
final long mutateSize;
230+
final ByteSizeUnit mutateUnit;
229231
switch (between(0, 1)) {
230232
case 0:
231-
long unitBytes = unit.toBytes(1);
232-
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / unitBytes);
233+
final long unitBytes = instanceUnit.toBytes(1);
234+
mutateSize = randomValueOtherThan(instanceSize, () -> randomNonNegativeLong() / unitBytes);
235+
mutateUnit = instanceUnit;
233236
break;
234237
case 1:
235-
unit = randomValueOtherThan(unit, () -> randomFrom(ByteSizeUnit.values()));
236-
long newUnitBytes = unit.toBytes(1);
237-
if (size >= Long.MAX_VALUE / newUnitBytes) {
238-
size = randomValueOtherThan(size, () -> randomNonNegativeLong() / newUnitBytes);
238+
mutateUnit = randomValueOtherThan(instanceUnit, () -> randomFrom(ByteSizeUnit.values()));
239+
final long newUnitBytes = mutateUnit.toBytes(1);
240+
/*
241+
* 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
242+
* need to randomize a new size. Additionally, if the size unit pair is such that the representation would be such that the
243+
* number of represented bytes would exceed Long.Max_VALUE, we have to randomize a new size too.
244+
*/
245+
if (instanceSize == 0 || instanceSize >= Long.MAX_VALUE / newUnitBytes) {
246+
mutateSize = randomValueOtherThanMany(
247+
v -> v == instanceSize && v >= Long.MAX_VALUE / newUnitBytes, () -> randomNonNegativeLong() / newUnitBytes);
248+
} else {
249+
mutateSize = instanceSize;
239250
}
240251
break;
241252
default:
242253
throw new AssertionError("Invalid randomisation branch");
243254
}
244-
return new ByteSizeValue(size, unit);
255+
return new ByteSizeValue(mutateSize, mutateUnit);
245256
}
246257

247258
public void testParse() {

0 commit comments

Comments
 (0)