|
12 | 12 | import org.elasticsearch.core.Strings; |
13 | 13 | import org.elasticsearch.test.ESTestCase; |
14 | 14 |
|
| 15 | +import java.util.Arrays; |
| 16 | + |
15 | 17 | import static org.hamcrest.Matchers.equalTo; |
16 | 18 | import static org.hamcrest.Matchers.greaterThan; |
17 | 19 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 20 | +import static org.junit.Assert.assertThrows; |
18 | 21 |
|
19 | 22 | public class ExponentialBucketHistogramTests extends ESTestCase { |
20 | 23 |
|
@@ -108,4 +111,29 @@ public void testMaxPercentile() { |
108 | 111 | histogram.addObservation(secondToLastBucketUpperBound + 1); |
109 | 112 | assertThat(histogram.getPercentile(1.0f), equalTo(Long.MAX_VALUE)); |
110 | 113 | } |
| 114 | + |
| 115 | + public void testClear() { |
| 116 | + ExponentialBucketHistogram histogram = new ExponentialBucketHistogram(); |
| 117 | + for (int i = 0; i < 100; i++) { |
| 118 | + histogram.addObservation(randomIntBetween(1, 100_000)); |
| 119 | + } |
| 120 | + assertThat(Arrays.stream(histogram.getSnapshot()).sum(), greaterThan(0L)); |
| 121 | + histogram.clear(); |
| 122 | + assertThat(Arrays.stream(histogram.getSnapshot()).sum(), equalTo(0L)); |
| 123 | + } |
| 124 | + |
| 125 | + public void testPercentileValidation() { |
| 126 | + ExponentialBucketHistogram histogram = new ExponentialBucketHistogram(); |
| 127 | + // valid values |
| 128 | + histogram.getPercentile(randomFloatBetween(0.0f, 1.0f, true)); |
| 129 | + // invalid values |
| 130 | + assertThrows( |
| 131 | + IllegalArgumentException.class, |
| 132 | + () -> histogram.getPercentile(randomFloatBetween(Float.NEGATIVE_INFINITY, 0.0f, false)) |
| 133 | + ); |
| 134 | + assertThrows( |
| 135 | + IllegalArgumentException.class, |
| 136 | + () -> histogram.getPercentile(randomFloatBetween(1.0f, Float.POSITIVE_INFINITY, false)) |
| 137 | + ); |
| 138 | + } |
111 | 139 | } |
0 commit comments