[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...
This commit is contained in:
Shay Banon 2014-06-30 23:11:23 +02:00
parent 5c5e13abce
commit e8519084c9
2 changed files with 43 additions and 8 deletions

View File

@ -19,10 +19,13 @@
package org.elasticsearch.indices.mapping;
import com.google.common.base.Predicate;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.junit.Test;
@ -53,24 +56,33 @@ public class SimpleDeleteMappingTests extends ElasticsearchIntegrationTest {
assertThat(countResponse.getCount(), equalTo(10l));
}
ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(true));
waitForMappingOnMaster("test", "type1");
GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings("test").setTypes("type1").execute().actionGet();
assertThat(mappingsResponse.getMappings().get("test").get("type1"), notNullValue());
ElasticsearchAssertions.assertAcked(client().admin().indices().prepareDeleteMapping().setIndices("test").setType("type1"));
assertAcked(client().admin().indices().prepareDeleteMapping().setIndices("test").setType("type1"));
for (int i = 0; i < 10; i++) {
CountResponse countResponse = client().prepareCount().setQuery(matchAllQuery()).execute().actionGet();
assertThat(countResponse.getCount(), equalTo(0l));
}
clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
assertThat(clusterState.metaData().index("test").mappings().containsKey("type1"), equalTo(false));
mappingsResponse = client().admin().indices().prepareGetMappings("test").setTypes("type1").execute().actionGet();
assertThat(mappingsResponse.getMappings().get("test"), nullValue());
boolean applied = awaitBusy(new Predicate<Object>() {
@Override
public boolean apply(Object input) {
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").setTypes("type1").get();
ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get("test");
if (mappings == null) {
return true;
}
return !mappings.containsKey("type1");
}
});
if (!applied) {
fail("failed to wait for the mapping to be removed from the master cluster state");
}
}

View File

@ -41,6 +41,7 @@ import org.elasticsearch.action.admin.cluster.tasks.PendingClusterTasksResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.flush.FlushResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
@ -55,6 +56,7 @@ import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.MutableShardRouting;
@ -62,6 +64,7 @@ import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
@ -803,6 +806,26 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
}
}
/**
* Waits for the given mapping type to exists on the master node.
*/
public void waitForMappingOnMaster(final String index, final String type) throws InterruptedException {
boolean applied = awaitBusy(new Predicate<Object>() {
@Override
public boolean apply(Object input) {
GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).setTypes(type).get();
ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get(index);
if (mappings == null) {
return false;
}
return mappings.containsKey(type);
}
});
if (!applied) {
fail("failed to find mappings for index " + index + ", type " + type + " on master node");
}
}
/**
* Restricts the given index to be allocated on <code>n</code> nodes using the allocation deciders.
* Yet if the shards can't be allocated on any other node shards for this index will remain allocated on