Skip to content

Commit 8122506

Browse files
committed
Remove random when using HLRC sync and async calls
This commit removes the randomization used by every execute call in the high level rest tests. Previously every execute call, which can be many calls per single test, would rely on a random boolean to determine if they should use the sync or async methods provided to the execute method. This commit runs the tests twice, using two different clusters, both of them providing the value one time via a sysprop. This ensures that the whole suite of tests is run using the sync and async code paths. Closes elastic#39667
1 parent 6abacd2 commit 8122506

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

client/rest-high-level/build.gradle

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.elasticsearch.gradle.test.RestIntegTestTask
2+
13
/*
24
* Licensed to Elasticsearch under one or more contributor
35
* license agreements. See the NOTICE file distributed with
@@ -106,33 +108,49 @@ File nodeTrustStore = file("./testnode.jks")
106108
File pkiTrustCert = file("./src/test/resources/org/elasticsearch/client/security/delegate_pki/testRootCA.crt")
107109

108110
integTest.runner {
111+
systemProperty 'tests.rest.async', 'false'
109112
systemProperty 'tests.rest.cluster.username', System.getProperty('tests.rest.cluster.username', 'test_user')
110113
systemProperty 'tests.rest.cluster.password', System.getProperty('tests.rest.cluster.password', 'test-password')
111114
}
112115

113-
testClusters.integTest {
114-
testDistribution = 'DEFAULT'
115-
systemProperty 'es.scripting.update.ctx_in_params', 'false'
116-
setting 'reindex.remote.whitelist', '[ "[::1]:*", "127.0.0.1:*" ]'
117-
setting 'xpack.license.self_generated.type', 'trial'
118-
setting 'xpack.security.enabled', 'true'
119-
setting 'xpack.security.authc.token.enabled', 'true'
120-
setting 'xpack.security.authc.api_key.enabled', 'true'
121-
// Truststore settings are not used since TLS is not enabled. Included for testing the get certificates API
122-
setting 'xpack.security.http.ssl.certificate_authorities', 'testnode.crt'
123-
setting 'xpack.security.transport.ssl.truststore.path', 'testnode.jks'
124-
setting 'xpack.security.authc.realms.file.default_file.order', '0'
125-
setting 'xpack.security.authc.realms.native.default_native.order', '1'
126-
setting 'xpack.security.authc.realms.pki.pki1.order', '2'
127-
setting 'xpack.security.authc.realms.pki.pki1.certificate_authorities', '[ "testRootCA.crt" ]'
128-
setting 'xpack.security.authc.realms.pki.pki1.delegation.enabled', 'true'
129-
130-
setting 'indices.lifecycle.poll_interval', '1000ms'
131-
keystore 'xpack.security.transport.ssl.truststore.secure_password', 'testnode'
132-
user username: System.getProperty('tests.rest.cluster.username', 'test_user'),
133-
password: System.getProperty('tests.rest.cluster.password', 'test-password')
134-
135-
extraConfigFile nodeCert.name, nodeCert
136-
extraConfigFile nodeTrustStore.name, nodeTrustStore
137-
extraConfigFile pkiTrustCert.name, pkiTrustCert
116+
// TODO: we can't use task avoidance here because RestIntegTestTask does the testcluster creation
117+
RestIntegTestTask asyncIntegTest = tasks.create("asyncIntegTest", RestIntegTestTask) {
118+
runner {
119+
systemProperty 'tests.rest.async', 'true'
120+
systemProperty 'tests.rest.cluster.username', System.getProperty('tests.rest.cluster.username', 'test_user')
121+
systemProperty 'tests.rest.cluster.password', System.getProperty('tests.rest.cluster.password', 'test-password')
122+
}
123+
}
124+
125+
check.dependsOn(asyncIntegTest)
126+
127+
['integTest', 'asyncIntegTest'].each { integName ->
128+
testClusters {
129+
"${integName}" {
130+
testDistribution = 'DEFAULT'
131+
systemProperty 'es.scripting.update.ctx_in_params', 'false'
132+
setting 'reindex.remote.whitelist', '[ "[::1]:*", "127.0.0.1:*" ]'
133+
setting 'xpack.license.self_generated.type', 'trial'
134+
setting 'xpack.security.enabled', 'true'
135+
setting 'xpack.security.authc.token.enabled', 'true'
136+
setting 'xpack.security.authc.api_key.enabled', 'true'
137+
// Truststore settings are not used since TLS is not enabled. Included for testing the get certificates API
138+
setting 'xpack.security.http.ssl.certificate_authorities', 'testnode.crt'
139+
setting 'xpack.security.transport.ssl.truststore.path', 'testnode.jks'
140+
setting 'xpack.security.authc.realms.file.default_file.order', '0'
141+
setting 'xpack.security.authc.realms.native.default_native.order', '1'
142+
setting 'xpack.security.authc.realms.pki.pki1.order', '2'
143+
setting 'xpack.security.authc.realms.pki.pki1.certificate_authorities', '[ "testRootCA.crt" ]'
144+
setting 'xpack.security.authc.realms.pki.pki1.delegation.enabled', 'true'
145+
146+
setting 'indices.lifecycle.poll_interval', '1000ms'
147+
keystore 'xpack.security.transport.ssl.truststore.secure_password', 'testnode'
148+
user username: System.getProperty('tests.rest.cluster.username', 'test_user'),
149+
password: System.getProperty('tests.rest.cluster.password', 'test-password')
150+
151+
extraConfigFile nodeCert.name, nodeCert
152+
extraConfigFile nodeTrustStore.name, nodeTrustStore
153+
extraConfigFile pkiTrustCert.name, pkiTrustCert
154+
}
155+
}
138156
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.search.SearchResponse;
2727
import org.elasticsearch.action.support.PlainActionFuture;
2828
import org.elasticsearch.client.indices.CreateIndexRequest;
29+
import org.elasticsearch.common.Booleans;
2930
import org.elasticsearch.common.bytes.BytesReference;
3031
import org.elasticsearch.common.settings.Settings;
3132
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -78,20 +79,22 @@ protected static <Req, Resp> Resp execute(Req request, SyncMethod<Req, Resp> syn
7879
AsyncMethod<Req, Resp> asyncMethod) throws IOException {
7980
return execute(request, syncMethod, asyncMethod, RequestOptions.DEFAULT);
8081
}
81-
82+
8283
/**
8384
* Executes the provided request using either the sync method or its async variant, both provided as functions
8485
*/
8586
protected static <Req, Resp> Resp execute(Req request, SyncMethod<Req, Resp> syncMethod,
8687
AsyncMethod<Req, Resp> asyncMethod, RequestOptions options) throws IOException {
87-
if (randomBoolean()) {
88+
final boolean async = Booleans.parseBoolean(System.getProperty("tests.rest.async", "false"));
89+
90+
if (async == false) {
8891
return syncMethod.execute(request, options);
8992
} else {
9093
PlainActionFuture<Resp> future = PlainActionFuture.newFuture();
9194
asyncMethod.execute(request, options, future);
9295
return future.actionGet();
9396
}
94-
}
97+
}
9598

9699
/**
97100
* Executes the provided request using either the sync method or its async
@@ -100,7 +103,9 @@ protected static <Req, Resp> Resp execute(Req request, SyncMethod<Req, Resp> syn
100103
*/
101104
protected static <Resp> Resp execute(SyncMethodNoRequest<Resp> syncMethodNoRequest, AsyncMethodNoRequest<Resp> asyncMethodNoRequest,
102105
RequestOptions requestOptions) throws IOException {
103-
if (randomBoolean()) {
106+
final boolean async = Booleans.parseBoolean(System.getProperty("tests.rest.async", "false"));
107+
108+
if (async == false) {
104109
return syncMethodNoRequest.execute(requestOptions);
105110
} else {
106111
PlainActionFuture<Resp> future = PlainActionFuture.newFuture();

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.elasticsearch.client.indices.CreateIndexRequest;
4949
import org.elasticsearch.cluster.SnapshotsInProgress;
5050
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
51+
import org.elasticsearch.common.Booleans;
5152
import org.elasticsearch.common.settings.Settings;
5253
import org.elasticsearch.common.unit.TimeValue;
5354
import org.elasticsearch.common.xcontent.XContentType;
@@ -457,7 +458,12 @@ public void testSnapshotVerifyRepository() throws IOException {
457458
List<VerifyRepositoryResponse.NodeView> repositoryMetaDataResponse = response.getNodes();
458459
// end::verify-repository-response
459460
assertThat(1, equalTo(repositoryMetaDataResponse.size()));
460-
assertThat("integTest-0", equalTo(repositoryMetaDataResponse.get(0).getName()));
461+
final boolean async = Booleans.parseBoolean(System.getProperty("tests.rest.async", "false"));
462+
if (async) {
463+
assertThat("asyncIntegTest-0", equalTo(repositoryMetaDataResponse.get(0).getName()));
464+
} else {
465+
assertThat("integTest-0", equalTo(repositoryMetaDataResponse.get(0).getName()));
466+
}
461467
}
462468

463469
public void testSnapshotVerifyRepositoryAsync() throws InterruptedException {

0 commit comments

Comments
 (0)