From 48c324fe8684433efc2af1727b663be8ebf0a207 Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Wed, 29 Apr 2020 17:31:43 -0700 Subject: [PATCH] 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. --- .../controllers/RemoteConfigControllerTest.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/RemoteConfigControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/RemoteConfigControllerTest.java index d1b3856dd..35cbe54db 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/RemoteConfigControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/RemoteConfigControllerTest.java @@ -22,6 +22,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -263,12 +264,13 @@ public class RemoteConfigControllerTest { Map enabledMap = new HashMap<>(); MessageDigest digest = MessageDigest.getInstance("SHA1"); 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())) { + if (RemoteConfigController.isInBucket(digest, getRandomUUID(secureRandom), config.getName().getBytes(), config.getPercentage(), new HashSet<>())) { 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); + } }