Browse Source

Make sure test don't use Math.random for reproducability (#36241)

Currently we use Math.random() in a few places in the tests which makes these
tests not reproducable with the random seed mechanism that comes with
ESTestCase. The change removes those instances.
Christoph Büscher 6 years ago
parent
commit
75635251ab

+ 2 - 0
buildSrc/src/main/resources/forbidden/es-test-signatures.txt

@@ -25,3 +25,5 @@ org.apache.lucene.util.LuceneTestCase$Nightly @ We don't run nightly tests at th
 com.carrotsearch.randomizedtesting.annotations.Nightly @ We don't run nightly tests at this point!
 
 org.junit.Test @defaultMessage Just name your test method testFooBar
+
+java.lang.Math#random() @ Use one of the various randomization methods from LuceneTestCase or ESTestCase for reproducibility

+ 6 - 15
server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java

@@ -30,15 +30,15 @@ import static org.hamcrest.Matchers.is;
 public class ArrayUtilsTests extends ESTestCase {
     public void testBinarySearch() throws Exception {
         for (int j = 0; j < 100; j++) {
-            int index = Math.min(randomInt(0, 10), 9);
-            double tolerance = Math.random() * 0.01;
-            double lookForValue = randomFreq(0.9) ? -1 : Double.NaN; // sometimes we'll look for NaN
+            int index = randomIntBetween(0, 9);
+            double tolerance = randomDoubleBetween(0, 0.01, true);
+            double lookForValue = frequently() ? -1 : Double.NaN; // sometimes we'll look for NaN
             double[] array = new double[10];
             for (int i = 0; i < array.length; i++) {
                 double value;
-                if (randomFreq(0.9)) {
-                    value = Math.random() * 10;
-                    array[i] = value + ((randomFreq(0.5) ? 1 : -1) * Math.random() * tolerance);
+                if (frequently()) {
+                    value = randomDoubleBetween(0, 9, true);
+                    array[i] = value + ((randomBoolean() ? 1 : -1) * randomDouble() * tolerance);
 
                 } else {                    // sometimes we'll have NaN in the array
                     value = Double.NaN;
@@ -73,15 +73,6 @@ public class ArrayUtilsTests extends ESTestCase {
         }
     }
 
-    private boolean randomFreq(double freq) {
-        return Math.random() < freq;
-    }
-
-    private int randomInt(int min, int max) {
-        int delta = (int) (Math.random() * (max - min));
-        return min + delta;
-    }
-
     public void testConcat() {
         assertArrayEquals(new String[]{"a", "b", "c", "d"}, ArrayUtils.concat(new String[]{"a", "b"}, new String[]{"c", "d"}));
         int firstSize = randomIntBetween(0, 10);

+ 1 - 1
server/src/test/java/org/elasticsearch/search/aggregations/support/PathTests.java

@@ -82,7 +82,7 @@ public class PathTests extends ESTestCase {
         }
 
         Tokens add(String name, String key) {
-            if (Math.random() > 0.5) {
+            if (randomBoolean()) {
                 tokens.add(new AggregationPath.PathElement(name + "." + key, name, key));
             } else {
                 tokens.add(new AggregationPath.PathElement(name + "[" + key + "]", name, key));