[TEST] Fix testRandomDirectoryIOExceptions to wait for green on reopen & add more assertions

This commit is contained in:
Simon Willnauer 2014-04-08 17:46:29 +02:00
parent a98b3fa1ff
commit 5b6fd6de42
1 changed files with 18 additions and 7 deletions

View File

@ -50,6 +50,7 @@ import java.util.concurrent.ExecutionException;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTest { public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTest {
@ -101,7 +102,7 @@ public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTes
numInitialDocs = between(10, 100); numInitialDocs = between(10, 100);
ensureYellow(); ensureYellow();
for (int i = 0; i < numInitialDocs ; i++) { for (int i = 0; i < numInitialDocs ; i++) {
client().prepareIndex("test", "type", "" + i).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", "init").get(); client().prepareIndex("test", "initial", "" + i).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", "init").get();
} }
client().admin().indices().prepareRefresh("test").execute().get(); client().admin().indices().prepareRefresh("test").execute().get();
client().admin().indices().prepareFlush("test").execute().get(); client().admin().indices().prepareFlush("test").execute().get();
@ -141,14 +142,16 @@ public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTes
long numCreated = 0; long numCreated = 0;
boolean[] added = new boolean[numDocs]; boolean[] added = new boolean[numDocs];
for (int i = 0; i < numDocs ; i++) { for (int i = 0; i < numDocs ; i++) {
added[i] = false;
try { try {
IndexResponse indexResponse = client().prepareIndex("test", "type", "" + (i + numInitialDocs)).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", English.intToEnglish(i)).get(); IndexResponse indexResponse = client().prepareIndex("test", "type", Integer.toString(i)).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", English.intToEnglish(i)).get();
if (indexResponse.isCreated()) { if (indexResponse.isCreated()) {
numCreated++; numCreated++;
added[i] = true; added[i] = true;
} }
} catch (ElasticsearchException ex) { } catch (ElasticsearchException ex) {
} }
} }
NumShards test = getNumShards("test"); NumShards test = getNumShards("test");
logger.info("Start Refresh"); logger.info("Start Refresh");
@ -160,11 +163,19 @@ public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTes
for (int i = 0; i < numSearches; i++) { for (int i = 0; i < numSearches; i++) {
try { try {
int docToQuery = between(0, numDocs-1); int docToQuery = between(0, numDocs-1);
long expectedResults = added[docToQuery] ? 1 : 0;
logger.info("Searching for [test:{}]", English.intToEnglish(docToQuery)); logger.info("Searching for [test:{}]", English.intToEnglish(docToQuery));
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("test", English.intToEnglish(docToQuery))).get(); SearchResponse searchResponse = client().prepareSearch().setTypes("type").setQuery(QueryBuilders.matchQuery("test", English.intToEnglish(docToQuery))).get();
logger.info("Successful shards: [{}] numShards: [{}]", searchResponse.getSuccessfulShards(), test.numPrimaries); logger.info("Successful shards: [{}] numShards: [{}]", searchResponse.getSuccessfulShards(), test.numPrimaries);
if (searchResponse.getSuccessfulShards() == test.numPrimaries && !refreshFailed) {
assertThat(searchResponse.getHits().getTotalHits(), Matchers.equalTo(expectedResults));
}
// check match all // check match all
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).get(); searchResponse = client().prepareSearch().setTypes("type").setQuery(QueryBuilders.matchAllQuery()).get();
logger.info("Match all Successful shards: [{}] numShards: [{}]", searchResponse.getSuccessfulShards(), test.numPrimaries);
if (searchResponse.getSuccessfulShards() == test.numPrimaries && !refreshFailed) {
assertThat(searchResponse.getHits().getTotalHits(), Matchers.equalTo(numCreated));
}
} catch (SearchPhaseExecutionException ex) { } catch (SearchPhaseExecutionException ex) {
if (expectAllShardsFailed || refreshResponse.getSuccessfulShards() == 0) { if (expectAllShardsFailed || refreshResponse.getSuccessfulShards() == 0) {
logger.info("expected SearchPhaseException: [{}]", ex.getMessage()); logger.info("expected SearchPhaseException: [{}]", ex.getMessage());
@ -181,9 +192,9 @@ public class SearchWithRandomExceptionsTests extends ElasticsearchIntegrationTes
.put(MockDirectoryHelper.RANDOM_IO_EXCEPTION_RATE, 0) .put(MockDirectoryHelper.RANDOM_IO_EXCEPTION_RATE, 0)
.put(MockDirectoryHelper.RANDOM_IO_EXCEPTION_RATE_ON_OPEN, 0)); .put(MockDirectoryHelper.RANDOM_IO_EXCEPTION_RATE_ON_OPEN, 0));
client().admin().indices().prepareOpen("test").execute().get(); client().admin().indices().prepareOpen("test").execute().get();
ensureYellow(); ensureGreen();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("test", "init")).get(); SearchResponse searchResponse = client().prepareSearch().setTypes("initial").setQuery(QueryBuilders.matchQuery("test", "init")).get();
assertThat(searchResponse.getHits().totalHits(), Matchers.equalTo(numInitialDocs)); assertHitCount(searchResponse, numInitialDocs);
} }
} }