Use awaiBusy rather than a tight loop in IndicesStoreTests

This commit is contained in:
Simon Willnauer 2013-08-23 13:12:23 +02:00
parent a943135ef6
commit 19cce0b329
1 changed files with 10 additions and 17 deletions

View File

@ -19,10 +19,10 @@
package org.elasticsearch.test.integration.indices.store; package org.elasticsearch.test.integration.indices.store;
import com.google.common.base.Predicate;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.node.internal.InternalNode; import org.elasticsearch.node.internal.InternalNode;
@ -61,12 +61,6 @@ public class IndicesStoreTests extends AbstractNodesTests {
@Test @Test
public void shardsCleanup() throws Exception { public void shardsCleanup() throws Exception {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception ex) {
// Ignore
}
logger.info("--> creating index [test] with one shard and on replica"); logger.info("--> creating index [test] with one shard and on replica");
client().admin().indices().create(createIndexRequest("test") client().admin().indices().create(createIndexRequest("test")
.settings(settingsBuilder().put("index.numberOfReplicas", 1).put("index.numberOfShards", 1))).actionGet(); .settings(settingsBuilder().put("index.numberOfReplicas", 1).put("index.numberOfShards", 1))).actionGet();
@ -85,7 +79,7 @@ public class IndicesStoreTests extends AbstractNodesTests {
startNode("server3"); startNode("server3");
logger.info("--> making sure that shard is not allocated on server3"); logger.info("--> making sure that shard is not allocated on server3");
assertThat(waitForShardDeletion(TimeValue.timeValueSeconds(1), "server3", "test", 0), equalTo(false)); assertThat(waitForShardDeletion("server3", "test", 0), equalTo(false));
File server2Shard = shardDirectory("server2", "test", 0); File server2Shard = shardDirectory("server2", "test", 0);
logger.info("--> stopping node server2"); logger.info("--> stopping node server2");
@ -113,7 +107,7 @@ public class IndicesStoreTests extends AbstractNodesTests {
logger.info("--> making sure that shard and it's replica are allocated on server1 and server3 but not on server2"); logger.info("--> making sure that shard and it's replica are allocated on server1 and server3 but not on server2");
assertThat(shardDirectory("server1", "test", 0).exists(), equalTo(true)); assertThat(shardDirectory("server1", "test", 0).exists(), equalTo(true));
assertThat(shardDirectory("server3", "test", 0).exists(), equalTo(true)); assertThat(shardDirectory("server3", "test", 0).exists(), equalTo(true));
assertThat(waitForShardDeletion(TimeValue.timeValueSeconds(1), "server2", "test", 0), equalTo(false)); assertThat(waitForShardDeletion("server2", "test", 0), equalTo(false));
} }
private File shardDirectory(String server, String index, int shard) { private File shardDirectory(String server, String index, int shard) {
@ -122,14 +116,13 @@ public class IndicesStoreTests extends AbstractNodesTests {
return env.shardLocations(new ShardId(index, shard))[0]; return env.shardLocations(new ShardId(index, shard))[0];
} }
private boolean waitForShardDeletion(TimeValue timeout, String server, String index, int shard) throws InterruptedException { private boolean waitForShardDeletion(final String server, final String index, final int shard) throws InterruptedException {
long start = System.currentTimeMillis(); awaitBusy(new Predicate<Object>() {
boolean shardExists; public boolean apply(Object o) {
do { return !shardDirectory(server, index, shard).exists();
shardExists = shardDirectory(server, index, shard).exists(); }
} });
while (shardExists && (System.currentTimeMillis() - start) < timeout.millis()); return shardDirectory(server, index, shard).exists();
return shardExists;
} }