|
151 | 151 | import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
|
152 | 152 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
|
153 | 153 | import static org.hamcrest.Matchers.equalTo;
|
| 154 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
154 | 155 |
|
155 | 156 | public abstract class InternalAggregationTestCase<T extends InternalAggregation> extends AbstractWireSerializingTestCase<T> {
|
156 | 157 | public static final int DEFAULT_MAX_BUCKETS = 100000;
|
@@ -267,7 +268,14 @@ public void testReduceRandom() {
|
267 | 268 | new InternalAggregation.ReduceContext(bigArrays, mockScriptService, bucketConsumer,false);
|
268 | 269 | @SuppressWarnings("unchecked")
|
269 | 270 | T reduced = (T) inputs.get(0).reduce(internalAggregations, context);
|
270 |
| - assertMultiBucketConsumer(reduced, bucketConsumer); |
| 271 | + int initialBucketCount = 0; |
| 272 | + for (InternalAggregation internalAggregation : internalAggregations) { |
| 273 | + initialBucketCount += countInnerBucket(internalAggregation); |
| 274 | + } |
| 275 | + int reducedBucketCount = countInnerBucket(reduced); |
| 276 | + //check that non final reduction never adds buckets |
| 277 | + assertThat(reducedBucketCount, lessThanOrEqualTo(initialBucketCount)); |
| 278 | + assertMultiBucketConsumer(reducedBucketCount, bucketConsumer); |
271 | 279 | toReduce = new ArrayList<>(toReduce.subList(r, toReduceSize));
|
272 | 280 | toReduce.add(reduced);
|
273 | 281 | }
|
@@ -332,14 +340,14 @@ protected NamedXContentRegistry xContentRegistry() {
|
332 | 340 |
|
333 | 341 | public final void testFromXContent() throws IOException {
|
334 | 342 | final T aggregation = createTestInstance();
|
335 |
| - final Aggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false); |
336 |
| - assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation); |
| 343 | + final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false); |
| 344 | + assertFromXContent(aggregation, parsedAggregation); |
337 | 345 | }
|
338 | 346 |
|
339 | 347 | public final void testFromXContentWithRandomFields() throws IOException {
|
340 | 348 | final T aggregation = createTestInstance();
|
341 |
| - final Aggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true); |
342 |
| - assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation); |
| 349 | + final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true); |
| 350 | + assertFromXContent(aggregation, parsedAggregation); |
343 | 351 | }
|
344 | 352 |
|
345 | 353 | protected abstract void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) throws IOException;
|
@@ -423,6 +431,10 @@ protected static DocValueFormat randomNumericDocValueFormat() {
|
423 | 431 | }
|
424 | 432 |
|
425 | 433 | public static void assertMultiBucketConsumer(Aggregation agg, MultiBucketConsumer bucketConsumer) {
|
426 |
| - assertThat(bucketConsumer.getCount(), equalTo(countInnerBucket(agg))); |
| 434 | + assertMultiBucketConsumer(countInnerBucket(agg), bucketConsumer); |
| 435 | + } |
| 436 | + |
| 437 | + private static void assertMultiBucketConsumer(int innerBucketCount, MultiBucketConsumer bucketConsumer) { |
| 438 | + assertThat(bucketConsumer.getCount(), equalTo(innerBucketCount)); |
427 | 439 | }
|
428 | 440 | }
|
0 commit comments