Skip to content

Commit 7563525

Browse files
author
Christoph Büscher
authored
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.
1 parent 190ac8e commit 7563525

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ org.apache.lucene.util.LuceneTestCase$Nightly @ We don't run nightly tests at th
2525
com.carrotsearch.randomizedtesting.annotations.Nightly @ We don't run nightly tests at this point!
2626

2727
org.junit.Test @defaultMessage Just name your test method testFooBar
28+
29+
java.lang.Math#random() @ Use one of the various randomization methods from LuceneTestCase or ESTestCase for reproducibility

server/src/test/java/org/elasticsearch/common/util/ArrayUtilsTests.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
public class ArrayUtilsTests extends ESTestCase {
3131
public void testBinarySearch() throws Exception {
3232
for (int j = 0; j < 100; j++) {
33-
int index = Math.min(randomInt(0, 10), 9);
34-
double tolerance = Math.random() * 0.01;
35-
double lookForValue = randomFreq(0.9) ? -1 : Double.NaN; // sometimes we'll look for NaN
33+
int index = randomIntBetween(0, 9);
34+
double tolerance = randomDoubleBetween(0, 0.01, true);
35+
double lookForValue = frequently() ? -1 : Double.NaN; // sometimes we'll look for NaN
3636
double[] array = new double[10];
3737
for (int i = 0; i < array.length; i++) {
3838
double value;
39-
if (randomFreq(0.9)) {
40-
value = Math.random() * 10;
41-
array[i] = value + ((randomFreq(0.5) ? 1 : -1) * Math.random() * tolerance);
39+
if (frequently()) {
40+
value = randomDoubleBetween(0, 9, true);
41+
array[i] = value + ((randomBoolean() ? 1 : -1) * randomDouble() * tolerance);
4242

4343
} else { // sometimes we'll have NaN in the array
4444
value = Double.NaN;
@@ -73,15 +73,6 @@ public void testBinarySearch() throws Exception {
7373
}
7474
}
7575

76-
private boolean randomFreq(double freq) {
77-
return Math.random() < freq;
78-
}
79-
80-
private int randomInt(int min, int max) {
81-
int delta = (int) (Math.random() * (max - min));
82-
return min + delta;
83-
}
84-
8576
public void testConcat() {
8677
assertArrayEquals(new String[]{"a", "b", "c", "d"}, ArrayUtils.concat(new String[]{"a", "b"}, new String[]{"c", "d"}));
8778
int firstSize = randomIntBetween(0, 10);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Tokens add(String name) {
8282
}
8383

8484
Tokens add(String name, String key) {
85-
if (Math.random() > 0.5) {
85+
if (randomBoolean()) {
8686
tokens.add(new AggregationPath.PathElement(name + "." + key, name, key));
8787
} else {
8888
tokens.add(new AggregationPath.PathElement(name + "[" + key + "]", name, key));

0 commit comments

Comments
 (0)