[TEST] Add client-test module and make client tests use randomized runner directly

The lucene-test dependency caused issues with IDEs as they would always load the lucene 5 jar although they shouldn't have, which caused jarhell in es core tests.

If we depend directly on randomized runner we don't have this problem. It is luckily still compatible with java 1.7. This requires though adding a thin module that includes the base test class which can be shared between client and client-sniffer.
This commit is contained in:
javanna 2016-06-22 19:22:48 +02:00 committed by Luca Cavanna
parent f0f4db08e0
commit c2839c1577
17 changed files with 255 additions and 122 deletions

View File

@ -165,6 +165,7 @@ subprojects {
"org.elasticsearch:elasticsearch:${version}": ':core',
"org.elasticsearch:client:${version}": ':client',
"org.elasticsearch:client-sniffer:${version}": ':client-sniffer',
"org.elasticsearch:client-test:${version}": ':client-test',
"org.elasticsearch.test:framework:${version}": ':test:framework',
"org.elasticsearch.distribution.integ-test-zip:elasticsearch:${version}": ':distribution:integ-test-zip',
"org.elasticsearch.distribution.zip:elasticsearch:${version}": ':distribution:zip',

View File

@ -34,13 +34,10 @@ dependencies {
compile "commons-logging:commons-logging:${versions.commonslogging}"
compile "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
testCompile "org.elasticsearch:client-test:${version}"
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testCompile "junit:junit:${versions.junit}"
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
//we use the last lucene-test version that is compatible with java 1.7
testCompile "org.apache.lucene:lucene-test-framework:5.5.1"
testCompile "org.apache.lucene:lucene-core:5.5.1"
testCompile "org.apache.lucene:lucene-codecs:5.5.1"
testCompile "org.elasticsearch:securemock:${versions.securemock}"
testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15"
signature "org.codehaus.mojo.signature:java17:1.0@signature"
@ -63,6 +60,7 @@ forbiddenApisTest {
jarHell.enabled=false
namingConventions {
testClass = 'org.elasticsearch.client.RestClientTestCase'
//we don't have integration tests
skipIntegTestInDisguise = true
}

View File

@ -22,10 +22,14 @@ package org.elasticsearch.client.sniff;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.apache.http.HttpHost;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientTestCase;
public class HostsSnifferBuilderTests extends LuceneTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
public class HostsSnifferBuilderTests extends RestClientTestCase {
public void testBuild() throws Exception {
try {
@ -35,7 +39,7 @@ public class HostsSnifferBuilderTests extends LuceneTestCase {
assertEquals(e.getMessage(), "restClient cannot be null");
}
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
int numNodes = RandomInts.randomIntBetween(getRandom(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200 + i);
@ -50,18 +54,18 @@ public class HostsSnifferBuilderTests extends LuceneTestCase {
}
try {
HostsSniffer.builder(client).setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
HostsSniffer.builder(client).setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeoutMillis must be greater than 0");
}
HostsSniffer.Builder builder = HostsSniffer.builder(client);
if (random().nextBoolean()) {
builder.setScheme(RandomPicks.randomFrom(random(), HostsSniffer.Scheme.values()));
if (getRandom().nextBoolean()) {
builder.setScheme(RandomPicks.randomFrom(getRandom(), HostsSniffer.Scheme.values()));
}
if (random().nextBoolean()) {
builder.setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
if (getRandom().nextBoolean()) {
builder.setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
assertNotNull(builder.build());
}

View File

@ -30,11 +30,11 @@ import com.sun.net.httpserver.HttpServer;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.lucene.util.LuceneTestCase;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientTestCase;
import org.junit.After;
import org.junit.Before;
@ -55,10 +55,13 @@ import java.util.Set;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
//animal-sniffer doesn't like our usage of com.sun.net.httpserver.* classes
@IgnoreJRERequirement
public class HostsSnifferTests extends LuceneTestCase {
public class HostsSnifferTests extends RestClientTestCase {
private int sniffRequestTimeout;
private HostsSniffer.Scheme scheme;
@ -67,8 +70,8 @@ public class HostsSnifferTests extends LuceneTestCase {
@Before
public void startHttpServer() throws IOException {
this.sniffRequestTimeout = RandomInts.randomIntBetween(random(), 1000, 10000);
this.scheme = RandomPicks.randomFrom(random(), HostsSniffer.Scheme.values());
this.sniffRequestTimeout = RandomInts.randomIntBetween(getRandom(), 1000, 10000);
this.scheme = RandomPicks.randomFrom(getRandom(), HostsSniffer.Scheme.values());
if (rarely()) {
this.sniffResponse = SniffResponse.buildFailure();
} else {
@ -148,28 +151,28 @@ public class HostsSnifferTests extends LuceneTestCase {
}
private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException {
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
int numNodes = RandomInts.randomIntBetween(getRandom(), 1, 5);
List<HttpHost> hosts = new ArrayList<>(numNodes);
JsonFactory jsonFactory = new JsonFactory();
StringWriter writer = new StringWriter();
JsonGenerator generator = jsonFactory.createGenerator(writer);
generator.writeStartObject();
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeStringField("cluster_name", "elasticsearch");
}
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
generator.writeObjectFieldStart("nodes");
for (int i = 0; i < numNodes; i++) {
String nodeId = RandomStrings.randomAsciiOfLengthBetween(random(), 5, 10);
String nodeId = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 5, 10);
generator.writeObjectFieldStart(nodeId);
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeArrayFieldStart("bogus_array");
generator.writeStartObject();
generator.writeEndObject();
@ -178,35 +181,35 @@ public class HostsSnifferTests extends LuceneTestCase {
boolean isHttpEnabled = rarely() == false;
if (isHttpEnabled) {
String host = "host" + i;
int port = RandomInts.randomIntBetween(random(), 9200, 9299);
int port = RandomInts.randomIntBetween(getRandom(), 9200, 9299);
HttpHost httpHost = new HttpHost(host, port, scheme.toString());
hosts.add(httpHost);
generator.writeObjectFieldStart("http");
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeArrayFieldStart("bound_address");
generator.writeString("[fe80::1]:" + port);
generator.writeString("[::1]:" + port);
generator.writeString("127.0.0.1:" + port);
generator.writeEndArray();
}
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
generator.writeEndObject();
}
generator.writeStringField("publish_address", httpHost.toHostString());
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
generator.writeNumberField("max_content_length_in_bytes", 104857600);
}
generator.writeEndObject();
}
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
String[] roles = {"master", "data", "ingest"};
int numRoles = RandomInts.randomIntBetween(random(), 0, 3);
int numRoles = RandomInts.randomIntBetween(getRandom(), 0, 3);
Set<String> nodeRoles = new HashSet<>(numRoles);
for (int j = 0; j < numRoles; j++) {
String role;
do {
role = RandomPicks.randomFrom(random(), roles);
role = RandomPicks.randomFrom(getRandom(), roles);
} while(nodeRoles.add(role) == false);
}
generator.writeArrayFieldStart("roles");
@ -215,7 +218,7 @@ public class HostsSnifferTests extends LuceneTestCase {
}
generator.writeEndArray();
}
int numAttributes = RandomInts.randomIntBetween(random(), 0, 3);
int numAttributes = RandomInts.randomIntBetween(getRandom(), 0, 3);
Map<String, String> attributes = new HashMap<>(numAttributes);
for (int j = 0; j < numAttributes; j++) {
attributes.put("attr" + j, "value" + j);
@ -264,6 +267,6 @@ public class HostsSnifferTests extends LuceneTestCase {
}
private static int randomErrorResponseCode() {
return RandomInts.randomIntBetween(random(), 400, 599);
return RandomInts.randomIntBetween(getRandom(), 400, 599);
}
}

View File

@ -20,10 +20,13 @@
package org.elasticsearch.client.sniff;
import org.apache.http.HttpHost;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientTestCase;
public class SniffOnFailureListenerTests extends LuceneTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class SniffOnFailureListenerTests extends RestClientTestCase {
public void testSetSniffer() throws Exception {
SniffOnFailureListener listener = new SniffOnFailureListener();

View File

@ -21,13 +21,17 @@ package org.elasticsearch.client.sniff;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import org.apache.http.HttpHost;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientTestCase;
public class SnifferBuilderTests extends LuceneTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
public class SnifferBuilderTests extends RestClientTestCase {
public void testBuild() throws Exception {
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
int numNodes = RandomInts.randomIntBetween(getRandom(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200 + i);
@ -51,7 +55,8 @@ public class SnifferBuilderTests extends LuceneTestCase {
}
try {
Sniffer.builder(client, hostsSniffer).setSniffIntervalMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
Sniffer.builder(client, hostsSniffer)
.setSniffIntervalMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage());
@ -59,7 +64,7 @@ public class SnifferBuilderTests extends LuceneTestCase {
try {
Sniffer.builder(client, hostsSniffer)
.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage());
@ -70,11 +75,11 @@ public class SnifferBuilderTests extends LuceneTestCase {
}
Sniffer.Builder builder = Sniffer.builder(client, hostsSniffer);
if (random().nextBoolean()) {
builder.setSniffIntervalMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
if (getRandom().nextBoolean()) {
builder.setSniffIntervalMillis(RandomInts.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
if (random().nextBoolean()) {
builder.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
if (getRandom().nextBoolean()) {
builder.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
try (Sniffer sniffer = builder.build()) {
assertNotNull(sniffer);

60
client-test/build.gradle Normal file
View File

@ -0,0 +1,60 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.elasticsearch.gradle.precommit.PrecommitTasks
import org.gradle.api.JavaVersion
apply plugin: 'elasticsearch.build'
apply plugin: 'ru.vyarus.animalsniffer'
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
dependencies {
compile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
compile "junit:junit:${versions.junit}"
compile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
compile "org.codehaus.mojo:animal-sniffer-annotations:1.15"
signature "org.codehaus.mojo.signature:java17:1.0@signature"
}
forbiddenApisMain {
//client does not depend on core, so only jdk signatures should be checked
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
}
forbiddenApisTest {
//we are using jdk-internal instead of jdk-non-portable to allow for com.sun.net.httpserver.* usage
bundledSignatures -= 'jdk-non-portable'
bundledSignatures += 'jdk-internal'
//client does not depend on core, so only jdk signatures should be checked
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
}
//JarHell is part of es core, which we don't want to pull in
jarHell.enabled=false
// TODO: should we have licenses for our test deps?
dependencyLicenses.enabled = false
namingConventions.enabled = false
//we aren't releasing this jar
thirdPartyAudit.enabled = false
test.enabled = false

View File

@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client;
import com.carrotsearch.randomizedtesting.JUnit3MethodProvider;
import com.carrotsearch.randomizedtesting.MixWithSuiteName;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.annotations.SeedDecorators;
import com.carrotsearch.randomizedtesting.annotations.TestMethodProviders;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakAction;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakGroup;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies;
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
@TestMethodProviders({
JUnit3MethodProvider.class
})
@SeedDecorators({MixWithSuiteName.class}) // See LUCENE-3995 for rationale.
@ThreadLeakScope(ThreadLeakScope.Scope.SUITE)
@ThreadLeakGroup(ThreadLeakGroup.Group.MAIN)
@ThreadLeakAction({ThreadLeakAction.Action.WARN, ThreadLeakAction.Action.INTERRUPT})
@ThreadLeakZombies(ThreadLeakZombies.Consequence.IGNORE_REMAINING_TESTS)
@TimeoutSuite(millis = 2 * 60 * 60 * 1000)
public abstract class RestClientTestCase extends RandomizedTest {
}

View File

@ -32,13 +32,10 @@ dependencies {
compile "commons-codec:commons-codec:${versions.commonscodec}"
compile "commons-logging:commons-logging:${versions.commonslogging}"
testCompile "org.elasticsearch:client-test:${version}"
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testCompile "junit:junit:${versions.junit}"
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
//we use the last lucene-test version that is compatible with java 1.7
testCompile "org.apache.lucene:lucene-test-framework:5.5.1"
testCompile "org.apache.lucene:lucene-core:5.5.1"
testCompile "org.apache.lucene:lucene-codecs:5.5.1"
testCompile "org.elasticsearch:securemock:${versions.securemock}"
testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15"
signature "org.codehaus.mojo.signature:java17:1.0@signature"
@ -61,6 +58,7 @@ forbiddenApisTest {
jarHell.enabled=false
namingConventions {
testClass = 'org.elasticsearch.client.RestClientTestCase'
//we don't have integration tests
skipIntegTestInDisguise = true
}

View File

@ -39,4 +39,4 @@ class CloseableBasicHttpResponse extends BasicHttpResponse implements CloseableH
public void close() throws IOException {
//nothing to close
}
}
}

View File

@ -36,7 +36,6 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -45,15 +44,16 @@ import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class RequestLoggerTests extends LuceneTestCase {
public class RequestLoggerTests extends RestClientTestCase {
public void testTraceRequest() throws IOException, URISyntaxException {
HttpHost host = new HttpHost("localhost", 9200, random().nextBoolean() ? "http" : "https");
HttpHost host = new HttpHost("localhost", 9200, getRandom().nextBoolean() ? "http" : "https");
URI uri = new URI("/index/type/_api");
HttpRequestBase request;
int requestType = RandomInts.randomIntBetween(random(), 0, 7);
int requestType = RandomInts.randomIntBetween(getRandom(), 0, 7);
switch(requestType) {
case 0:
request = new HttpGetWithEntity(uri);
@ -84,13 +84,13 @@ public class RequestLoggerTests extends LuceneTestCase {
}
String expected = "curl -iX " + request.getMethod() + " '" + host + uri + "'";
boolean hasBody = request instanceof HttpEntityEnclosingRequest && random().nextBoolean();
boolean hasBody = request instanceof HttpEntityEnclosingRequest && getRandom().nextBoolean();
String requestBody = "{ \"field\": \"value\" }";
if (hasBody) {
expected += " -d '" + requestBody + "'";
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity;
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
entity = new StringEntity(requestBody, StandardCharsets.UTF_8);
} else {
entity = new InputStreamEntity(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)));
@ -109,25 +109,25 @@ public class RequestLoggerTests extends LuceneTestCase {
public void testTraceResponse() throws IOException {
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int statusCode = RandomInts.randomIntBetween(random(), 200, 599);
int statusCode = RandomInts.randomIntBetween(getRandom(), 200, 599);
String reasonPhrase = "REASON";
BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase);
String expected = "# " + statusLine.toString();
BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine);
int numHeaders = RandomInts.randomIntBetween(random(), 0, 3);
int numHeaders = RandomInts.randomIntBetween(getRandom(), 0, 3);
for (int i = 0; i < numHeaders; i++) {
httpResponse.setHeader("header" + i, "value");
expected += "\n# header" + i + ": value";
}
expected += "\n#";
boolean hasBody = random().nextBoolean();
boolean hasBody = getRandom().nextBoolean();
String responseBody = "{\n \"field\": \"value\"\n}";
if (hasBody) {
expected += "\n# {";
expected += "\n# \"field\": \"value\"";
expected += "\n# }";
HttpEntity entity;
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
entity = new StringEntity(responseBody, StandardCharsets.UTF_8);
} else {
entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)));

View File

@ -24,11 +24,14 @@ import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.lucene.util.LuceneTestCase;
import java.io.IOException;
public class RestClientBuilderTests extends LuceneTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
public class RestClientBuilderTests extends RestClientTestCase {
public void testBuild() throws IOException {
try {
@ -54,7 +57,7 @@ public class RestClientBuilderTests extends LuceneTestCase {
try {
RestClient.builder(new HttpHost("localhost", 9200))
.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals("maxRetryTimeoutMillis must be greater than 0", e.getMessage());
@ -81,25 +84,25 @@ public class RestClientBuilderTests extends LuceneTestCase {
assertEquals("failure listener must not be null", e.getMessage());
}
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
int numNodes = RandomInts.randomIntBetween(getRandom(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200 + i);
}
RestClient.Builder builder = RestClient.builder(hosts);
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
builder.setHttpClient(HttpClientBuilder.create().build());
}
if (random().nextBoolean()) {
int numHeaders = RandomInts.randomIntBetween(random(), 1, 5);
if (getRandom().nextBoolean()) {
int numHeaders = RandomInts.randomIntBetween(getRandom(), 1, 5);
Header[] headers = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
headers[i] = new BasicHeader("header" + i, "value");
}
builder.setDefaultHeaders(headers);
}
if (random().nextBoolean()) {
builder.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
if (getRandom().nextBoolean()) {
builder.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
try (RestClient restClient = builder.build()) {
assertNotNull(restClient);

View File

@ -31,7 +31,6 @@ import org.apache.http.HttpHost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -53,6 +52,10 @@ import static org.elasticsearch.client.RestClientTestUtil.getAllStatusCodes;
import static org.elasticsearch.client.RestClientTestUtil.getHttpMethods;
import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Integration test to check interaction between {@link RestClient} and {@link org.apache.http.client.HttpClient}.
@ -60,7 +63,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
*/
//animal-sniffer doesn't like our usage of com.sun.net.httpserver.* classes
@IgnoreJRERequirement
public class RestClientIntegTests extends LuceneTestCase {
public class RestClientIntegTests extends RestClientTestCase {
private static HttpServer httpServer;
private static RestClient restClient;
@ -74,11 +77,11 @@ public class RestClientIntegTests extends LuceneTestCase {
for (int statusCode : getAllStatusCodes()) {
createStatusCodeContext(httpServer, statusCode);
}
int numHeaders = RandomInts.randomIntBetween(random(), 0, 3);
int numHeaders = RandomInts.randomIntBetween(getRandom(), 0, 3);
defaultHeaders = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = "Header-default" + (random().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String headerName = "Header-default" + (getRandom().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
defaultHeaders[i] = new BasicHeader(headerName, headerValue);
}
restClient = RestClient.builder(new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()))
@ -144,20 +147,20 @@ public class RestClientIntegTests extends LuceneTestCase {
if (method.equals("HEAD") == false) {
standardHeaders.add("Content-length");
}
int numHeaders = RandomInts.randomIntBetween(random(), 1, 5);
int numHeaders = RandomInts.randomIntBetween(getRandom(), 1, 5);
Map<String, String> expectedHeaders = new HashMap<>();
for (Header defaultHeader : defaultHeaders) {
expectedHeaders.put(defaultHeader.getName(), defaultHeader.getValue());
}
Header[] headers = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = "Header" + (random().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String headerName = "Header" + (getRandom().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
headers[i] = new BasicHeader(headerName, headerValue);
expectedHeaders.put(headerName, headerValue);
}
int statusCode = randomStatusCode(random());
int statusCode = randomStatusCode(getRandom());
Response esResponse;
try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null, headers)) {
@ -186,7 +189,7 @@ public class RestClientIntegTests extends LuceneTestCase {
* Exercises the test http server ability to send back whatever body it received.
*/
public void testDeleteWithBody() throws Exception {
testBody("DELETE");
bodyTest("DELETE");
}
/**
@ -195,15 +198,15 @@ public class RestClientIntegTests extends LuceneTestCase {
* Exercises the test http server ability to send back whatever body it received.
*/
public void testGetWithBody() throws Exception {
testBody("GET");
bodyTest("GET");
}
private void testBody(String method) throws Exception {
private void bodyTest(String method) throws Exception {
String requestBody = "{ \"field\": \"value\" }";
StringEntity entity = new StringEntity(requestBody);
Response esResponse;
String responseBody;
int statusCode = randomStatusCode(random());
int statusCode = randomStatusCode(getRandom());
try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), entity)) {
responseBody = EntityUtils.toString(response.getEntity());

View File

@ -19,6 +19,7 @@
package org.elasticsearch.client;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
@ -29,7 +30,6 @@ import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicStatusLine;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Before;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@ -46,6 +46,10 @@ import static org.elasticsearch.client.RestClientTestUtil.randomHttpMethod;
import static org.elasticsearch.client.RestClientTestUtil.randomOkStatusCode;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -54,7 +58,7 @@ import static org.mockito.Mockito.when;
* Tests for {@link RestClient} behaviour against multiple hosts: fail-over, blacklisting etc.
* Relies on a mock http client to intercept requests and return desired responses based on request path.
*/
public class RestClientMultipleHostsTests extends LuceneTestCase {
public class RestClientMultipleHostsTests extends RestClientTestCase {
private RestClient restClient;
private HttpHost[] httpHosts;
@ -82,7 +86,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
}
});
int numHosts = RandomInts.randomIntBetween(random(), 2, 5);
int numHosts = RandomInts.randomIntBetween(getRandom(), 2, 5);
httpHosts = new HttpHost[numHosts];
for (int i = 0; i < numHosts; i++) {
httpHosts[i] = new HttpHost("localhost", 9200 + i);
@ -92,13 +96,13 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
}
public void testRoundRobinOkStatusCodes() throws Exception {
int numIters = RandomInts.randomIntBetween(random(), 1, 5);
int numIters = RandomInts.randomIntBetween(getRandom(), 1, 5);
for (int i = 0; i < numIters; i++) {
Set<HttpHost> hostsSet = new HashSet<>();
Collections.addAll(hostsSet, httpHosts);
for (int j = 0; j < httpHosts.length; j++) {
int statusCode = randomOkStatusCode(random());
try (Response response = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode,
int statusCode = randomOkStatusCode(getRandom());
try (Response response = restClient.performRequest(randomHttpMethod(getRandom()), "/" + statusCode,
Collections.<String, String>emptyMap(), null)) {
assertThat(response.getStatusLine().getStatusCode(), equalTo(statusCode));
assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost()));
@ -110,13 +114,13 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
}
public void testRoundRobinNoRetryErrors() throws Exception {
int numIters = RandomInts.randomIntBetween(random(), 1, 5);
int numIters = RandomInts.randomIntBetween(getRandom(), 1, 5);
for (int i = 0; i < numIters; i++) {
Set<HttpHost> hostsSet = new HashSet<>();
Collections.addAll(hostsSet, httpHosts);
for (int j = 0; j < httpHosts.length; j++) {
String method = randomHttpMethod(random());
int statusCode = randomErrorNoRetryStatusCode(random());
String method = randomHttpMethod(getRandom());
int statusCode = randomErrorNoRetryStatusCode(getRandom());
try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null)) {
if (method.equals("HEAD") && statusCode == 404) {
@ -145,7 +149,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
public void testRoundRobinRetryErrors() throws Exception {
String retryEndpoint = randomErrorRetryEndpoint();
try {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null);
restClient.performRequest(randomHttpMethod(getRandom()), retryEndpoint, Collections.<String, String>emptyMap(), null);
fail("request should have failed");
} catch(ResponseException e) {
Set<HttpHost> hostsSet = new HashSet<>();
@ -187,7 +191,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
assertEquals("every host should have been used but some weren't: " + hostsSet, 0, hostsSet.size());
}
int numIters = RandomInts.randomIntBetween(random(), 2, 5);
int numIters = RandomInts.randomIntBetween(getRandom(), 2, 5);
for (int i = 1; i <= numIters; i++) {
//check that one different host is resurrected at each new attempt
Set<HttpHost> hostsSet = new HashSet<>();
@ -195,7 +199,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
for (int j = 0; j < httpHosts.length; j++) {
retryEndpoint = randomErrorRetryEndpoint();
try {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint, Collections.<String, String>emptyMap(), null);
restClient.performRequest(randomHttpMethod(getRandom()), retryEndpoint, Collections.<String, String>emptyMap(), null);
fail("request should have failed");
} catch(ResponseException e) {
Response response = e.getResponse();
@ -214,14 +218,14 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
}
}
assertEquals("every host should have been used but some weren't: " + hostsSet, 0, hostsSet.size());
if (random().nextBoolean()) {
if (getRandom().nextBoolean()) {
//mark one host back alive through a successful request and check that all requests after that are sent to it
HttpHost selectedHost = null;
int iters = RandomInts.randomIntBetween(random(), 2, 10);
int iters = RandomInts.randomIntBetween(getRandom(), 2, 10);
for (int y = 0; y < iters; y++) {
int statusCode = randomErrorNoRetryStatusCode(random());
int statusCode = randomErrorNoRetryStatusCode(getRandom());
Response response;
try (Response esResponse = restClient.performRequest(randomHttpMethod(random()), "/" + statusCode,
try (Response esResponse = restClient.performRequest(randomHttpMethod(getRandom()), "/" + statusCode,
Collections.<String, String>emptyMap(), null)) {
response = esResponse;
}
@ -241,7 +245,7 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
for (int y = 0; y < i + 1; y++) {
retryEndpoint = randomErrorRetryEndpoint();
try {
restClient.performRequest(randomHttpMethod(random()), retryEndpoint,
restClient.performRequest(randomHttpMethod(getRandom()), retryEndpoint,
Collections.<String, String>emptyMap(), null);
fail("request should have failed");
} catch(ResponseException e) {
@ -260,9 +264,9 @@ public class RestClientMultipleHostsTests extends LuceneTestCase {
}
private static String randomErrorRetryEndpoint() {
switch(RandomInts.randomIntBetween(random(), 0, 3)) {
switch(RandomInts.randomIntBetween(getRandom(), 0, 3)) {
case 0:
return "/" + randomErrorRetryStatusCode(random());
return "/" + randomErrorRetryStatusCode(getRandom());
case 1:
return "/coe";
case 2:

View File

@ -19,6 +19,7 @@
package org.elasticsearch.client;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import org.apache.http.Header;
@ -43,7 +44,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Before;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
@ -65,6 +65,12 @@ import static org.elasticsearch.client.RestClientTestUtil.randomHttpMethod;
import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@ -76,7 +82,7 @@ import static org.mockito.Mockito.when;
* body, different status codes and corresponding responses/exceptions.
* Relies on a mock http client to intercept requests and return desired responses based on request path.
*/
public class RestClientSingleHostTests extends LuceneTestCase {
public class RestClientSingleHostTests extends RestClientTestCase {
private RestClient restClient;
private Header[] defaultHeaders;
@ -114,11 +120,11 @@ public class RestClientSingleHostTests extends LuceneTestCase {
return httpResponse;
}
});
int numHeaders = RandomInts.randomIntBetween(random(), 0, 3);
int numHeaders = RandomInts.randomIntBetween(getRandom(), 0, 3);
defaultHeaders = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = "Header-default" + (random().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String headerName = "Header-default" + (getRandom().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
defaultHeaders[i] = new BasicHeader(headerName, headerValue);
}
httpHost = new HttpHost("localhost", 9200);
@ -269,7 +275,7 @@ public class RestClientSingleHostTests extends LuceneTestCase {
}
for (String method : Arrays.asList("HEAD", "OPTIONS", "TRACE")) {
try {
restClient.performRequest(method, "/" + randomStatusCode(random()),
restClient.performRequest(method, "/" + randomStatusCode(getRandom()),
Collections.<String, String>emptyMap(), entity);
fail("request should have failed");
} catch(UnsupportedOperationException e) {
@ -279,8 +285,8 @@ public class RestClientSingleHostTests extends LuceneTestCase {
}
public void testNullHeaders() throws Exception {
String method = randomHttpMethod(random());
int statusCode = randomStatusCode(random());
String method = randomHttpMethod(getRandom());
int statusCode = randomStatusCode(getRandom());
try {
restClient.performRequest(method, "/" + statusCode, Collections.<String, String>emptyMap(), null, (Header[])null);
fail("request should have failed");
@ -296,8 +302,8 @@ public class RestClientSingleHostTests extends LuceneTestCase {
}
public void testNullParams() throws Exception {
String method = randomHttpMethod(random());
int statusCode = randomStatusCode(random());
String method = randomHttpMethod(getRandom());
int statusCode = randomStatusCode(getRandom());
try {
restClient.performRequest(method, "/" + statusCode, null, null);
fail("request should have failed");
@ -316,16 +322,16 @@ public class RestClientSingleHostTests extends LuceneTestCase {
for (Header defaultHeader : defaultHeaders) {
expectedHeaders.put(defaultHeader.getName(), defaultHeader.getValue());
}
int numHeaders = RandomInts.randomIntBetween(random(), 1, 5);
int numHeaders = RandomInts.randomIntBetween(getRandom(), 1, 5);
Header[] headers = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = "Header" + (random().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String headerName = "Header" + (getRandom().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
headers[i] = new BasicHeader(headerName, headerValue);
expectedHeaders.put(headerName, headerValue);
}
int statusCode = randomStatusCode(random());
int statusCode = randomStatusCode(getRandom());
Response esResponse;
try (Response response = restClient.performRequest(method, "/" + statusCode,
Collections.<String, String>emptyMap(), null, headers)) {
@ -343,15 +349,15 @@ public class RestClientSingleHostTests extends LuceneTestCase {
}
private HttpUriRequest performRandomRequest(String method) throws IOException, URISyntaxException {
String uriAsString = "/" + randomStatusCode(random());
String uriAsString = "/" + randomStatusCode(getRandom());
URIBuilder uriBuilder = new URIBuilder(uriAsString);
Map<String, String> params = Collections.emptyMap();
if (random().nextBoolean()) {
int numParams = RandomInts.randomIntBetween(random(), 1, 3);
if (getRandom().nextBoolean()) {
int numParams = RandomInts.randomIntBetween(getRandom(), 1, 3);
params = new HashMap<>(numParams);
for (int i = 0; i < numParams; i++) {
String paramKey = "param-" + i;
String paramValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String paramValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
params.put(paramKey, paramValue);
uriBuilder.addParameter(paramKey, paramValue);
}
@ -389,8 +395,8 @@ public class RestClientSingleHostTests extends LuceneTestCase {
}
HttpEntity entity = null;
if (request instanceof HttpEntityEnclosingRequest && random().nextBoolean()) {
entity = new StringEntity(RandomStrings.randomAsciiOfLengthBetween(random(), 10, 100));
if (request instanceof HttpEntityEnclosingRequest && getRandom().nextBoolean()) {
entity = new StringEntity(RandomStrings.randomAsciiOfLengthBetween(getRandom(), 10, 100));
((HttpEntityEnclosingRequest) request).setEntity(entity);
}
@ -399,12 +405,12 @@ public class RestClientSingleHostTests extends LuceneTestCase {
//default headers are expected but not sent for each request
request.setHeader(defaultHeader);
}
if (random().nextBoolean()) {
int numHeaders = RandomInts.randomIntBetween(random(), 1, 5);
if (getRandom().nextBoolean()) {
int numHeaders = RandomInts.randomIntBetween(getRandom(), 1, 5);
headers = new Header[numHeaders];
for (int i = 0; i < numHeaders; i++) {
String headerName = "Header" + (random().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(random(), 3, 10);
String headerName = "Header" + (getRandom().nextBoolean() ? i : "");
String headerValue = RandomStrings.randomAsciiOfLengthBetween(getRandom(), 3, 10);
BasicHeader basicHeader = new BasicHeader(headerName, headerValue);
headers[i] = basicHeader;
request.setHeader(basicHeader);

View File

@ -7,6 +7,7 @@ List projects = [
'docs',
'client',
'client-sniffer',
'client-test',
'benchmarks',
'distribution:integ-test-zip',
'distribution:zip',