Skip to content

Commit

Permalink
[TESTS] Make DisabledFieldDataFormatTests less evil.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpountz committed Aug 14, 2014
1 parent a4f974d commit feefc41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,16 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.MapperService.SmartNameFieldMappers;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;

import java.util.Set;
import java.util.concurrent.Callable;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

@ClusterScope(randomDynamicTemplates = false)
public class DisabledFieldDataFormatTests extends ElasticsearchIntegrationTest {

@Override
protected int numberOfReplicas() {
return 0;
}
public class DisabledFieldDataFormatTests extends ElasticsearchSingleNodeTest {

public void test() throws Exception {
prepareCreate("test").addMapping("type", "s", "type=string").execute().actionGet();
ensureGreen();
createIndex("test", ImmutableSettings.EMPTY, "type", "s", "type=string");
logger.info("indexing data start");
for (int i = 0; i < 10; ++i) {
client().prepareIndex("test", "type", Integer.toString(i)).setSource("s", "value" + i).execute().actionGet();
Expand All @@ -57,7 +41,7 @@ public void test() throws Exception {

final int searchCycles = 1;

refresh();
client().admin().indices().prepareRefresh().execute().actionGet();

// disable field data
updateFormat("disabled");
Expand Down Expand Up @@ -97,7 +81,7 @@ public void test() throws Exception {

// but add more docs and the new segment won't be loaded
client().prepareIndex("test", "type", "-1").setSource("s", "value").execute().actionGet();
refresh();
client().admin().indices().prepareRefresh().execute().actionGet();
for (int i = 0; i < searchCycles; i++) {
try {
resp = client().prepareSearch("test").setPreference(Integer.toString(i)).addAggregation(AggregationBuilders.terms("t").field("s")
Expand All @@ -124,24 +108,6 @@ private void updateFormat(final String format) throws Exception {
.endObject()
.endObject()).get());
logger.info(">> put mapping end {}", format);
assertBusy(new Callable<Object>() {
@Override
public Object call() throws Exception {
Set<String> nodes = internalCluster().nodesInclude("test");
assertFalse(nodes.isEmpty());
for (String node : nodes) {
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, node);
IndexService indexService = indicesService.indexService("test");
assertThat(indexService, notNullValue());
final SmartNameFieldMappers mappers = indexService.mapperService().smartName("s");
assertThat(mappers, notNullValue());
assertTrue(mappers.hasMapper());
final String currentFormat = mappers.mapper().fieldDataType().getFormat(ImmutableSettings.EMPTY);
assertThat(currentFormat, equalTo(format));
}
return null;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.cache.recycler.PageCacheRecycler;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.metadata.IndexMetaData;
Expand Down Expand Up @@ -60,8 +61,8 @@ private static class Holder {
}

static void cleanup() {
assertAcked(Holder.NODE.client().admin().indices().prepareDelete("*").get());
MetaData metaData = Holder.NODE.client().admin().cluster().prepareState().get().getState().getMetaData();
assertAcked(client().admin().indices().prepareDelete("*").get());
MetaData metaData = client().admin().cluster().prepareState().get().getState().getMetaData();
assertThat("test leaves persistent cluster metadata behind: " + metaData.persistentSettings().getAsMap(),
metaData.persistentSettings().getAsMap().size(), equalTo(0));
assertThat("test leaves transient cluster metadata behind: " + metaData.transientSettings().getAsMap(),
Expand Down Expand Up @@ -89,6 +90,13 @@ private static Node newNode() {
return build;
}

/**
* Returns a client to the single-node cluster.
*/
public static Client client() {
return Holder.NODE.client();
}

/**
* Returns the single test nodes name.
*/
Expand Down Expand Up @@ -128,21 +136,36 @@ protected static IndexService createIndex(String index) {
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings) {
return createIndex(index, settings, null, null);
return createIndex(index, settings, null, (XContentBuilder) null);
}

/**
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings, String type, XContentBuilder mappings) {
CreateIndexRequestBuilder createIndexRequestBuilder = Holder.NODE.client().admin().indices().prepareCreate(index).setSettings(settings);
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
if (type != null && mappings != null) {
createIndexRequestBuilder.addMapping(type, mappings);
}
return createIndex(index, createIndexRequestBuilder);
}

/**
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings, String type, Object... mappings) {
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
if (type != null && mappings != null) {
createIndexRequestBuilder.addMapping(type, mappings);
}
return createIndex(index, createIndexRequestBuilder);
}

private static IndexService createIndex(String index, CreateIndexRequestBuilder createIndexRequestBuilder) {
assertAcked(createIndexRequestBuilder.get());
// Wait for the index to be allocated so that cluster state updates don't override
// changes that would have been done locally
ClusterHealthResponse health = Holder.NODE.client().admin().cluster()
ClusterHealthResponse health = client().admin().cluster()
.health(Requests.clusterHealthRequest(index).waitForYellowStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
assertThat(health.getStatus(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW));
assertThat("Cluster must be a single node cluster", health.getNumberOfDataNodes(), equalTo(1));
Expand Down

0 comments on commit feefc41

Please sign in to comment.