Skip to content

Commit a9315e4

Browse files
authored
Fixing SamplingServiceSampleStatsTests.testToXContent (elastic#139146)
SamplingServiceSampleStatsTests.testToXContent generates data with random long values. If a value is small enough to fit in an integer, then XContentParser::map (through jackson) will return the value as an Integer rather than a Long, causing the test to fail. This test converts whatever it gets back to a long. Closes elastic#138841
1 parent 026b5c9 commit a9315e4

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

server/src/test/java/org/elasticsearch/ingest/SamplingServiceSampleStatsTests.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
2626
import static org.hamcrest.Matchers.equalTo;
27+
import static org.hamcrest.Matchers.instanceOf;
2728
import static org.junit.Assert.assertNotSame;
2829

2930
public class SamplingServiceSampleStatsTests extends AbstractWireSerializingTestCase<SampleStats> {
@@ -148,13 +149,19 @@ public void testToXContent() throws IOException {
148149
parserMap.get("samples_rejected_for_max_samples_exceeded"),
149150
equalTo(sampleStats.getSamplesRejectedForMaxSamplesExceeded())
150151
);
151-
assertThat(parserMap.get("samples_rejected_for_condition"), equalTo(sampleStats.getSamplesRejectedForCondition()));
152-
assertThat(parserMap.get("samples_rejected_for_rate"), equalTo(sampleStats.getSamplesRejectedForRate()));
153-
assertThat(parserMap.get("samples_rejected_for_exception"), equalTo(sampleStats.getSamplesRejectedForException()));
154-
assertThat(parserMap.get("samples_rejected_for_size"), equalTo(sampleStats.getSamplesRejectedForSize()));
155-
assertThat(parserMap.get("samples_accepted"), equalTo(sampleStats.getSamples()));
156-
assertThat(parserMap.get("time_sampling_millis"), equalTo(sampleStats.getTimeSampling().millis()));
157-
assertThat(parserMap.get("time_compiling_condition_millis"), equalTo(sampleStats.getTimeCompilingCondition().millis()));
152+
assertNumberEqualsLong(parserMap.get("samples_rejected_for_condition"), sampleStats.getSamplesRejectedForCondition());
153+
assertNumberEqualsLong(parserMap.get("samples_rejected_for_rate"), sampleStats.getSamplesRejectedForRate());
154+
assertNumberEqualsLong(parserMap.get("samples_rejected_for_exception"), sampleStats.getSamplesRejectedForException());
155+
assertNumberEqualsLong(parserMap.get("samples_rejected_for_size"), sampleStats.getSamplesRejectedForSize());
156+
assertNumberEqualsLong(parserMap.get("samples_accepted"), sampleStats.getSamples());
157+
assertNumberEqualsLong(
158+
((Number) parserMap.get("time_sampling_millis")).longValue(),
159+
sampleStats.getTimeSampling().millis()
160+
);
161+
assertNumberEqualsLong(
162+
((Number) parserMap.get("time_compiling_condition_millis")).longValue(),
163+
sampleStats.getTimeCompilingCondition().millis()
164+
);
158165
if (humanReadable) {
159166
assertThat(parserMap.get("time_sampling"), equalTo(sampleStats.getTimeSampling().toHumanReadableString(1)));
160167
assertThat(
@@ -174,4 +181,13 @@ public void testToXContent() throws IOException {
174181
}
175182
}
176183
}
184+
185+
/*
186+
* The XContentParser::map will return numbers as Integers if they are small enough. In that case, the actual and expected will not be
187+
* equal if the expected is a long. This method gets the long value of the actual result before asserting that they are equal.
188+
*/
189+
private void assertNumberEqualsLong(Object actual, long expected) {
190+
assertThat(actual, instanceOf(Number.class));
191+
assertThat(((Number) actual).longValue(), equalTo(expected));
192+
}
177193
}

0 commit comments

Comments
 (0)