diff --git a/buildSrc/src/main/resources/forbidden/es-test-signatures.txt b/buildSrc/src/main/resources/forbidden/es-test-signatures.txt index 08e591e1cfa40..766e13878cc25 100644 --- a/buildSrc/src/main/resources/forbidden/es-test-signatures.txt +++ b/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 diff --git a/server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java b/server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java index 8a346ed4fbebc..8a81f9f818ff5 100644 --- a/server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java +++ b/server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java @@ -30,15 +30,15 @@ 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 void testBinarySearch() throws Exception { } } - 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); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/support/PathTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/support/PathTests.java index c5c681c117f1c..c18a3523e7a21 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/support/PathTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/support/PathTests.java @@ -82,7 +82,7 @@ Tokens add(String name) { } 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));