diff --git a/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java b/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java index 52cca6e467338..784bc25df849f 100644 --- a/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java +++ b/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java @@ -232,25 +232,36 @@ protected Reader instanceReader() { } @Override - protected ByteSizeValue mutateInstance(ByteSizeValue instance) throws IOException { - long size = instance.getSize(); - ByteSizeUnit unit = instance.getUnit(); + protected ByteSizeValue mutateInstance(final ByteSizeValue instance) { + final long instanceSize = instance.getSize(); + final ByteSizeUnit instanceUnit = instance.getUnit(); + final long mutateSize; + final ByteSizeUnit mutateUnit; switch (between(0, 1)) { case 0: - long unitBytes = unit.toBytes(1); - size = randomValueOtherThan(size, () -> randomNonNegativeLong() / unitBytes); + final long unitBytes = instanceUnit.toBytes(1); + mutateSize = randomValueOtherThan(instanceSize, () -> randomNonNegativeLong() / unitBytes); + mutateUnit = instanceUnit; break; case 1: - unit = randomValueOtherThan(unit, () -> randomFrom(ByteSizeUnit.values())); - long newUnitBytes = unit.toBytes(1); - if (size >= Long.MAX_VALUE / newUnitBytes) { - size = randomValueOtherThan(size, () -> randomNonNegativeLong() / newUnitBytes); + mutateUnit = randomValueOtherThan(instanceUnit, () -> randomFrom(ByteSizeUnit.values())); + final long newUnitBytes = mutateUnit.toBytes(1); + /* + * 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 + * need to randomize a new size. Additionally, if the size unit pair is such that the representation would be such that the + * number of represented bytes would exceed Long.Max_VALUE, we have to randomize a new size too. + */ + if (instanceSize == 0 || instanceSize >= Long.MAX_VALUE / newUnitBytes) { + mutateSize = randomValueOtherThanMany( + v -> v == instanceSize && v >= Long.MAX_VALUE / newUnitBytes, () -> randomNonNegativeLong() / newUnitBytes); + } else { + mutateSize = instanceSize; } break; default: throw new AssertionError("Invalid randomisation branch"); } - return new ByteSizeValue(size, unit); + return new ByteSizeValue(mutateSize, mutateUnit); } public void testParse() {