Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static TransportVersion getFirstVersion() {

/** Returns a random {@link TransportVersion} from all available versions. */
public static TransportVersion randomVersion() {
return VersionUtils.randomFrom(random(), allReleasedVersions(), TransportVersion::fromId);
return VersionUtils.randomFrom(random(), allReleasedVersions());
}

/** Returns a random {@link TransportVersion} from all available versions without the ignore set */
Expand All @@ -50,7 +50,7 @@ public static TransportVersion randomVersion(Set<TransportVersion> ignore) {

/** Returns a random {@link TransportVersion} from all available versions. */
public static TransportVersion randomVersion(Random random) {
return VersionUtils.randomFrom(random, allReleasedVersions(), TransportVersion::fromId);
return VersionUtils.randomFrom(random, allReleasedVersions());
}

/** Returns a random {@link TransportVersion} between <code>minVersion</code> and <code>maxVersion</code> (inclusive). */
Expand All @@ -77,7 +77,7 @@ public static TransportVersion randomVersionBetween(
versions = versions.headSet(maxVersion, true);
}

return VersionUtils.randomFrom(random, versions, TransportVersion::fromId);
return VersionUtils.randomFrom(random, versions);
}

public static TransportVersion getPreviousVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

package org.elasticsearch.test;

import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;

import org.elasticsearch.Build;
import org.elasticsearch.Version;
Expand All @@ -21,7 +21,6 @@
import java.util.NavigableSet;
import java.util.Random;
import java.util.TreeSet;
import java.util.function.IntFunction;

/** Utilities for selecting versions in tests */
public class VersionUtils {
Expand Down Expand Up @@ -77,7 +76,7 @@ public static Version getFirstVersion() {

/** Returns a random {@link Version} from all available versions. */
public static Version randomVersion(Random random) {
return randomFrom(random, ALL_VERSIONS, Version::fromId);
return randomFrom(random, ALL_VERSIONS);
}

/** Returns a random {@link Version} from all available versions, that is compatible with the given version. */
Expand Down Expand Up @@ -106,24 +105,15 @@ public static Version randomVersionBetween(Random random, @Nullable Version minV
versions = versions.headSet(maxVersion, true);
}

return randomFrom(random, versions, Version::fromId);
return randomFrom(random, versions);
}

/** Returns the maximum {@link Version} that is compatible with the given version. */
public static Version maxCompatibleVersion(Version version) {
return ALL_VERSIONS.tailSet(version, true).descendingSet().stream().filter(version::isCompatible).findFirst().orElseThrow();
}

public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set, IntFunction<T> ctor) {
// get the first and last id, pick a random id in the middle, then find that id in the set in O(nlogn) time
// this assumes the id numbers are reasonably evenly distributed in the set
assert set.isEmpty() == false;
int lowest = set.getFirst().id();
int highest = set.getLast().id();

T randomId = ctor.apply(RandomNumbers.randomIntBetween(random, lowest, highest));
// try to find the id below, then the id above. We're just looking for *some* item in the set that is close to randomId
T found = set.floor(randomId);
return found != null ? found : set.ceiling(randomId);
public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set) {
return RandomPicks.randomFrom(random, set);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public static IndexVersion getLowestWriteCompatibleVersion() {

/** Returns a random {@link IndexVersion} from all available versions. */
public static IndexVersion randomVersion() {
return VersionUtils.randomFrom(random(), ALL_VERSIONS, IndexVersion::fromId);
return VersionUtils.randomFrom(random(), ALL_VERSIONS);
}

/** Returns a random {@link IndexVersion} from all versions that can be written to. */
public static IndexVersion randomWriteVersion() {
return VersionUtils.randomFrom(random(), ALL_WRITE_VERSIONS, IndexVersion::fromId);
return VersionUtils.randomFrom(random(), ALL_WRITE_VERSIONS);
}

/** Returns a random {@link IndexVersion} from all available versions without the ignore set */
Expand Down Expand Up @@ -78,7 +78,7 @@ public static IndexVersion randomVersionBetween(Random random, @Nullable IndexVe
versions = versions.headSet(maxVersion, true);
}

return VersionUtils.randomFrom(random, versions, IndexVersion::fromId);
return VersionUtils.randomFrom(random, versions);
}

public static IndexVersion getPreviousVersion() {
Expand Down