Skip to content

Commit 3ee6255

Browse files
committed
[TEST] properly wait for mapping on master node
add helper method to do so, by not assuming that the mapping will exists right away by waiting for green or refreshing...
1 parent b72094b commit 3ee6255

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/test/java/org/elasticsearch/indices/mapping/SimpleDeleteMappingTests.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919

2020
package org.elasticsearch.indices.mapping;
2121

22+
import com.google.common.base.Predicate;
2223
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
2425
import org.elasticsearch.action.count.CountResponse;
2526
import org.elasticsearch.cluster.ClusterState;
27+
import org.elasticsearch.cluster.metadata.MappingMetaData;
28+
import org.elasticsearch.common.collect.ImmutableOpenMap;
2629
import org.elasticsearch.test.ElasticsearchIntegrationTest;
2730
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
2831
import org.junit.Test;
@@ -53,24 +56,33 @@ public void simpleDeleteMapping() throws Exception {
5356
assertThat(countResponse.getCount(), equalTo(10l));
5457
}
5558

56-
ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
57-
58-
assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(true));
59+
waitForMappingOnMaster("test", "type1");
5960

6061
GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings("test").setTypes("type1").execute().actionGet();
6162
assertThat(mappingsResponse.getMappings().get("test").get("type1"), notNullValue());
6263

63-
ElasticsearchAssertions.assertAcked(client().admin().indices().prepareDeleteMapping().setIndices("test").setType("type1"));
64+
assertAcked(client().admin().indices().prepareDeleteMapping().setIndices("test").setType("type1"));
6465

6566
for (int i = 0; i < 10; i++) {
6667
CountResponse countResponse = client().prepareCount().setQuery(matchAllQuery()).execute().actionGet();
6768
assertThat(countResponse.getCount(), equalTo(0l));
6869
}
6970

70-
clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
71-
assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(false));
72-
mappingsResponse = client().admin().indices().prepareGetMappings("test").setTypes("type1").execute().actionGet();
73-
assertThat(mappingsResponse.getMappings().get("test"), nullValue());
71+
boolean applied = awaitBusy(new Predicate<Object>() {
72+
@Override
73+
public boolean apply(Object input) {
74+
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").setTypes("type1").get();
75+
ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get("test");
76+
if (mappings == null) {
77+
return true;
78+
}
79+
return !mappings.containsKey("type1");
80+
}
81+
});
82+
if (!applied) {
83+
fail("failed to wait for the mapping to be removed from the master cluster state");
84+
}
85+
7486
}
7587

7688

src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
4242
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
4343
import org.elasticsearch.action.admin.indices.flush.FlushResponse;
44+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
4445
import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse;
4546
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
4647
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
@@ -55,13 +56,15 @@
5556
import org.elasticsearch.client.Client;
5657
import org.elasticsearch.client.Requests;
5758
import org.elasticsearch.cluster.ClusterService;
59+
import org.elasticsearch.cluster.metadata.MappingMetaData;
5860
import org.elasticsearch.cluster.metadata.MetaData;
5961
import org.elasticsearch.cluster.node.DiscoveryNode;
6062
import org.elasticsearch.cluster.routing.MutableShardRouting;
6163
import org.elasticsearch.cluster.routing.ShardRoutingState;
6264
import org.elasticsearch.common.Nullable;
6365
import org.elasticsearch.common.Priority;
6466
import org.elasticsearch.common.Strings;
67+
import org.elasticsearch.common.collect.ImmutableOpenMap;
6568
import org.elasticsearch.common.collect.Tuple;
6669
import org.elasticsearch.common.settings.ImmutableSettings;
6770
import org.elasticsearch.common.settings.Settings;
@@ -807,6 +810,26 @@ public boolean apply(Object input) {
807810
}
808811
}
809812

813+
/**
814+
* Waits for the given mapping type to exists on the master node.
815+
*/
816+
public void waitForMappingOnMaster(final String index, final String type) throws InterruptedException {
817+
boolean applied = awaitBusy(new Predicate<Object>() {
818+
@Override
819+
public boolean apply(Object input) {
820+
GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).setTypes(type).get();
821+
ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get(index);
822+
if (mappings == null) {
823+
return false;
824+
}
825+
return mappings.containsKey(type);
826+
}
827+
});
828+
if (!applied) {
829+
fail("failed to find mappings for index " + index + ", type " + type + " on master node");
830+
}
831+
}
832+
810833
/**
811834
* Restricts the given index to be allocated on <code>n</code> nodes using the allocation deciders.
812835
* Yet if the shards can't be allocated on any other node shards for this index will remain allocated on

0 commit comments

Comments
 (0)