|
12 | 12 | import org.elasticsearch.ElasticsearchParseException; |
13 | 13 | import org.elasticsearch.common.io.stream.Writeable.Reader; |
14 | 14 | import org.elasticsearch.test.AbstractWireSerializingTestCase; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
15 | 16 | import org.hamcrest.MatcherAssert; |
16 | 17 |
|
17 | 18 | import java.io.IOException; |
@@ -263,37 +264,9 @@ protected Reader<ByteSizeValue> instanceReader() { |
263 | 264 |
|
264 | 265 | @Override |
265 | 266 | 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); |
297 | 270 | } |
298 | 271 |
|
299 | 272 | public void testParse() { |
|
0 commit comments