diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java index dd207033554..18a13c13189 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import static java.util.Collections.emptyList; @@ -229,11 +230,14 @@ public class XPackRestIT extends ESClientYamlSuiteTestCase { CheckedFunction success, Supplier error) { try { - // The actual method call that sends the API requests returns a Future, but we immediately - // call .get() on it so there's no need for this method to do any other awaiting. - ClientYamlTestResponse response = callApi(apiName, params, bodies, getApiCallHeaders()); - assertEquals(response.getStatusCode(), HttpStatus.SC_OK); - success.apply(response); + final AtomicReference response = new AtomicReference<>(); + assertBusy(() -> { + // The actual method call that sends the API requests returns a Future, but we immediately + // call .get() on it so there's no need for this method to do any other awaiting. + response.set(callApi(apiName, params, bodies, getApiCallHeaders())); + assertEquals(HttpStatus.SC_OK, response.get().getStatusCode()); + }); + success.apply(response.get()); } catch (Exception e) { throw new IllegalStateException(error.get(), e); }