Test: Change randomValueOtherThan(null, supplier) (#27901)

When the first parameter of `ESTestCase#randomValueOtherThan` is `null`
then run the supplier until it returns non-`null`. Previously,
`randomValueOtherThan` just ran the supplier one time which was
confusing.

Unexpectedly, it looks like not tests rely on the original `null`
handling.

Closes #27821
This commit is contained in:
Nik Everett 2017-12-19 10:23:38 -05:00 committed by GitHub
parent b1039164a1
commit 32669ca265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -684,11 +684,7 @@ public abstract class ESTestCase extends LuceneTestCase {
* helper to get a random value in a certain range that's different from the input
*/
public static <T> T randomValueOtherThan(T input, Supplier<T> randomSupplier) {
if (input != null) {
return randomValueOtherThanMany(input::equals, randomSupplier);
}
return(randomSupplier.get());
return randomValueOtherThanMany(v -> Objects.equals(input, v), randomSupplier);
}
/**

View File

@ -38,6 +38,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
@ -166,4 +167,18 @@ public class ESTestCaseTests extends ESTestCase {
public void testRandomUniqueNormalUsageAlwayMoreThanOne() {
assertThat(randomUnique(() -> randomAlphaOfLengthBetween(1, 20), 10), hasSize(greaterThan(0)));
}
public void testRandomValueOtherThan() {
// "normal" way of calling where the value is not null
int bad = randomInt();
assertNotEquals(bad, (int) randomValueOtherThan(bad, ESTestCase::randomInt));
/*
* "funny" way of calling where the value is null. This once
* had a unique behavior but at this point `null` acts just
* like any other value.
*/
Supplier<Object> usuallyNull = () -> usually() ? null : randomInt();
assertNotNull(randomValueOtherThan(null, usuallyNull));
}
}