Skip to content

Instantly share code, notes, and snippets.

@ejain
Created March 7, 2012 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejain/1995058 to your computer and use it in GitHub Desktop.
Save ejain/1995058 to your computer and use it in GitHub Desktop.
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ReopenTest {
private Node node;
private Client client;
@Before
public void setUp() {
node = NodeBuilder.nodeBuilder().clusterName("test").node();
client = node.client();
}
@Test
public void test() {
String index = "test";
int expectedCount = 1000;
create(index);
for (int i = 0; i < expectedCount; ++i) {
put(index, "type", i, "bla");
}
tearDown();
setUp();
recover(ClusterHealthStatus.GREEN);
Assert.assertEquals("Documents in index", expectedCount, count(index));
}
public void create(String index) {
Settings settings = ImmutableSettings.settingsBuilder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
Assert.assertTrue("Index created", client.admin().indices().prepareCreate(index)
.setSettings(settings).execute().actionGet().acknowledged());
}
private IndexResponse put(String index, String type, int id, String value) {
return client.prepareIndex(index, type, Integer.toString(id)).setSource("value", value).execute().actionGet();
}
private void recover(ClusterHealthStatus expectedStatus) {
ClusterHealthStatus status = client.admin().cluster().prepareHealth().setWaitForStatus(expectedStatus)
.setTimeout(new TimeValue(10, TimeUnit.SECONDS)).execute().actionGet().getStatus();
Assert.assertTrue("Cluster status is at least " + expectedStatus, status.compareTo(expectedStatus) <= 0);
}
private long count(String index) {
return client.prepareCount(index).execute().actionGet().count();
}
@After
public void tearDown() {
client.close();
node.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment