diff --git a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index ee7e607d5a9ec..36ee02c2c5cc6 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -283,17 +283,11 @@ public static void assertHitCount(SearchRequestBuilder searchRequestBuilder, lon } public static void assertHitCount(ActionFuture<SearchResponse> responseFuture, long expectedHitCount) { - SearchResponse res; try { - res = responseFuture.get(); + assertResponse(responseFuture, res -> assertHitCount(res, expectedHitCount)); } catch (ExecutionException | InterruptedException ex) { throw new AssertionError(ex); } - try { - assertHitCount(res, expectedHitCount); - } finally { - res.decRef(); - } } public static void assertHitCount(SearchResponse countResponse, long expectedHitCount) { @@ -354,6 +348,16 @@ public static void assertResponse(SearchRequestBuilder searchRequestBuilder, Con } } + public static void assertResponse(ActionFuture<SearchResponse> responseFuture, Consumer<SearchResponse> consumer) + throws ExecutionException, InterruptedException { + var res = responseFuture.get(); + try { + consumer.accept(res); + } finally { + res.decRef(); + } + } + public static void assertCheckedResponse( SearchRequestBuilder searchRequestBuilder, CheckedConsumer<SearchResponse, IOException> consumer @@ -439,12 +443,7 @@ public static void assertHighlight( int totalFragments, Matcher<String> matcher ) { - var resp = searchRequestBuilder.get(); - try { - assertHighlight(resp, hit, field, fragment, equalTo(totalFragments), matcher); - } finally { - resp.decRef(); - } + assertResponse(searchRequestBuilder, response -> assertHighlight(response, hit, field, fragment, equalTo(totalFragments), matcher)); } public static void assertHighlight( @@ -455,12 +454,7 @@ public static void assertHighlight( int totalFragments, Matcher<String> matcher ) throws ExecutionException, InterruptedException { - var resp = responseFuture.get(); - try { - assertHighlight(resp, hit, field, fragment, equalTo(totalFragments), matcher); - } finally { - resp.decRef(); - } + assertResponse(responseFuture, response -> assertHighlight(response, hit, field, fragment, equalTo(totalFragments), matcher)); } public static void assertHighlight( @@ -508,12 +502,7 @@ private static void assertHighlight( } public static void assertNotHighlighted(SearchRequestBuilder searchRequestBuilder, int hit, String field) { - var resp = searchRequestBuilder.get(); - try { - assertNotHighlighted(resp, hit, field); - } finally { - resp.decRef(); - } + assertResponse(searchRequestBuilder, response -> assertNotHighlighted(response, hit, field)); } public static void assertNotHighlighted(SearchResponse resp, int hit, String field) { @@ -589,7 +578,6 @@ public static void assertIndexTemplateExists(GetIndexTemplatesResponse templates assertThat(templatesResponse.getIndexTemplates(), hasItem(transformedMatch(IndexTemplateMetadata::name, equalTo(name)))); } - /* /* * matchers */