[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
This commit is contained in:
javanna 2014-09-16 12:46:15 +02:00 committed by Luca Cavanna
parent cc99bfe802
commit e78737b19b
2 changed files with 16 additions and 1 deletions

View File

@ -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<InetSocketAddress> newAddresses = Sets.newHashSet(addresses);
Set<InetSocketAddress> previousAddresses = Sets.newHashSet(restClient.httpAddresses());
if (!newAddresses.equals(previousAddresses)) {
restClient.close();
restClient = new RestClient(restSpec, settings, addresses);
}
}
}

View File

@ -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
*/