|
14 | 14 | public class BlobStoreActionStatsTests extends ESTestCase { |
15 | 15 |
|
16 | 16 | public void testAdd() { |
17 | | - final BlobStoreActionStats lhs = randomEndpointStats(1 << 30); |
18 | | - final BlobStoreActionStats rhs = randomEndpointStats(1 << 30); |
| 17 | + final BlobStoreActionStats lhs = randomBlobStoreActionStats(1 << 30); |
| 18 | + final BlobStoreActionStats rhs = randomBlobStoreActionStats(1 << 30); |
19 | 19 | final BlobStoreActionStats result = lhs.add(rhs); |
20 | 20 | assertEquals(lhs.operations() + rhs.operations(), result.operations()); |
21 | 21 | assertEquals(lhs.requests() + rhs.requests(), result.requests()); |
22 | 22 | } |
23 | 23 |
|
24 | 24 | public void testAddOverflow() { |
25 | | - final BlobStoreActionStats lhs = new BlobStoreActionStats(randomLongBetween(50, 1 << 30), randomLongBetween(50, 1 << 30)); |
26 | | - final int fieldToOverflow = randomIntBetween(0, 1); |
27 | | - final BlobStoreActionStats rhs = new BlobStoreActionStats( |
28 | | - fieldToOverflow == 0 ? (Long.MAX_VALUE - lhs.operations()) + 1 : 1, |
29 | | - fieldToOverflow == 1 ? (Long.MAX_VALUE - lhs.requests()) + 1 : 1 |
30 | | - ); |
| 25 | + final BlobStoreActionStats lhs = randomBlobStoreActionStats(50, 1 << 30); |
| 26 | + // We can only overflow requests, or both values (we can't just overflow operations because requests >= operations |
| 27 | + final boolean overflowRequestsOnly = randomBoolean(); |
| 28 | + final long valueToCauseOverflow = (Long.MAX_VALUE - lhs.operations()) + 1; |
| 29 | + final long operationsValue = overflowRequestsOnly ? 1 : valueToCauseOverflow; |
| 30 | + final BlobStoreActionStats rhs = new BlobStoreActionStats(operationsValue, valueToCauseOverflow); |
31 | 31 | assertThrows(ArithmeticException.class, () -> lhs.add(rhs)); |
32 | 32 | } |
33 | 33 |
|
34 | 34 | public void testIsZero() { |
35 | 35 | assertTrue(new BlobStoreActionStats(0, 0).isZero()); |
36 | | - assertFalse(new BlobStoreActionStats(randomLongBetween(1, Long.MAX_VALUE), 0).isZero()); |
37 | 36 | assertFalse(new BlobStoreActionStats(0, randomLongBetween(1, Long.MAX_VALUE)).isZero()); |
| 37 | + assertFalse(randomBlobStoreActionStats(1, Long.MAX_VALUE).isZero()); |
38 | 38 | } |
39 | 39 |
|
40 | | - private BlobStoreActionStats randomEndpointStats() { |
41 | | - return randomEndpointStats(Long.MAX_VALUE); |
| 40 | + private BlobStoreActionStats randomBlobStoreActionStats(long upperBound) { |
| 41 | + return randomBlobStoreActionStats(0, upperBound); |
42 | 42 | } |
43 | 43 |
|
44 | | - private BlobStoreActionStats randomEndpointStats(long upperBound) { |
45 | | - return new BlobStoreActionStats(randomLongBetween(0, upperBound), randomLongBetween(0, upperBound)); |
| 44 | + private BlobStoreActionStats randomBlobStoreActionStats(long lowerBound, long upperBound) { |
| 45 | + assert upperBound >= lowerBound; |
| 46 | + long operations = randomLongBetween(lowerBound, upperBound); |
| 47 | + long requests = randomLongBetween(operations, upperBound); |
| 48 | + return new BlobStoreActionStats(operations, requests); |
46 | 49 | } |
47 | 50 | } |
0 commit comments