Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Sniffer and make it testable #29638

Merged
merged 25 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e581c56
Simplify `Sniffer` and begin testing it properly
javanna Apr 4, 2018
900d3aa
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna Apr 10, 2018
0d8a270
some improvements
javanna Apr 10, 2018
c9ec5a8
tests rewritten
javanna Apr 12, 2018
1858a4c
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna Apr 20, 2018
4c886a0
refactor and add tests
javanna Apr 20, 2018
c2a88a3
improve tests
javanna Apr 20, 2018
5d7c636
address first review comments
javanna Apr 20, 2018
9407f80
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna Apr 30, 2018
9658886
adapt NodeFailureListener
javanna Apr 30, 2018
4094a4e
fixed warnings
javanna Apr 30, 2018
f3baab3
Adapt HttpExporter
javanna Apr 30, 2018
07dce21
adapt NodeFailureListenerTests
javanna Apr 30, 2018
5603296
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 1, 2018
440bd5c
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 14, 2018
2cf41b0
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 17, 2018
dccb673
Add tests around cancelling tasks and make impl more robust
javanna May 25, 2018
880b6a9
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 25, 2018
1d5ac98
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 28, 2018
1759215
update comments
javanna May 28, 2018
44b5178
addressed comments
javanna May 30, 2018
2dd307e
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 30, 2018
8b22063
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 30, 2018
8a8d348
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 31, 2018
f80b119
Merge branch 'master' into enhancement/rest_client_sniffer_tests
javanna May 31, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -132,7 +133,7 @@ public synchronized void setHosts(HttpHost... hosts) {
if (hosts == null || hosts.length == 0) {
throw new IllegalArgumentException("hosts must not be null nor empty");
}
Set<HttpHost> httpHosts = new HashSet<>();
Set<HttpHost> httpHosts = new LinkedHashSet<>();
AuthCache authCache = new BasicAuthCache();
for (HttpHost host : hosts) {
Objects.requireNonNull(host, "host cannot be null");
Expand All @@ -143,6 +144,13 @@ public synchronized void setHosts(HttpHost... hosts) {
this.blacklist.clear();
}

/**
* Returns the configured hosts
*/
public List<HttpHost> getHosts() {
return new ArrayList<>(hostTuple.hosts);
}

/**
* Sends a request to the Elasticsearch cluster that the client points to.
* Blocks until the request is completed and returns its response or fails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.nio.entity.NStringEntity;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is worse? Maybe revert it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sorry

import org.elasticsearch.client.HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -251,6 +252,37 @@ public void testSetHostsWrongArguments() throws IOException {
}
}

public void testSetHostsPreservesOrdering() throws Exception {
try (RestClient restClient = createRestClient()) {
HttpHost[] hosts = randomHosts();
restClient.setHosts(hosts);
assertEquals(Arrays.asList(hosts), restClient.getHosts());
}
}

private static HttpHost[] randomHosts() {
int numHosts = randomIntBetween(1, 10);
HttpHost[] hosts = new HttpHost[numHosts];
for (int i = 0; i < hosts.length; i++) {
hosts[i] = new HttpHost("host-" + i, 9200);
}
return hosts;
}

public void testSetHostsDuplicatedHosts() throws Exception {
try (RestClient restClient = createRestClient()) {
int numHosts = randomIntBetween(1, 10);
HttpHost[] hosts = new HttpHost[numHosts];
HttpHost host = new HttpHost("host", 9200);
for (int i = 0; i < hosts.length; i++) {
hosts[i] = host;
}
restClient.setHosts(hosts);
assertEquals(1, restClient.getHosts().size());
assertEquals(host, restClient.getHosts().get(0));
}
}

/**
* @deprecated will remove method in 7.0 but needs tests until then. Replaced by {@link RequestTests#testConstructor()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void onFailure(HttpHost host) {
if (sniffer == null) {
throw new IllegalStateException("sniffer was not set, unable to sniff on failure");
}
//re-sniff immediately but take out the node that failed
sniffer.sniffOnFailure(host);
sniffer.sniffOnFailure();
}
}
Loading