From 1ed829677de73c038127cccd27b7c4123867f246 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Fri, 8 Dec 2023 10:59:14 -0600 Subject: [PATCH] fixing DownsampleDataStreamTests --- .../downsample/DownsampleDataStreamTests.java | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/DownsampleDataStreamTests.java b/x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/DownsampleDataStreamTests.java index 95640f4625849..5892d35669e8f 100644 --- a/x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/DownsampleDataStreamTests.java +++ b/x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/DownsampleDataStreamTests.java @@ -216,31 +216,32 @@ private void putComposableIndexTemplate(final String id, final List patt } private void indexDocs(final String dataStream, int numDocs, long startTime) { - final BulkRequest bulkRequest = new BulkRequest(); - for (int i = 0; i < numDocs; i++) { - final String timestamp = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(startTime + i); - bulkRequest.add( - new IndexRequest(dataStream).opType(DocWriteRequest.OpType.CREATE) - .source( - String.format( - Locale.ROOT, - "{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\"}", - DEFAULT_TIMESTAMP_FIELD, - timestamp, - "routing_field", - 0, - "counter", - i + 1 - ), - XContentType.JSON - ) - ); + try (BulkRequest bulkRequest = new BulkRequest()) { + for (int i = 0; i < numDocs; i++) { + final String timestamp = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(startTime + i); + bulkRequest.add( + new IndexRequest(dataStream).opType(DocWriteRequest.OpType.CREATE) + .source( + String.format( + Locale.ROOT, + "{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\"}", + DEFAULT_TIMESTAMP_FIELD, + timestamp, + "routing_field", + 0, + "counter", + i + 1 + ), + XContentType.JSON + ) + ); + } + final BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); + final BulkItemResponse[] items = bulkResponse.getItems(); + assertThat(items.length, equalTo(numDocs)); + assertThat(bulkResponse.hasFailures(), equalTo(false)); + final RefreshResponse refreshResponse = indicesAdmin().refresh(new RefreshRequest(dataStream)).actionGet(); + assertThat(refreshResponse.getStatus().getStatus(), equalTo(RestStatus.OK.getStatus())); } - final BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); - final BulkItemResponse[] items = bulkResponse.getItems(); - assertThat(items.length, equalTo(numDocs)); - assertThat(bulkResponse.hasFailures(), equalTo(false)); - final RefreshResponse refreshResponse = indicesAdmin().refresh(new RefreshRequest(dataStream)).actionGet(); - assertThat(refreshResponse.getStatus().getStatus(), equalTo(RestStatus.OK.getStatus())); } }