[TESTS] Fix test bugs for parent/child queries.

If you got a bad seed and tests.nightly=true, these tests would either call
Random#nextInt on `0` or trigger infinite loops.
This commit is contained in:
Adrien Grand 2014-05-14 09:34:45 +02:00
parent 9daa72941a
commit 265b386fa7
4 changed files with 7 additions and 4 deletions

View File

@ -243,7 +243,8 @@ public class ChildrenConstantScoreQueryTests extends ElasticsearchLuceneTestCase
// Simulate a parent update // Simulate a parent update
if (random().nextBoolean()) { if (random().nextBoolean()) {
int numberOfUpdates = scaledRandomIntBetween(1, 25); final int numberOfUpdatableParents = numParentDocs - filteredOrDeletedDocs.size();
int numberOfUpdates = scaledRandomIntBetween(0, numberOfUpdatableParents);
for (int j = 0; j < numberOfUpdates; j++) { for (int j = 0; j < numberOfUpdates; j++) {
int parentId; int parentId;
do { do {

View File

@ -21,6 +21,7 @@ package org.elasticsearch.index.search.child;
import com.carrotsearch.hppc.FloatArrayList; import com.carrotsearch.hppc.FloatArrayList;
import com.carrotsearch.hppc.IntOpenHashSet; import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.ObjectObjectOpenHashMap; import com.carrotsearch.hppc.ObjectObjectOpenHashMap;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
@ -187,7 +188,8 @@ public class ChildrenQueryTests extends ElasticsearchLuceneTestCase {
// Simulate a parent update // Simulate a parent update
if (random().nextBoolean()) { if (random().nextBoolean()) {
int numberOfUpdates = 1 + random().nextInt(TEST_NIGHTLY ? 25 : 5); final int numberOfUpdatableParents = numParentDocs - filteredOrDeletedDocs.size();
int numberOfUpdates = RandomInts.randomIntBetween(random(), 0, Math.min(numberOfUpdatableParents, TEST_NIGHTLY ? 25 : 5));
for (int j = 0; j < numberOfUpdates; j++) { for (int j = 0; j < numberOfUpdates; j++) {
int parentId; int parentId;
do { do {

View File

@ -184,7 +184,7 @@ public class ParentConstantScoreQueryTests extends ElasticsearchLuceneTestCase {
// Simulate a child update // Simulate a child update
if (random().nextBoolean()) { if (random().nextBoolean()) {
int numberOfUpdates = scaledRandomIntBetween(1, 25); int numberOfUpdates = childIdToParentId.isEmpty() ? 0 : scaledRandomIntBetween(1, 25);
int[] childIds = childIdToParentId.keys().toArray(); int[] childIds = childIdToParentId.keys().toArray();
for (int j = 0; j < numberOfUpdates; j++) { for (int j = 0; j < numberOfUpdates; j++) {
int childId = childIds[random().nextInt(childIds.length)]; int childId = childIds[random().nextInt(childIds.length)];

View File

@ -184,7 +184,7 @@ public class ParentQueryTests extends ElasticsearchLuceneTestCase {
// Simulate a child update // Simulate a child update
if (random().nextBoolean()) { if (random().nextBoolean()) {
int numberOfUpdates = scaledRandomIntBetween(1, 5); int numberOfUpdates = childIdToParentId.isEmpty() ? 0 : scaledRandomIntBetween(1, 5);
int[] childIds = childIdToParentId.keys().toArray(); int[] childIds = childIdToParentId.keys().toArray();
for (int j = 0; j < numberOfUpdates; j++) { for (int j = 0; j < numberOfUpdates; j++) {
int childId = childIds[random().nextInt(childIds.length)]; int childId = childIds[random().nextInt(childIds.length)];