From e78737b19bbc127e78536de2184e6450bea2ed54 Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 16 Sep 2014 12:46:15 +0200 Subject: [PATCH] [TEST] Update REST client before each test in our REST tests In #7723 we removed the `updateAddresses` method from `RestClient` under the assumption that the addresses never change during the suite execution, as REST tests rely on the global cluster. Due to #6734 we restart the global cluster though before each test if there was a failure in the suite. If that happens we do need to make sure that the REST client points to the proper nodes. What was missing before was the http call to verify the es version every time the addresses change, which we do now since we effectively re-initialize the REST client when needed (if the http addresses have changed). Closes #7737 --- .../test/rest/RestTestExecutionContext.java | 13 ++++++++++++- .../elasticsearch/test/rest/client/RestClient.java | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/elasticsearch/test/rest/RestTestExecutionContext.java b/src/test/java/org/elasticsearch/test/rest/RestTestExecutionContext.java index 838633fbfdc..2c232a7cacc 100644 --- a/src/test/java/org/elasticsearch/test/rest/RestTestExecutionContext.java +++ b/src/test/java/org/elasticsearch/test/rest/RestTestExecutionContext.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.rest; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; @@ -34,6 +35,7 @@ import java.net.InetSocketAddress; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; /** * Execution context passed across the REST tests. @@ -116,11 +118,20 @@ public class RestTestExecutionContext implements Closeable { } /** - * Creates the embedded REST client when needed. Needs to be called before executing any test. + * Creates or updates the embedded REST client when needed. Needs to be called before each test. */ public void resetClient(InetSocketAddress[] addresses, Settings settings) throws IOException, RestException { if (restClient == null) { restClient = new RestClient(restSpec, settings, addresses); + } else { + //re-initialize the REST client if the addresses have changed + //happens if there's a failure since we restart the global cluster due to that + Set newAddresses = Sets.newHashSet(addresses); + Set previousAddresses = Sets.newHashSet(restClient.httpAddresses()); + if (!newAddresses.equals(previousAddresses)) { + restClient.close(); + restClient = new RestClient(restSpec, settings, addresses); + } } } diff --git a/src/test/java/org/elasticsearch/test/rest/client/RestClient.java b/src/test/java/org/elasticsearch/test/rest/client/RestClient.java index ecbd434b0da..63fbd589879 100644 --- a/src/test/java/org/elasticsearch/test/rest/client/RestClient.java +++ b/src/test/java/org/elasticsearch/test/rest/client/RestClient.java @@ -221,6 +221,10 @@ public class RestClient implements Closeable { return HttpClients.createDefault(); } + public InetSocketAddress[] httpAddresses() { + return addresses; + } + /** * Closes the REST client and the underlying http client */