Skip to content
Permalink
Browse files
8253750: use build-stable default seed for Utils.RANDOM_GENERATOR
Reviewed-by: rriggs
  • Loading branch information
iignatev committed Oct 6, 2020
1 parent 6712f8c commit ac772cd91632f16efad4ce92b7518c282ce0cfad
Showing with 53 additions and 14 deletions.
  1. +15 −6 test/lib-test/jdk/test/lib/RandomGeneratorTest.java
  2. +38 −8 test/lib/jdk/test/lib/Utils.java
@@ -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 + "=";
@@ -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);
}
};

/**
@@ -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
@@ -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;
@@ -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;
@@ -127,9 +129,8 @@
*/
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;

@@ -141,7 +142,32 @@
/**
* 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}.
@@ -531,9 +557,13 @@ public static String toHexString(byte[] bytes) {

/**
* 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() {

1 comment on commit ac772cd

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on ac772cd Oct 6, 2020

Please sign in to comment.