diff options
author | Christoph Büscher <christoph@elastic.co> | 2017-05-16 11:54:02 +0200 |
---|---|---|
committer | Christoph Büscher <christoph@elastic.co> | 2017-05-16 11:54:02 +0200 |
commit | 059b23e92ee34ab064b24d3d4bd8cf9f5bced353 (patch) | |
tree | 22e190488188712b8f1d5108119f99fb144d99ed /core/src/test/java | |
parent | 5fb04fa6038cf1a7f348b6de5a6d826a96fcafba (diff) | |
parent | 8c6b5a953e2b3497f495c5947669c494a6af8639 (diff) |
Merge branch 'master' into feature/client_aggs_parsing
Diffstat (limited to 'core/src/test/java')
9 files changed, 232 insertions, 43 deletions
diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java index 84c9518051..0d879e4813 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java @@ -31,6 +31,7 @@ import java.io.UncheckedIOException; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import static org.hamcrest.Matchers.hasSize; @@ -108,7 +109,6 @@ public class ObjectParserTests extends ESTestCase { s = objectParser.parse(parser, null); assertNotNull(s.object); assertEquals(s.object.test, 0); - } /** @@ -353,7 +353,11 @@ public class ObjectParserTests extends ESTestCase { builder.field("string_array_field", "5"); } boolean nullValue = randomBoolean(); - builder.field("boolean_field", nullValue); + if (randomBoolean()) { + builder.field("boolean_field", nullValue); + } else { + builder.field("boolean_field", Boolean.toString(nullValue)); + } builder.field("string_or_null", nullValue ? null : "5"); builder.endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string()); @@ -424,19 +428,19 @@ public class ObjectParserTests extends ESTestCase { objectParser.declareStringOrNull(TestStruct::setString_or_null, new ParseField("string_or_null")); objectParser.declareBoolean(TestStruct::setNull_value, new ParseField("boolean_field")); TestStruct parse = objectParser.parse(parser, new TestStruct(), null); - assertArrayEquals(parse.double_array_field.toArray(), Arrays.asList(2.1d).toArray()); + assertArrayEquals(parse.double_array_field.toArray(), Collections.singletonList(2.1d).toArray()); assertEquals(parse.double_field, 2.1d, 0.0d); - assertArrayEquals(parse.long_array_field.toArray(), Arrays.asList(4L).toArray()); + assertArrayEquals(parse.long_array_field.toArray(), Collections.singletonList(4L).toArray()); assertEquals(parse.long_field, 4L); - assertArrayEquals(parse.string_array_field.toArray(), Arrays.asList("5").toArray()); + assertArrayEquals(parse.string_array_field.toArray(), Collections.singletonList("5").toArray()); assertEquals(parse.string_field, "5"); - assertArrayEquals(parse.int_array_field.toArray(), Arrays.asList(1).toArray()); + assertArrayEquals(parse.int_array_field.toArray(), Collections.singletonList(1).toArray()); assertEquals(parse.int_field, 1); - assertArrayEquals(parse.float_array_field.toArray(), Arrays.asList(3.1f).toArray()); + assertArrayEquals(parse.float_array_field.toArray(), Collections.singletonList(3.1f).toArray()); assertEquals(parse.float_field, 3.1f, 0.0f); assertEquals(nullValue, parse.null_value); diff --git a/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java index c321ffa965..d19e8e32ff 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.index.Fields; import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.memory.MemoryIndex; +import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchException; @@ -61,6 +62,7 @@ import java.util.stream.Stream; import static org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.instanceOf; public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLikeThisQueryBuilder> { @@ -264,6 +266,13 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik protected void doAssertLuceneQuery(MoreLikeThisQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { if (queryBuilder.likeItems() != null && queryBuilder.likeItems().length > 0) { assertThat(query, instanceOf(BooleanQuery.class)); + BooleanQuery booleanQuery = (BooleanQuery) query; + for (BooleanClause booleanClause : booleanQuery) { + if (booleanClause.getQuery() instanceof MoreLikeThisQuery) { + MoreLikeThisQuery moreLikeThisQuery = (MoreLikeThisQuery) booleanClause.getQuery(); + assertThat(moreLikeThisQuery.getLikeFields().length, greaterThan(0)); + } + } } else { // we rely on integration tests for a deeper check here assertThat(query, instanceOf(MoreLikeThisQuery.class)); @@ -310,6 +319,12 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik assertEquals(expectedItem, newItem); } + public void testItemCopy() throws IOException { + Item expectedItem = generateRandomItem(); + Item newItem = new Item(expectedItem); + assertEquals(expectedItem, newItem); + } + public void testItemFromXContent() throws IOException { Item expectedItem = generateRandomItem(); String json = expectedItem.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).string(); diff --git a/core/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java b/core/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java index f0f38ba48c..7542545bc3 100644 --- a/core/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java +++ b/core/src/test/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java @@ -77,6 +77,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; import static org.elasticsearch.node.RecoverySettingsChunkSizePlugin.CHUNK_SIZE_SETTING; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -348,39 +349,28 @@ public class IndexRecoveryIT extends ESIntegTestCase { assertRecoveryState(recoveryStates.get(0), 0, PeerRecoverySource.INSTANCE, true, Stage.DONE, nodeA, nodeB); validateIndexRecoveryState(recoveryStates.get(0).getIndex()); - - statsResponse = client().admin().cluster().prepareNodesStats().clear().setIndices(new CommonStatsFlags(CommonStatsFlags.Flag.Recovery)).get(); - assertThat(statsResponse.getNodes(), hasSize(2)); - for (NodeStats nodeStats : statsResponse.getNodes()) { + Consumer<String> assertNodeHasThrottleTimeAndNoRecoveries = nodeName -> { + NodesStatsResponse nodesStatsResponse = client().admin().cluster().prepareNodesStats().setNodesIds(nodeName) + .clear().setIndices(new CommonStatsFlags(CommonStatsFlags.Flag.Recovery)).get(); + assertThat(nodesStatsResponse.getNodes(), hasSize(1)); + NodeStats nodeStats = nodesStatsResponse.getNodes().get(0); final RecoveryStats recoveryStats = nodeStats.getIndices().getRecoveryStats(); assertThat(recoveryStats.currentAsSource(), equalTo(0)); assertThat(recoveryStats.currentAsTarget(), equalTo(0)); - if (nodeStats.getNode().getName().equals(nodeA)) { - assertThat("node A throttling should be >0", recoveryStats.throttleTime().millis(), greaterThan(0L)); - } - if (nodeStats.getNode().getName().equals(nodeB)) { - assertThat("node B throttling should be >0 ", recoveryStats.throttleTime().millis(), greaterThan(0L)); - } - } + assertThat(nodeName + " throttling should be >0", recoveryStats.throttleTime().millis(), greaterThan(0L)); + }; + // we have to use assertBusy as recovery counters are decremented only when the last reference to the RecoveryTarget + // is decremented, which may happen after the recovery was done. + assertBusy(() -> assertNodeHasThrottleTimeAndNoRecoveries.accept(nodeA)); + assertBusy(() -> assertNodeHasThrottleTimeAndNoRecoveries.accept(nodeB)); logger.info("--> bump replica count"); client().admin().indices().prepareUpdateSettings(INDEX_NAME) .setSettings(Settings.builder().put("number_of_replicas", 1)).execute().actionGet(); ensureGreen(); - statsResponse = client().admin().cluster().prepareNodesStats().clear().setIndices(new CommonStatsFlags(CommonStatsFlags.Flag.Recovery)).get(); - assertThat(statsResponse.getNodes(), hasSize(2)); - for (NodeStats nodeStats : statsResponse.getNodes()) { - final RecoveryStats recoveryStats = nodeStats.getIndices().getRecoveryStats(); - assertThat(recoveryStats.currentAsSource(), equalTo(0)); - assertThat(recoveryStats.currentAsTarget(), equalTo(0)); - if (nodeStats.getNode().getName().equals(nodeA)) { - assertThat("node A throttling should be >0", recoveryStats.throttleTime().millis(), greaterThan(0L)); - } - if (nodeStats.getNode().getName().equals(nodeB)) { - assertThat("node B throttling should be >0 ", recoveryStats.throttleTime().millis(), greaterThan(0L)); - } - } + assertBusy(() -> assertNodeHasThrottleTimeAndNoRecoveries.accept(nodeA)); + assertBusy(() -> assertNodeHasThrottleTimeAndNoRecoveries.accept(nodeB)); logger.info("--> start node C"); String nodeC = internalCluster().startNode(); diff --git a/core/src/test/java/org/elasticsearch/script/ScriptModesTests.java b/core/src/test/java/org/elasticsearch/script/ScriptModesTests.java index 63bd3fd377..33d8c89ecd 100644 --- a/core/src/test/java/org/elasticsearch/script/ScriptModesTests.java +++ b/core/src/test/java/org/elasticsearch/script/ScriptModesTests.java @@ -97,14 +97,14 @@ public class ScriptModesTests extends ESTestCase { } public void testDefaultSettings() { - this.scriptModes = new ScriptModes(scriptSettings, Settings.EMPTY); + this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, Settings.EMPTY); assertScriptModesAllOps(true, ScriptType.FILE); assertScriptModesAllOps(false, ScriptType.STORED, ScriptType.INLINE); } public void testMissingSetting() { assertAllSettingsWereChecked = false; - this.scriptModes = new ScriptModes(scriptSettings, Settings.EMPTY); + this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, Settings.EMPTY); try { scriptModes.getScriptEnabled("non_existing", randomFrom(ScriptType.values()), randomFrom(scriptContexts)); fail("Expected IllegalArgumentException"); @@ -131,7 +131,7 @@ public class ScriptModesTests extends ESTestCase { builder.put("script" + "." + randomScriptTypes[i].getName(), randomScriptModes[i]); deprecated.add("script" + "." + randomScriptTypes[i].getName()); } - this.scriptModes = new ScriptModes(scriptSettings, builder.build()); + this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, builder.build()); for (int i = 0; i < randomInt; i++) { assertScriptModesAllOps(randomScriptModes[i], randomScriptTypes[i]); @@ -167,7 +167,7 @@ public class ScriptModesTests extends ESTestCase { builder.put("script" + "." + randomScriptContexts[i].getKey(), randomScriptModes[i]); deprecated.add("script" + "." + randomScriptContexts[i].getKey()); } - this.scriptModes = new ScriptModes(scriptSettings, builder.build()); + this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, builder.build()); for (int i = 0; i < randomInt; i++) { assertScriptModesAllTypes(randomScriptModes[i], randomScriptContexts[i]); @@ -187,7 +187,7 @@ public class ScriptModesTests extends ESTestCase { .put("script.stored", "true") .put("script.inline", "true"); //operations generic settings have precedence over script type generic settings - this.scriptModes = new ScriptModes(scriptSettings, builder.build()); + this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, builder.build()); assertScriptModesAllTypes(false, scriptContext); ScriptContext[] complementOf = complementOf(scriptContext); assertScriptModes(true, new ScriptType[]{ScriptType.FILE, ScriptType.STORED}, complementOf); diff --git a/core/src/test/java/org/elasticsearch/script/ScriptServiceTests.java b/core/src/test/java/org/elasticsearch/script/ScriptServiceTests.java index c4fd39550b..55cf11c986 100644 --- a/core/src/test/java/org/elasticsearch/script/ScriptServiceTests.java +++ b/core/src/test/java/org/elasticsearch/script/ScriptServiceTests.java @@ -216,6 +216,69 @@ public class ScriptServiceTests extends ESTestCase { assertThat(compiledScript1.compiled(), sameInstance(compiledScript2.compiled())); } + public void testAllowAllScriptTypeSettings() throws IOException { + buildScriptService(Settings.EMPTY); + + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileAccepted("painless", "script", ScriptType.STORED, ScriptContext.Standard.SEARCH); + } + + public void testAllowAllScriptContextSettings() throws IOException { + buildScriptService(Settings.EMPTY); + + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.AGGS); + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.UPDATE); + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.INGEST); + } + + public void testAllowSomeScriptTypeSettings() throws IOException { + Settings.Builder builder = Settings.builder(); + builder.put("script.types_allowed", "inline"); + builder.put("script.engine.painless.stored", false); + buildScriptService(builder.build()); + + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileRejected("painless", "script", ScriptType.STORED, ScriptContext.Standard.SEARCH); + + assertSettingDeprecationsAndWarnings( + ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.engine.painless.stored")); + } + + public void testAllowSomeScriptContextSettings() throws IOException { + Settings.Builder builder = Settings.builder(); + builder.put("script.contexts_allowed", "search, aggs"); + builder.put("script.update", false); + buildScriptService(builder.build()); + + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileAccepted("painless", "script", ScriptType.INLINE, ScriptContext.Standard.AGGS); + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.UPDATE); + + assertSettingDeprecationsAndWarnings( + ScriptSettingsTests.buildDeprecatedSettingsArray(scriptSettings, "script.update")); + } + + public void testAllowNoScriptTypeSettings() throws IOException { + Settings.Builder builder = Settings.builder(); + builder.put("script.types_allowed", ""); + buildScriptService(builder.build()); + + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileRejected("painless", "script", ScriptType.STORED, ScriptContext.Standard.SEARCH); + } + + public void testAllowNoScriptContextSettings() throws IOException { + Settings.Builder builder = Settings.builder(); + builder.put("script.contexts_allowed", ""); + buildScriptService(builder.build()); + + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.SEARCH); + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.AGGS); + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.UPDATE); + assertCompileRejected("painless", "script", ScriptType.INLINE, ScriptContext.Standard.INGEST); + } + public void testDefaultBehaviourFineGrainedSettings() throws IOException { Settings.Builder builder = Settings.builder(); //rarely inject the default settings, which have no effect @@ -345,7 +408,7 @@ public class ScriptServiceTests extends ESTestCase { do { pluginName = randomAlphaOfLength(randomIntBetween(1, 10)); unknownContext = randomAlphaOfLength(randomIntBetween(1, 30)); - } while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext))); + } while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext).getKey())); String type = scriptEngine.getType(); try { @@ -491,8 +554,8 @@ public class ScriptServiceTests extends ESTestCase { try { scriptService.compile(new Script(scriptType, lang, script, Collections.emptyMap()), scriptContext); fail("compile should have been rejected for lang [" + lang + "], script_type [" + scriptType + "], scripted_op [" + scriptContext + "]"); - } catch(IllegalStateException e) { - //all good + } catch (IllegalArgumentException | IllegalStateException e) { + // pass } } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/adjacency/InternalAdjacencyMatrixTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/adjacency/InternalAdjacencyMatrixTests.java new file mode 100644 index 0000000000..53ae1db454 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/adjacency/InternalAdjacencyMatrixTests.java @@ -0,0 +1,92 @@ +/* + * 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.search.aggregations.bucket.adjacency; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.test.InternalAggregationTestCase; +import org.junit.Before; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class InternalAdjacencyMatrixTests extends InternalAggregationTestCase<InternalAdjacencyMatrix> { + + private List<String> keys; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + keys = new ArrayList<>(); + int numFilters = randomIntBetween(2, 4); + String[] filters = new String[numFilters]; + for (int i = 0; i < numFilters; i++) { + filters[i] = randomAlphaOfLength(5); + } + for (int i = 0; i < filters.length; i++) { + keys.add(filters[i]); + for (int j = i + 1; j < filters.length; j++) { + if (filters[i].compareTo(filters[j]) <= 0) { + keys.add(filters[i] + "&" + filters[j]); + } else { + keys.add(filters[j] + "&" + filters[i]); + } + } + } + } + + @Override + protected InternalAdjacencyMatrix createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, + Map<String, Object> metaData) { + final List<InternalAdjacencyMatrix.InternalBucket> buckets = new ArrayList<>(); + for (int i = 0; i < keys.size(); ++i) { + String key = keys.get(i); + int docCount = randomIntBetween(0, 1000); + buckets.add(new InternalAdjacencyMatrix.InternalBucket(key, docCount, InternalAggregations.EMPTY)); + } + return new InternalAdjacencyMatrix(name, buckets, pipelineAggregators, metaData); + } + + @Override + protected void assertReduced(InternalAdjacencyMatrix reduced, List<InternalAdjacencyMatrix> inputs) { + final Map<String, Long> expectedCounts = new TreeMap<>(); + for (InternalAdjacencyMatrix input : inputs) { + for (InternalAdjacencyMatrix.InternalBucket bucket : input.getBuckets()) { + expectedCounts.compute(bucket.getKeyAsString(), + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); + } + } + final Map<String, Long> actualCounts = new TreeMap<>(); + for (InternalAdjacencyMatrix.InternalBucket bucket : reduced.getBuckets()) { + actualCounts.compute(bucket.getKeyAsString(), + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); + } + assertEquals(expectedCounts, actualCounts); + } + + @Override + protected Reader<InternalAdjacencyMatrix> instanceReader() { + return InternalAdjacencyMatrix::new; + } +} diff --git a/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java b/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java index 0d2c5cf1bd..8d4f2921f2 100644 --- a/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java +++ b/core/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java @@ -623,4 +623,18 @@ public class MoreLikeThisIT extends ESIntegTestCase { assertSearchResponse(response); assertHitCount(response, 1); } + + public void testWithRouting() throws IOException { + client().prepareIndex("index", "type", "1").setRouting("3").setSource("text", "this is a document").get(); + client().prepareIndex("index", "type", "2").setRouting("1").setSource("text", "this is another document").get(); + client().prepareIndex("index", "type", "3").setRouting("4").setSource("text", "this is yet another document").get(); + refresh("index"); + + Item item = new Item("index", "type", "2").routing("1"); + MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = new MoreLikeThisQueryBuilder(new String[]{"text"}, null, new Item[]{item}); + moreLikeThisQueryBuilder.minTermFreq(1); + moreLikeThisQueryBuilder.minDocFreq(1); + SearchResponse searchResponse = client().prepareSearch("index").setQuery(moreLikeThisQueryBuilder).get(); + assertEquals(2, searchResponse.getHits().totalHits); + } } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java b/core/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java index 82b1dc3656..864f060be0 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java @@ -1156,7 +1156,7 @@ public class SuggestSearchIT extends ESIntegTestCase { @Override public Object run() { - return new BytesArray(result); + return result; } }; } diff --git a/core/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java b/core/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java index f7ab29c95c..38e73c209a 100644 --- a/core/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java +++ b/core/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java @@ -498,7 +498,7 @@ public class RemoteClusterConnectionTests extends ESTestCase { barrier.await(); CountDownLatch latch = new CountDownLatch(numConnectionAttempts); for (int i = 0; i < numConnectionAttempts; i++) { - AtomicReference<RuntimeException> executed = new AtomicReference<>(); + AtomicReference<Exception> executed = new AtomicReference<>(); ActionListener<Void> listener = ActionListener.wrap( x -> { if (executed.compareAndSet(null, new RuntimeException())) { @@ -508,10 +508,21 @@ public class RemoteClusterConnectionTests extends ESTestCase { } }, x -> { - if (executed.compareAndSet(null, new RuntimeException())) { + if (executed.compareAndSet(null, x)) { latch.countDown(); } else { - throw new AssertionError("shit's been called twice", executed.get()); + final String message = x.getMessage(); + if ((executed.get().getClass() == x.getClass() + && "operation was cancelled reason [connect handler is closed]".equals(message) + && message.equals(executed.get().getMessage())) == false) { + // we do cancel the operation and that means that if timing allows it, the caller + // of a blocking call as well as the handler will get the exception from the + // ExecutionCancelledException concurrently. unless that is the case we fail + // if we get called more than once! + AssertionError assertionError = new AssertionError("shit's been called twice", x); + assertionError.addSuppressed(executed.get()); + throw assertionError; + } } if (x instanceof RejectedExecutionException || x instanceof AlreadyClosedException || x instanceof CancellableThreads.ExecutionCancelledException) { |