Skip to content

Move test seed setup to global build info #47763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2019
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 @@ -131,7 +131,6 @@ class BuildPlugin implements Plugin<Project> {

project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)

setupSeed(project)
configureRepositories(project)
project.extensions.getByType(ExtraPropertiesExtension).set('versions', VersionProperties.versions)
configureInputNormalization(project)
Expand Down Expand Up @@ -950,32 +949,6 @@ class BuildPlugin implements Plugin<Project> {
}
}

/**
* Pins the test seed at configuration time so it isn't different on every
* {@link Test} execution. This is useful if random
* decisions in one run of {@linkplain Test} influence the
* outcome of subsequent runs. Pinning the seed up front like this makes
* the reproduction line from one run be useful on another run.
*/
static String setupSeed(Project project) {
ExtraPropertiesExtension ext = project.rootProject.extensions.getByType(ExtraPropertiesExtension)
if (ext.has('testSeed')) {
/* Skip this if we've already pinned the testSeed. It is important
* that this checks the rootProject so that we know we've only ever
* initialized one time. */
return ext.get('testSeed')
}

String testSeed = System.getProperty('tests.seed')
if (testSeed == null) {
long seed = new Random(System.currentTimeMillis()).nextLong()
testSeed = Long.toUnsignedString(seed, 16).toUpperCase(Locale.ROOT)
}

ext.set('testSeed', testSeed)
return testSeed
}

private static class TestFailureReportingPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

public class GlobalBuildInfoPlugin implements Plugin<Project> {
Expand All @@ -46,6 +48,15 @@ public void apply(Project project) {
File compilerJavaHome = findCompilerJavaHome();
File runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome);

String testSeedProperty = System.getProperty("tests.seed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be simplified to something like this which seems easier to grok:

String testSeed = System.getProperty("tests.seed", generateSeed());

private static String generateSeed() {
    double seed = new Random().nextLong();
    return Long.toUnsignedString(seed, 16).toUpperCase(Locale.ROOT);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean always generating seed, even when one is passed in as a property. If you are adamant about that, I'll let you make that change with the other refactorings to global build info we discussed.

final String testSeed;
if (testSeedProperty == null) {
long seed = new Random(System.currentTimeMillis()).nextLong();
testSeed = Long.toUnsignedString(seed, 16).toUpperCase(Locale.ROOT);
} else {
testSeed = testSeedProperty;
}

final List<JavaHome> javaVersions = new ArrayList<>();
for (int version = 8; version <= Integer.parseInt(minimumCompilerVersion.getMajorVersion()); version++) {
if (System.getenv(getJavaHomeEnvVarName(Integer.toString(version))) != null) {
Expand Down Expand Up @@ -95,6 +106,7 @@ public void apply(Project project) {
ext.set("gradleJavaVersion", Jvm.current().getJavaVersion());
ext.set("gitRevision", gitRevision(project.getRootProject().getRootDir()));
ext.set("buildDate", ZonedDateTime.now(ZoneOffset.UTC));
ext.set("testSeed", testSeed);
ext.set("isCi", System.getenv("JENKINS_URL") != null);
});
}
Expand Down Expand Up @@ -265,5 +277,4 @@ private static String readFirstLine(final Path path) throws IOException {
.findFirst()
.orElseThrow(() -> new IOException("file [" + path + "] is empty"));
}

}