Skip to content

Commit 221e079

Browse files
committed
Additional tests
1 parent 5d2cbad commit 221e079

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

server/src/test/java/org/elasticsearch/common/metrics/ExponentialBucketHistogramTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import org.elasticsearch.core.Strings;
1313
import org.elasticsearch.test.ESTestCase;
1414

15+
import java.util.Arrays;
16+
1517
import static org.hamcrest.Matchers.equalTo;
1618
import static org.hamcrest.Matchers.greaterThan;
1719
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
20+
import static org.junit.Assert.assertThrows;
1821

1922
public class ExponentialBucketHistogramTests extends ESTestCase {
2023

@@ -108,4 +111,29 @@ public void testMaxPercentile() {
108111
histogram.addObservation(secondToLastBucketUpperBound + 1);
109112
assertThat(histogram.getPercentile(1.0f), equalTo(Long.MAX_VALUE));
110113
}
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+
}
111139
}

0 commit comments

Comments
 (0)