From 265b386fa75f9446378e65002d9fc16766e67892 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Wed, 14 May 2014 09:34:45 +0200 Subject: [PATCH] [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. --- .../index/search/child/ChildrenConstantScoreQueryTests.java | 3 ++- .../elasticsearch/index/search/child/ChildrenQueryTests.java | 4 +++- .../index/search/child/ParentConstantScoreQueryTests.java | 2 +- .../elasticsearch/index/search/child/ParentQueryTests.java | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java index a16d1314430..b8eea76a3bc 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ChildrenConstantScoreQueryTests.java @@ -243,7 +243,8 @@ public class ChildrenConstantScoreQueryTests extends ElasticsearchLuceneTestCase // Simulate a parent update 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++) { int parentId; do { diff --git a/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java index e5386823d26..adee4173841 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ChildrenQueryTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.search.child; import com.carrotsearch.hppc.FloatArrayList; import com.carrotsearch.hppc.IntOpenHashSet; import com.carrotsearch.hppc.ObjectObjectOpenHashMap; +import com.carrotsearch.randomizedtesting.generators.RandomInts; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; @@ -187,7 +188,8 @@ public class ChildrenQueryTests extends ElasticsearchLuceneTestCase { // Simulate a parent update 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++) { int parentId; do { diff --git a/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java index 127a4ba6196..e43bcaa470b 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ParentConstantScoreQueryTests.java @@ -184,7 +184,7 @@ public class ParentConstantScoreQueryTests extends ElasticsearchLuceneTestCase { // Simulate a child update if (random().nextBoolean()) { - int numberOfUpdates = scaledRandomIntBetween(1, 25); + int numberOfUpdates = childIdToParentId.isEmpty() ? 0 : scaledRandomIntBetween(1, 25); int[] childIds = childIdToParentId.keys().toArray(); for (int j = 0; j < numberOfUpdates; j++) { int childId = childIds[random().nextInt(childIds.length)]; diff --git a/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java b/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java index f86e4815809..0a9f370fb94 100644 --- a/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java +++ b/src/test/java/org/elasticsearch/index/search/child/ParentQueryTests.java @@ -184,7 +184,7 @@ public class ParentQueryTests extends ElasticsearchLuceneTestCase { // Simulate a child update if (random().nextBoolean()) { - int numberOfUpdates = scaledRandomIntBetween(1, 5); + int numberOfUpdates = childIdToParentId.isEmpty() ? 0 : scaledRandomIntBetween(1, 5); int[] childIds = childIdToParentId.keys().toArray(); for (int j = 0; j < numberOfUpdates; j++) { int childId = childIds[random().nextInt(childIds.length)];