Use a static sequence of randomness in tests
The RemoteConfigControllerTest#testMath unit test would occassionally fail because randomness doesn't necessarily group into expected ranges over a finite trial count. This changes the test to use a predefined PRNG sequence instead of one that varies with each test so that the test will no long randomly fail.
This commit is contained in:
parent
0c495e7e72
commit
48c324fe86
|
@ -22,6 +22,7 @@ import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -263,12 +264,13 @@ public class RemoteConfigControllerTest {
|
||||||
Map<String, Integer> enabledMap = new HashMap<>();
|
Map<String, Integer> enabledMap = new HashMap<>();
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
||||||
int iterations = 100000;
|
int iterations = 100000;
|
||||||
|
SecureRandom secureRandom = new SecureRandom(new byte[]{42}); // the seed value doesn't matter so much as it's constant to make the test not flaky
|
||||||
|
|
||||||
for (int i=0;i<iterations;i++) {
|
for (int i=0;i<iterations;i++) {
|
||||||
for (RemoteConfig config : remoteConfigList) {
|
for (RemoteConfig config : remoteConfigList) {
|
||||||
int count = enabledMap.getOrDefault(config.getName(), 0);
|
int count = enabledMap.getOrDefault(config.getName(), 0);
|
||||||
|
|
||||||
if (RemoteConfigController.isInBucket(digest, UUID.randomUUID(), config.getName().getBytes(), config.getPercentage(), new HashSet<>())) {
|
if (RemoteConfigController.isInBucket(digest, getRandomUUID(secureRandom), config.getName().getBytes(), config.getPercentage(), new HashSet<>())) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,5 +287,13 @@ public class RemoteConfigControllerTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static UUID getRandomUUID(SecureRandom secureRandom) {
|
||||||
|
long mostSignificantBits = secureRandom.nextLong();
|
||||||
|
long leastSignificantBits = secureRandom.nextLong();
|
||||||
|
mostSignificantBits &= 0xffffffffffff0fffL;
|
||||||
|
mostSignificantBits |= 0x0000000000004000L;
|
||||||
|
leastSignificantBits &= 0x3fffffffffffffffL;
|
||||||
|
leastSignificantBits |= 0x8000000000000000L;
|
||||||
|
return new UUID(mostSignificantBits, leastSignificantBits);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue