Skip to content

Commit ac772cd

Browse files
committedOct 6, 2020
8253750: use build-stable default seed for Utils.RANDOM_GENERATOR
Reviewed-by: rriggs
1 parent 6712f8c commit ac772cd

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed
 

‎test/lib-test/jdk/test/lib/RandomGeneratorTest.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@
4646
/**
4747
* The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
4848
* Test works in three modes: same seed provided, no seed provided and
49-
* different seed provided. In the first case the test expects that all random numbers
50-
* will be repeated in all next iterations. For other two modes test expects that
51-
* randomly generated numbers differ from original.
49+
* different seed provided.
50+
* In the first case, the test expects that all random numbers will be repeated in all next iterations.
51+
* In the second case, the numbers are expected to be the same for promotable builds and different for other builds.
52+
* In the last case, the test expects the randomly generated numbers differ from original.
5253
*/
5354
public class RandomGeneratorTest {
5455
private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
@@ -102,12 +103,22 @@ public void verify(String orig, String[] cmdLine) {
102103
cmdLine[0] = getSeedOption();
103104
super.verify(orig, cmdLine);
104105
}
106+
107+
@Override
108+
protected boolean isOutputExpected(String orig, String output) {
109+
return !output.equals(orig);
110+
}
105111
},
106112
NO_SEED {
107113
@Override
108114
public String getSeedOption() {
109115
return null;
110116
}
117+
118+
@Override
119+
protected boolean isOutputExpected(String orig, String output) {
120+
return Runtime.version().build().orElse(0) > 0 ^ !output.equals(orig);
121+
}
111122
};
112123

113124
/**
@@ -118,9 +129,7 @@ public String getSeedOption() {
118129
*/
119130
public abstract String getSeedOption();
120131

121-
protected boolean isOutputExpected(String orig, String output) {
122-
return !output.equals(orig);
123-
}
132+
protected abstract boolean isOutputExpected(String orig, String output);
124133

125134
/**
126135
* Verifies that the original output meets expectations

‎test/lib/jdk/test/lib/Utils.java

+38-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28-
import java.lang.annotation.Annotation;
29-
import java.lang.reflect.Method;
3028
import java.net.Inet6Address;
3129
import java.net.InetAddress;
3230
import java.net.InetSocketAddress;
@@ -35,11 +33,15 @@
3533
import java.net.URL;
3634
import java.net.URLClassLoader;
3735
import java.net.UnknownHostException;
36+
import java.nio.ByteBuffer;
37+
import java.nio.charset.StandardCharsets;
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
4040
import java.nio.file.Paths;
4141
import java.nio.file.attribute.FileAttribute;
4242
import java.nio.channels.SocketChannel;
43+
import java.security.MessageDigest;
44+
import java.security.NoSuchAlgorithmException;
4345
import java.util.ArrayList;
4446
import java.util.Arrays;
4547
import java.util.Collection;
@@ -127,9 +129,8 @@ public final class Utils {
127129
*/
128130
public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";
129131

130-
/* (non-javadoc)
131-
* Random generator with (or without) predefined seed. Depends on
132-
* "jdk.test.lib.random.seed" property value.
132+
/**
133+
* Random generator with predefined seed.
133134
*/
134135
private static volatile Random RANDOM_GENERATOR;
135136

@@ -141,7 +142,32 @@ public final class Utils {
141142
/**
142143
* Contains the seed value used for {@link java.util.Random} creation.
143144
*/
144-
public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
145+
public static final long SEED;
146+
static {
147+
var seed = Long.getLong(SEED_PROPERTY_NAME);
148+
if (seed != null) {
149+
// use explicitly set seed
150+
SEED = seed;
151+
} else {
152+
var v = Runtime.version();
153+
// promotable builds have build number, and it's greater than 0
154+
if (v.build().orElse(0) > 0) {
155+
// promotable build -> use 1st 8 bytes of md5($version)
156+
try {
157+
var md = MessageDigest.getInstance("MD5");
158+
var bytes = v.toString()
159+
.getBytes(StandardCharsets.UTF_8);
160+
bytes = md.digest(bytes);
161+
SEED = ByteBuffer.wrap(bytes).getLong();
162+
} catch (NoSuchAlgorithmException e) {
163+
throw new Error(e);
164+
}
165+
} else {
166+
// "personal" build -> use random seed
167+
SEED = new Random().nextLong();
168+
}
169+
}
170+
}
145171
/**
146172
* Returns the value of 'test.timeout.factor' system property
147173
* converted to {@code double}.
@@ -531,9 +557,13 @@ public static byte[] toByteArray(String hex) {
531557

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

0 commit comments

Comments
 (0)
Please sign in to comment.