- Cut DeletePercolatorTypeTests and TTLPercolatorTests over to the AbstractIntegrationTest.

This commit is contained in:
Martijn van Groningen 2013-09-20 15:08:06 +02:00
parent c3ecd6fd1b
commit f6c2d9caf3
3 changed files with 89 additions and 141 deletions

View File

@ -1,111 +0,0 @@
package org.elasticsearch.percolator;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.percolate.PercolateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Priority;
import org.elasticsearch.index.mapper.DocumentTypeListener;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.test.AbstractNodesTests;
import org.junit.Test;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import static org.elasticsearch.action.percolate.PercolateSourceBuilder.docBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.equalTo;
/**
*/
public class DeletePercolatorTypeTests extends AbstractNodesTests {
public void beforeClass() throws Exception {
startNode("node1");
startNode("node2");
}
@Test
public void testDeletePercolatorType() throws Exception {
DeleteIndexResponse deleteIndexResponse = client().admin().indices().prepareDelete().execute().actionGet();
assertThat("Delete Index failed - not acked", deleteIndexResponse.isAcknowledged(), equalTo(true));
ensureGreen();
client().admin().indices().prepareCreate("test1").execute().actionGet();
client().admin().indices().prepareCreate("test2").execute().actionGet();
ensureGreen();
client().prepareIndex("test1", "_percolator", "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
client().prepareIndex("test2", "_percolator", "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
PercolateResponse response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertThat(response.getCount(), equalTo(2l));
CountDownLatch test1Latch = createCountDownLatch("test1");
CountDownLatch test2Latch =createCountDownLatch("test2");
client().admin().indices().prepareDeleteMapping("test1").setType("_percolator").execute().actionGet();
test1Latch.await();
response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(1l));
client().admin().indices().prepareDeleteMapping("test2").setType("_percolator").execute().actionGet();
test2Latch.await();
// Percolate api should return 0 matches, because all _percolate types have been removed.
response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(0l));
}
private CountDownLatch createCountDownLatch(String index) {
List<Node> nodes = nodes();
final CountDownLatch latch = new CountDownLatch(nodes.size());
for (Node node : nodes) {
IndicesService indexServices = ((InternalNode) node).injector().getInstance(IndicesService.class);
MapperService mapperService = indexServices.indexService(index).mapperService();
mapperService.addTypeListener(new DocumentTypeListener() {
@Override
public void created(String type) {
}
@Override
public void removed(String type) {
latch.countDown();
}
});
}
return latch;
}
public ClusterHealthStatus ensureGreen() {
ClusterHealthResponse actionGet = client().admin().cluster()
.health(Requests.clusterHealthRequest().waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
assertThat(actionGet.isTimedOut(), equalTo(false));
assertThat(actionGet.getStatus(), equalTo(ClusterHealthStatus.GREEN));
return actionGet.getStatus();
}
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.percolator;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.percolate.PercolateResponse;
@ -38,14 +39,18 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.engine.DocumentMissingException;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.index.mapper.DocumentTypeListener;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.factor.FactorBuilder;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import static org.elasticsearch.action.percolate.PercolateSourceBuilder.docBuilder;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
@ -1450,6 +1455,73 @@ public class PercolatorTests extends AbstractIntegrationTest {
assertThat(matches[4].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown <em>fox</em> jumps over the lazy dog"));
}
@Test
public void testDeletePercolatorType() throws Exception {
DeleteIndexResponse deleteIndexResponse = client().admin().indices().prepareDelete().execute().actionGet();
assertThat("Delete Index failed - not acked", deleteIndexResponse.isAcknowledged(), equalTo(true));
ensureGreen();
client().admin().indices().prepareCreate("test1").execute().actionGet();
client().admin().indices().prepareCreate("test2").execute().actionGet();
ensureGreen();
client().prepareIndex("test1", "_percolator", "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
client().prepareIndex("test2", "_percolator", "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
PercolateResponse response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertThat(response.getCount(), equalTo(2l));
CountDownLatch test1Latch = createCountDownLatch("test1");
CountDownLatch test2Latch =createCountDownLatch("test2");
client().admin().indices().prepareDeleteMapping("test1").setType("_percolator").execute().actionGet();
test1Latch.await();
response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(1l));
client().admin().indices().prepareDeleteMapping("test2").setType("_percolator").execute().actionGet();
test2Latch.await();
// Percolate api should return 0 matches, because all _percolate types have been removed.
response = client().preparePercolate()
.setIndices("test1", "test2").setDocumentType("type").setOnlyCount(true)
.setPercolateDoc(docBuilder().setDoc(jsonBuilder().startObject().field("field1", "b").endObject()))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(0l));
}
private CountDownLatch createCountDownLatch(String index) {
final CountDownLatch latch = new CountDownLatch(cluster().numNodes());
Iterable<IndicesService> mapperServices = cluster().getInstances(IndicesService.class);
for (IndicesService indicesService : mapperServices) {
MapperService mapperService = indicesService.indexService(index).mapperService();
mapperService.addTypeListener(new DocumentTypeListener() {
@Override
public void created(String type) {
}
@Override
public void removed(String type) {
latch.countDown();
}
});
}
return latch;
}
public static String[] convertFromTextArray(PercolateResponse.Match[] matches, String index) {
if (matches.length == 0) {
return Strings.EMPTY_ARRAY;

View File

@ -1,17 +1,14 @@
package org.elasticsearch.percolator;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.percolate.PercolateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.AbstractNodesTests;
import org.junit.After;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.Before;
import org.junit.Test;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
@ -22,21 +19,23 @@ import static org.hamcrest.Matchers.*;
/**
*/
public class TTLPercolatorTests extends AbstractNodesTests {
public class TTLPercolatorTests extends AbstractIntegrationTest {
private long purgeInterval = 200;
private Settings ttlSettings = ImmutableSettings.settingsBuilder()
.put("indices.ttl.interval", purgeInterval)
.build();
@Before
public void setup() {
updateClusterSettings(ttlSettings);
}
@Test
public void testPercolatingWithTimeToLive() throws Exception {
long purgeInterval = 200;
Settings settings = settingsBuilder()
.put("gateway.type", "none")
.put("indices.ttl.interval", purgeInterval).build(); // <-- For testing ttl.
logger.info("--> starting 2 nodes");
startNode("node1", settings);
startNode("node2", settings);
Client client = client("node1");
Client client = client();
client.admin().indices().prepareDelete().execute().actionGet();
ensureGreen(client);
ensureGreen();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("_percolator")
.startObject("_ttl").field("enabled", true).endObject()
@ -48,7 +47,7 @@ public class TTLPercolatorTests extends AbstractNodesTests {
.addMapping("_percolator", mapping)
.addMapping("type1", mapping)
.execute().actionGet();
ensureGreen(client);
ensureGreen();
long ttl = 1500;
long now = System.currentTimeMillis();
@ -121,16 +120,4 @@ public class TTLPercolatorTests extends AbstractNodesTests {
assertThat(percolateResponse.getMatches(), emptyArray());
}
@After
public void cleanAndCloseNodes() throws Exception {
closeAllNodes();
}
public static void ensureGreen(Client client) {
ClusterHealthResponse actionGet = client.admin().cluster()
.health(Requests.clusterHealthRequest().waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
assertThat(actionGet.isTimedOut(), equalTo(false));
assertThat(actionGet.getStatus(), equalTo(ClusterHealthStatus.GREEN));
}
}