Skip to content
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

8253750: use build-stable default seed for Utils.RANDOM_GENERATOR #391

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 15 additions & 6 deletions test/lib-test/jdk/test/lib/RandomGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
/**
* The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
* Test works in three modes: same seed provided, no seed provided and
* different seed provided. In the first case the test expects that all random numbers
* will be repeated in all next iterations. For other two modes test expects that
* randomly generated numbers differ from original.
* different seed provided.
* In the first case, the test expects that all random numbers will be repeated in all next iterations.
* In the second case, the numbers are expected to be the same for promotable builds and different for other builds.
* In the last case, the test expects the randomly generated numbers differ from original.
*/
public class RandomGeneratorTest {
private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
Expand Down Expand Up @@ -102,12 +103,22 @@ public void verify(String orig, String[] cmdLine) {
cmdLine[0] = getSeedOption();
super.verify(orig, cmdLine);
}

@Override
protected boolean isOutputExpected(String orig, String output) {
return !output.equals(orig);
}
},
NO_SEED {
@Override
public String getSeedOption() {
return null;
}

@Override
protected boolean isOutputExpected(String orig, String output) {
return Runtime.version().build().orElse(0) > 0 ^ !output.equals(orig);
}
};

/**
Expand All @@ -118,9 +129,7 @@ public String getSeedOption() {
*/
public abstract String getSeedOption();

protected boolean isOutputExpected(String orig, String output) {
return !output.equals(orig);
}
protected abstract boolean isOutputExpected(String orig, String output);

/**
* Verifies that the original output meets expectations
Expand Down
46 changes: 38 additions & 8 deletions test/lib/jdk/test/lib/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand All @@ -35,11 +33,15 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.channels.SocketChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -127,9 +129,8 @@ public final class Utils {
*/
public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";

/* (non-javadoc)
* Random generator with (or without) predefined seed. Depends on
* "jdk.test.lib.random.seed" property value.
/**
* Random generator with predefined seed.
*/
private static volatile Random RANDOM_GENERATOR;

Expand All @@ -141,7 +142,32 @@ public final class Utils {
/**
* Contains the seed value used for {@link java.util.Random} creation.
*/
public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
public static final long SEED;
static {
var seed = Long.getLong(SEED_PROPERTY_NAME);
if (seed != null) {
// use explicitly set seed
SEED = seed;
} else {
var v = Runtime.version();
// promotable builds have build number, and it's greater than 0
if (v.build().orElse(0) > 0) {
// promotable build -> use 1st 8 bytes of md5($version)
try {
var md = MessageDigest.getInstance("MD5");
var bytes = v.toString()
.getBytes(StandardCharsets.UTF_8);
bytes = md.digest(bytes);
SEED = ByteBuffer.wrap(bytes).getLong();
} catch (NoSuchAlgorithmException e) {
throw new Error(e);
}
} else {
// "personal" build -> use random seed
SEED = new Random().nextLong();
}
}
}
/**
* Returns the value of 'test.timeout.factor' system property
* converted to {@code double}.
Expand Down Expand Up @@ -531,9 +557,13 @@ public static byte[] toByteArray(String hex) {

/**
* Returns {@link java.util.Random} generator initialized with particular seed.
* The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
* In case no seed is provided, the method uses a random number.
* The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}.
* In case no seed is provided and the build under test is "promotable"
* (its build number ({@code $BUILD} in {@link Runtime.Version}) is greater than 0,
* the seed based on string representation of {@link Runtime#version()} is used.
* Otherwise, the seed is randomly generated.
* The used seed printed to stdout.
*
* @return {@link java.util.Random} generator with particular seed.
*/
public static Random getRandomInstance() {
Expand Down