Skip to content

Commit 8c407e2

Browse files
authored
Cover missing case in top_metrics test (#52516)
The top_metrics test assumed that it'd never end up *only* reducing unmapped results. But, rarely, it does. This handles that case in the test. Closes #52462
1 parent d46d931 commit 8c407e2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetricsTests.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.Optional;
3031
import java.util.function.Predicate;
3132

3233
import static java.util.Collections.emptyList;
@@ -166,16 +167,23 @@ protected void assertFromXContent(InternalTopMetrics aggregation, ParsedAggregat
166167
@Override
167168
protected void assertReduced(InternalTopMetrics reduced, List<InternalTopMetrics> inputs) {
168169
InternalTopMetrics first = inputs.get(0);
169-
InternalTopMetrics winner = inputs.stream()
170+
Optional<InternalTopMetrics> winner = inputs.stream()
170171
.filter(tm -> tm.isMapped())
171-
.min((lhs, rhs) -> first.getSortOrder().reverseMul() * lhs.getSortValue().compareTo(rhs.getSortValue()))
172-
.get();
172+
.min((lhs, rhs) -> first.getSortOrder().reverseMul() * lhs.getSortValue().compareTo(rhs.getSortValue()));
173+
173174
assertThat(reduced.getName(), equalTo(first.getName()));
174-
assertThat(reduced.getSortValue(), equalTo(winner.getSortValue()));
175-
assertThat(reduced.getSortFormat(), equalTo(winner.getSortFormat()));
176175
assertThat(reduced.getSortOrder(), equalTo(first.getSortOrder()));
177-
assertThat(reduced.getMetricValue(), equalTo(winner.getMetricValue()));
178176
assertThat(reduced.getMetricName(), equalTo(first.getMetricName()));
177+
if (winner.isPresent()) {
178+
assertThat(reduced.getSortValue(), equalTo(winner.get().getSortValue()));
179+
assertThat(reduced.getSortFormat(), equalTo(winner.get().getSortFormat()));
180+
assertThat(reduced.getMetricValue(), equalTo(winner.get().getMetricValue()));
181+
} else {
182+
// Reduced only unmapped metrics
183+
assertThat(reduced.getSortValue(), equalTo(first.getSortValue()));
184+
assertThat(reduced.getSortFormat(), equalTo(first.getSortFormat()));
185+
assertThat(reduced.getMetricValue(), equalTo(first.getMetricValue()));
186+
}
179187
}
180188

181189
private static SortValue randomSortValue() {

0 commit comments

Comments
 (0)