diff --git a/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java b/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java index 0d855fd7..58338476 100644 --- a/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java @@ -7,6 +7,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; import java.util.function.Function; import java.util.regex.Pattern; @@ -26,7 +27,7 @@ public class GUIDTest { @Test public void testConstructorGUID() { - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { final long msb = random.nextLong(); final long lsb = random.nextLong(); @@ -38,7 +39,7 @@ public void testConstructorGUID() { @Test public void testConstructorLongs() { - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { final long msb = random.nextLong(); final long lsb = random.nextLong(); @@ -50,7 +51,7 @@ public void testConstructorLongs() { @Test public void testConstructorJdkUUID() { - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { final long msb = random.nextLong(); final long lsb = random.nextLong(); @@ -62,7 +63,7 @@ public void testConstructorJdkUUID() { @Test public void testConstructorString() { - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { final long msb = random.nextLong(); final long lsb = random.nextLong(); @@ -76,10 +77,10 @@ public void testConstructorString() { @Test public void testConstructorBytes() { - Random random = new Random(); + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { byte[] bytes = new byte[GUID.GUID_BYTES]; - random.nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); GUID guid = new GUID(bytes); assertEquals(Arrays.toString(bytes), Arrays.toString(guid.toBytes())); } @@ -249,7 +250,7 @@ public void testV7() { @Test public void testToUUID() { - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { final long msb = random.nextLong(); final long lsb = random.nextLong(); @@ -270,11 +271,11 @@ public void testToString() { @Test public void testToBytes() { - Random random = new Random(); + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { byte[] bytes1 = new byte[16]; - random.nextBytes(bytes1); + (new Random(seeder.nextLong())).nextBytes(bytes1); GUID guid0 = new GUID(bytes1); byte[] bytes2 = guid0.toBytes(); @@ -287,19 +288,19 @@ public void testToBytes() { @Test public void testHashCode() { - Random random = new Random(); + SplittableRandom seeder = new SplittableRandom(1); byte[] bytes = new byte[GUID.GUID_BYTES]; // invoked on the same object for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - random.nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); GUID guid1 = new GUID(bytes); assertEquals(guid1.hashCode(), guid1.hashCode()); } // invoked on two equal objects for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - random.nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); GUID guid1 = new GUID(bytes); GUID guid2 = new GUID(bytes); assertEquals(guid1.hashCode(), guid2.hashCode()); @@ -309,12 +310,12 @@ public void testHashCode() { @Test public void testEquals() { - Random random = new Random(); + SplittableRandom seeder = new SplittableRandom(1); byte[] bytes = new byte[GUID.GUID_BYTES]; for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - random.nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); GUID guid1 = new GUID(bytes); GUID guid2 = new GUID(bytes); assertEquals(guid1, guid2); @@ -336,7 +337,7 @@ public void testEquals() { public void testCompareTo() { final long zero = 0L; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); byte[] bytes = new byte[GUID.GUID_BYTES]; for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderDecoderTest.java b/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderDecoderTest.java index baeb21ed..621f18c7 100644 --- a/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderDecoderTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderDecoderTest.java @@ -4,8 +4,9 @@ import java.math.BigInteger; import java.util.Arrays; +import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import org.junit.Test; @@ -30,11 +31,12 @@ public void testDecode() { @Test public void testMultiply() { + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 1000; i++) { byte[] bytes = new byte[UUID_BYTES]; - ThreadLocalRandom.current().nextBytes(bytes); - long multiplier = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive - long addend = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive + (new Random(seeder.nextLong())).nextBytes(bytes); + long multiplier = seeder.nextInt() & 0x7fffffff; // positive + long addend = seeder.nextInt() & 0x7fffffff; // positive BigInteger number1 = new BigInteger(1, bytes); BigInteger product1 = number1.multiply(BigInteger.valueOf(multiplier)).add(BigInteger.valueOf(addend)); @@ -77,11 +79,12 @@ protected static byte[] fromLongs(long[] longs) { private String getRandomString(BaseN base) { + SplittableRandom random = new SplittableRandom(1); char[] chars = new char[base.getLength()]; chars[0] = base.getPadding(); // to avoid overflow for (int i = 1; i < chars.length; i++) { - chars[i] = base.getAlphabet().get(ThreadLocalRandom.current().nextInt(base.getRadix())); + chars[i] = base.getAlphabet().get(random.nextInt(base.getRadix())); } return new String(chars); diff --git a/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderEncoderTest.java b/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderEncoderTest.java index 4a6502f9..1a1d6538 100644 --- a/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderEncoderTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/codec/base/function/BaseNRemainderEncoderTest.java @@ -4,7 +4,8 @@ import java.math.BigInteger; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; +import java.util.Random; +import java.util.SplittableRandom; import org.junit.Test; @@ -22,10 +23,10 @@ public class BaseNRemainderEncoderTest { @Test public void testEncode() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 1000; i++) { byte[] bytes = new byte[UUID_BYTES]; - ThreadLocalRandom.current().nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); UUID uuid = BinaryCodec.INSTANCE.decode(bytes); String string = Base62Codec.INSTANCE.encode(uuid); assertEquals(encode(Base62Codec.INSTANCE.getBase(), bytes), string); @@ -34,11 +35,11 @@ public void testEncode() { @Test public void testDivide() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 1000; i++) { byte[] bytes = new byte[UUID_BYTES]; - ThreadLocalRandom.current().nextBytes(bytes); - int divisor = ThreadLocalRandom.current().nextInt() & 0x7fffffff; // positive divisor + (new Random(seeder.nextLong())).nextBytes(bytes); + int divisor = seeder.nextInt() & 0x7fffffff; // positive divisor CustomDivider divider = x -> new long[] { x / divisor, x % divisor }; diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactoryTest.java index a179a476..14f8f620 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/AbstRandomBasedFactoryTest.java @@ -15,8 +15,8 @@ import java.util.Arrays; import java.util.List; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; @@ -25,10 +25,10 @@ public class AbstRandomBasedFactoryTest extends UuidFactoryTest { @Test public void testByteRandomNextLong() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 10; i++) { byte[] bytes = new byte[Long.BYTES]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); long number = ByteBuffer.wrap(bytes).getLong(); AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes); assertEquals(number, random.nextLong()); @@ -40,7 +40,7 @@ public void testByteRandomNextLong() { int size = Long.BYTES * longs; byte[] bytes = new byte[size]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); ByteBuffer buffer1 = ByteBuffer.wrap(bytes); ByteBuffer buffer2 = ByteBuffer.wrap(bytes); @@ -58,10 +58,10 @@ public void testByteRandomNextLong() { @Test public void testByteRandomNextBytes() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 10; i++) { byte[] bytes = new byte[Long.BYTES]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.SafeRandom((x) -> bytes); assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES))); } @@ -72,7 +72,7 @@ public void testByteRandomNextBytes() { int size = Long.BYTES * ints; byte[] bytes = new byte[size]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); ByteBuffer buffer1 = ByteBuffer.wrap(bytes); ByteBuffer buffer2 = ByteBuffer.wrap(bytes); @@ -92,10 +92,10 @@ public void testByteRandomNextBytes() { @Test public void testLogRandomNextLong() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 10; i++) { byte[] bytes = new byte[Long.BYTES]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); long number = ByteBuffer.wrap(bytes).getLong(); AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number); assertEquals(number, random.nextLong()); @@ -107,7 +107,7 @@ public void testLogRandomNextLong() { int size = Long.BYTES * ints; byte[] bytes = new byte[size]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); ByteBuffer buffer1 = ByteBuffer.wrap(bytes); ByteBuffer buffer2 = ByteBuffer.wrap(bytes); @@ -122,10 +122,10 @@ public void testLogRandomNextLong() { @Test public void testLogRandomNextBytes() { - + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < 10; i++) { byte[] bytes = new byte[Long.BYTES]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); long number = ByteBuffer.wrap(bytes).getLong(); AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> number); assertEquals(Arrays.toString(bytes), Arrays.toString(random.nextBytes(Long.BYTES))); @@ -137,7 +137,7 @@ public void testLogRandomNextBytes() { int size = Long.BYTES * ints; byte[] bytes = new byte[size]; - (new Random()).nextBytes(bytes); + (new Random(seeder.nextLong())).nextBytes(bytes); ByteBuffer buffer1 = ByteBuffer.wrap(bytes); ByteBuffer buffer2 = ByteBuffer.wrap(bytes); @@ -163,7 +163,7 @@ public void testLongRandom() { } { - long nextLong = ThreadLocalRandom.current().nextLong(); + long nextLong = new SplittableRandom(1).nextLong(); AbstRandomBasedFactory.IRandom random = new AbstRandomBasedFactory.FastRandom(() -> nextLong); byte[] bytes = random.nextBytes(Long.BYTES); ByteBuffer buffer = ByteBuffer.wrap(bytes); @@ -198,11 +198,12 @@ public void testLongRandom() { } { + SplittableRandom entropy = new SplittableRandom(1); long[] nextLong = { // - ThreadLocalRandom.current().nextLong(), // - ThreadLocalRandom.current().nextLong(), // - ThreadLocalRandom.current().nextLong(), // - ThreadLocalRandom.current().nextLong() /// + entropy.nextLong(), // + entropy.nextLong(), // + entropy.nextLong(), // + entropy.nextLong() /// }; int octects = nextLong.length * Byte.SIZE; @@ -237,9 +238,10 @@ public void testLongRandomWithFactory() { { // RandomBasedFactory + SplittableRandom random = new SplittableRandom(1); List list = new ArrayList<>(); - list.add(ThreadLocalRandom.current().nextLong()); - list.add(ThreadLocalRandom.current().nextLong()); + list.add(random.nextLong()); + list.add(random.nextLong()); UUID uuid1 = UuidUtil.setVersion(new UUID(list.get(0), list.get(1)), 4); RandomBasedFactory factory = new RandomBasedFactory(() -> list.remove(0)); UUID uuid2 = factory.create(); diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/AbstTimeBasedFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/AbstTimeBasedFactoryTest.java index cd3b1d70..e58f6603 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/AbstTimeBasedFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/AbstTimeBasedFactoryTest.java @@ -3,7 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; import org.junit.Test; @@ -39,7 +39,7 @@ public void testSelectNodeIdentifierStrategy() { fail("It should use Random node identifier supplier"); } - Long number = ThreadLocalRandom.current().nextLong() & 0x0000ffffffffffffL; + Long number = new SplittableRandom(1).nextLong() & 0x0000ffffffffffffL; SettingsUtil.setProperty(SettingsUtil.PROPERTY_NODE, number.toString()); supplier = AbstTimeBasedFactory.selectNodeIdFunction(); assertEquals(number.longValue(), supplier.getAsLong()); diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/ClockSeqPoolTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/ClockSeqPoolTest.java index 352b417c..c9d7d37e 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/ClockSeqPoolTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/ClockSeqPoolTest.java @@ -4,7 +4,7 @@ import static org.junit.Assert.*; import java.util.HashSet; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; import com.github.f4b6a3.uuid.factory.function.impl.DefaultClockSeqFunction.ClockSeqPool; @@ -125,6 +125,7 @@ public void testClockSequencePool5() throws InterruptedException { private static class TestThread extends Thread { private int index; + private static SplittableRandom random = new SplittableRandom(1); public static int[] list = new int[CLOCK_SEQUENCE_MAX]; private ClockSeqPool pool; @@ -136,7 +137,7 @@ public TestThread(ClockSeqPool pool, int index) { @Override public void run() { - int take = ThreadLocalRandom.current().nextInt(CLOCK_SEQUENCE_MAX); + int take = random.nextInt(CLOCK_SEQUENCE_MAX); list[index] = pool.take(take); } } diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/DefaultTimeFunctionTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/DefaultTimeFunctionTest.java index c0b8bdbb..5ecf3fc6 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/DefaultTimeFunctionTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/DefaultTimeFunctionTest.java @@ -7,7 +7,7 @@ import java.time.Clock; import java.time.Instant; import java.time.ZoneOffset; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; public class DefaultTimeFunctionTest { @@ -33,10 +33,12 @@ public void testGetTimestampMillisecondWithClock() { final long ticks = (long) Math.pow(10, 4); final long bound = (long) Math.pow(2, 60) / ticks; + SplittableRandom random = new SplittableRandom(1); + for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // instantiate a factory with a Clock that returns a fixed value - final long millis = Math.abs(ThreadLocalRandom.current().nextLong(bound)); + final long millis = Math.abs(random.nextLong(bound)); Clock clock = Clock.fixed(Instant.ofEpochMilli(millis), ZoneOffset.UTC); DefaultTimeFunction function = new DefaultTimeFunction(clock); diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/WindowsTimeFunctionTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/WindowsTimeFunctionTest.java index a6980ed0..40bd7d04 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/WindowsTimeFunctionTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/function/impl/WindowsTimeFunctionTest.java @@ -7,7 +7,7 @@ import java.time.Clock; import java.time.Instant; import java.time.ZoneOffset; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; public class WindowsTimeFunctionTest { @@ -33,10 +33,12 @@ public void testGetTimestampMillisecondWithClock() { final long ticks = (long) Math.pow(10, 4); final long bound = (long) Math.pow(2, 60) / ticks; + SplittableRandom random = new SplittableRandom(1); + for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // instantiate a factory with a Clock that returns a fixed value - final long millis = Math.abs(ThreadLocalRandom.current().nextLong(bound)); + final long millis = Math.abs(random.nextLong(bound)); Clock clock = Clock.fixed(Instant.ofEpochMilli(millis), ZoneOffset.UTC); WindowsTimeFunction function = new WindowsTimeFunction(clock); diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/PrefixCombFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/PrefixCombFactoryTest.java index c9c65524..989a441e 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/PrefixCombFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/PrefixCombFactoryTest.java @@ -15,8 +15,8 @@ import java.time.Instant; import java.util.Arrays; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class PrefixCombFactoryTest extends UuidFactoryTest { @@ -40,10 +40,12 @@ public void testGetPrefixComb() { @Test public void testGetPrefixCombCheckTimestamp() { + SplittableRandom random = new SplittableRandom(1); + for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - long random = ThreadLocalRandom.current().nextLong(1L << 48); - Clock clock = Clock.fixed(Instant.ofEpochMilli(random), Clock.systemUTC().getZone()); + long time = random.nextLong(1L << 48); + Clock clock = Clock.fixed(Instant.ofEpochMilli(time), Clock.systemUTC().getZone()); Instant instant1 = clock.instant(); long timestamp1 = UuidTime.toUnixTimestamp(instant1); @@ -87,8 +89,8 @@ public void testGetPrefixCombCheckTime() { public void testGetPrefixCombWithRandom() { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - Random random = new Random(); - PrefixCombFactory factory = new PrefixCombFactory(random); + SplittableRandom seeder = new SplittableRandom(1); + PrefixCombFactory factory = new PrefixCombFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -102,9 +104,9 @@ public void testGetPrefixCombWithRandom() { @Test public void testGetPrefixCombWithRandomFunction() { - + SplittableRandom random = new SplittableRandom(1); UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier randomFunction = () -> random.nextLong(); PrefixCombFactory factory = new PrefixCombFactory(randomFunction); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -143,7 +145,7 @@ public void testGetPrefixCombInParallel() throws InterruptedException { public void testMinAndMax() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0000ffffffffffffL; for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortPrefixCombFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortPrefixCombFactoryTest.java index db925fa1..43f9ad74 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortPrefixCombFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortPrefixCombFactoryTest.java @@ -10,8 +10,8 @@ import java.util.Arrays; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class ShortPrefixCombFactoryTest extends UuidFactoryTest { @@ -91,8 +91,8 @@ public void testGetShortPrefixCombCheckTimeWithDifferentInterval() { public void testGetShortPrefixCombWithRandom() { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - Random random = new Random(); - ShortPrefixCombFactory factory = new ShortPrefixCombFactory(random); + SplittableRandom seeder = new SplittableRandom(1); + ShortPrefixCombFactory factory = new ShortPrefixCombFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -106,9 +106,9 @@ public void testGetShortPrefixCombWithRandom() { @Test public void testGetShortPrefixCombWithRandomFunction() { - + SplittableRandom random = new SplittableRandom(1); UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier randomFunction = () -> random.nextLong(); ShortPrefixCombFactory factory = new ShortPrefixCombFactory(randomFunction); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortSuffixCombFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortSuffixCombFactoryTest.java index a2b50d9c..b5c00466 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortSuffixCombFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/ShortSuffixCombFactoryTest.java @@ -11,8 +11,8 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class ShortSuffixCombFactoryTest extends UuidFactoryTest { @@ -92,8 +92,8 @@ public void testGetShortSuffixCombCheckTimeWithDifferentInterval() { public void testGetShortSuffixCombWithRandom() { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - Random random = new Random(); - ShortSuffixCombFactory factory = new ShortSuffixCombFactory(random); + SplittableRandom seeder = new SplittableRandom(1); + ShortSuffixCombFactory factory = new ShortSuffixCombFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -107,9 +107,9 @@ public void testGetShortSuffixCombWithRandom() { @Test public void testGetShortSuffixCombWithRandomFunction() { - + SplittableRandom random = new SplittableRandom(1); UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier randomFunction = () -> random.nextLong(); ShortSuffixCombFactory factory = new ShortSuffixCombFactory(randomFunction); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/SuffixCombFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/SuffixCombFactoryTest.java index 41b427af..3f0e9918 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/SuffixCombFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/nonstandard/SuffixCombFactoryTest.java @@ -15,8 +15,8 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class SuffixCombFactoryTest extends UuidFactoryTest { @@ -39,11 +39,11 @@ public void testGetSuffixComb() { @Test public void testGetSuffixCombCheckTimestamp() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - long random = ThreadLocalRandom.current().nextLong(1L << 48); - Clock clock = Clock.fixed(Instant.ofEpochMilli(random), Clock.systemUTC().getZone()); + long time = random.nextLong(1L << 48); + Clock clock = Clock.fixed(Instant.ofEpochMilli(time), Clock.systemUTC().getZone()); Instant instant1 = clock.instant(); long timestamp1 = UuidTime.toUnixTimestamp(instant1); @@ -86,8 +86,8 @@ public void testGetSuffixCombCheckTime() { public void testGetSuffixCombWithRandom() { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - Random random = new Random(); - SuffixCombFactory factory = new SuffixCombFactory(random); + SplittableRandom seeder = new SplittableRandom(1); + SuffixCombFactory factory = new SuffixCombFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -101,9 +101,9 @@ public void testGetSuffixCombWithRandom() { @Test public void testGetSuffixCombWithRandomFunction() { - + SplittableRandom random = new SplittableRandom(1); UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier randomFunction = () -> random.nextLong(); SuffixCombFactory factory = new SuffixCombFactory(randomFunction); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -141,7 +141,7 @@ public void testGetSuffixCombInParallel() throws InterruptedException { public void testMinAndMax() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0000ffffffffffffL; for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/RandomBasedFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/RandomBasedFactoryTest.java index 0f5a4290..ab639a7b 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/RandomBasedFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/RandomBasedFactoryTest.java @@ -9,8 +9,8 @@ import static org.junit.Assert.assertEquals; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class RandomBasedFactoryTest extends UuidFactoryTest { @@ -47,8 +47,8 @@ public void testGetRandomBasedFast() { public void testGetRandomBasedWithRandom() { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - Random random = new Random(); - RandomBasedFactory factory = new RandomBasedFactory(random); + SplittableRandom seeder = new SplittableRandom(1); + RandomBasedFactory factory = new RandomBasedFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -61,9 +61,9 @@ public void testGetRandomBasedWithRandom() { @Test public void testGetRandomBasedWithRandomFunction() { - + SplittableRandom random = new SplittableRandom(1); UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier randomFunction = () -> random.nextLong(); RandomBasedFactory factory = new RandomBasedFactory(randomFunction); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeBasedFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeBasedFactoryTest.java index a7a068be..c79ece30 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeBasedFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeBasedFactoryTest.java @@ -16,9 +16,8 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; -import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; public class TimeBasedFactoryTest extends UuidFactoryTest { @@ -136,18 +135,18 @@ public void testGetTimeBasedInParallel() throws InterruptedException { @Test public void testGetTimeBasedWithOptionalArguments() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // Get 46 random bits to generate a date from the year 1970 to 2193. // (2^46 / 10000 / 60 / 60 / 24 / 365.25 + 1970 A.D. = ~2193 A.D.) - final Instant instant = Instant.ofEpochMilli(ThreadLocalRandom.current().nextLong() >>> 24); + final Instant instant = Instant.ofEpochMilli(random.nextLong() >>> 24); // Get 14 random bits random to generate the clock sequence - final int clockseq = ThreadLocalRandom.current().nextInt() & 0x000003ff; + final int clockseq = random.nextInt() & 0x000003ff; // Get 48 random bits for the node identifier, and set the multicast bit to ONE - final long nodeid = ThreadLocalRandom.current().nextLong() & 0x0000ffffffffffffL | 0x0000010000000000L; + final long nodeid = random.nextLong() & 0x0000ffffffffffffL | 0x0000010000000000L; // Create a time-based UUID with those random values UUID uuid = UuidCreator.getTimeBased(instant, clockseq, nodeid); @@ -166,7 +165,7 @@ public void testGetTimeBasedWithOptionalArguments() { public void testMinAndMax() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0fffffffffffffffL; for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactoryTest.java index fc2d2668..04afd91e 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedEpochFactoryTest.java @@ -12,7 +12,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.security.SecureRandom; import java.time.Clock; import java.time.Instant; import java.time.ZoneId; @@ -21,7 +20,6 @@ import java.util.SplittableRandom; import java.util.TreeSet; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongSupplier; public class TimeOrderedEpochFactoryTest extends UuidFactoryTest { @@ -34,39 +32,37 @@ public void testDefault() { @Test public void testWithRandom() { + SplittableRandom seeder = new SplittableRandom(1); { - Random random = new Random(); - TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(random); + TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(new Random(seeder.nextLong())); assertNotNull(factory.create()); } { - SecureRandom random = new SecureRandom(); - TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(random); + TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(new Random(seeder.nextLong())); assertNotNull(factory.create()); } } @Test public void testWithRandomFunction() { + SplittableRandom random = new SplittableRandom(1); { - SplittableRandom random = new SplittableRandom(); LongSupplier function = () -> random.nextLong(); TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(function); assertNotNull(factory.create()); } { - LongSupplier function = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier function = () -> random.nextLong(); TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(function); assertNotNull(factory.create()); } { - SplittableRandom random = new SplittableRandom(); LongSupplier function = () -> random.nextLong(); TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(function, Clock.systemDefaultZone()); assertNotNull(factory.create()); } { - LongSupplier function = () -> ThreadLocalRandom.current().nextLong(); + LongSupplier function = () -> random.nextLong(); TimeOrderedEpochFactory factory = new TimeOrderedEpochFactory(function, Clock.systemDefaultZone()); assertNotNull(factory.create()); } @@ -116,11 +112,11 @@ public void testGetTimeOrderedEpoch() { @Test public void testGetTimeOrderedEpochCheckTimestamp() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - long random = ThreadLocalRandom.current().nextLong(1L << 48); - Clock clock = Clock.fixed(Instant.ofEpochMilli(random), Clock.systemUTC().getZone()); + long time = random.nextLong(1L << 48); + Clock clock = Clock.fixed(Instant.ofEpochMilli(time), Clock.systemUTC().getZone()); Instant instant1 = clock.instant(); long timestamp1 = UuidTime.toGregTimestamp(instant1); @@ -222,12 +218,12 @@ private void checkCreationTime(UUID[] list, long startTime, long endTime) { @Test public void testGetTimeOrderedEpochWithRandom() { - Random random = new Random(); + SplittableRandom seeder = new SplittableRandom(1); TimeOrderedEpochFactory factory; { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; - factory = new TimeOrderedEpochFactory(random); + factory = new TimeOrderedEpochFactory(new Random(seeder.nextLong())); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { list[i] = factory.create(); @@ -240,6 +236,7 @@ public void testGetTimeOrderedEpochWithRandom() { { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; + Random random = new Random(seeder.nextLong()); factory = TimeOrderedEpochFactory.builder().withRandom(random).withIncrementPlus1().build(); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -253,6 +250,7 @@ public void testGetTimeOrderedEpochWithRandom() { { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; + Random random = new Random(seeder.nextLong()); factory = TimeOrderedEpochFactory.builder().withRandom(random).withIncrementPlusN().build(); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -266,6 +264,7 @@ public void testGetTimeOrderedEpochWithRandom() { { UUID[] list = new UUID[DEFAULT_LOOP_MAX]; + Random random = new Random(seeder.nextLong()); factory = TimeOrderedEpochFactory.builder().withRandom(random).withIncrementPlusN(1_000_000).build(); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { @@ -280,8 +279,8 @@ public void testGetTimeOrderedEpochWithRandom() { @Test public void testGetTimeOrderedEpochWithRandomFunction() { - - LongSupplier randomFunction = () -> ThreadLocalRandom.current().nextLong(); + SplittableRandom random = new SplittableRandom(1); + LongSupplier randomFunction = () -> random.nextLong(); TimeOrderedEpochFactory factory; @@ -341,14 +340,15 @@ public void testGetTimeOrderedEpochWithRandomFunction() { @Test public void testGetTimeOrderedEpochInParallel() throws InterruptedException { - Random random = new Random(); Clock clock = Clock.systemDefaultZone(); + SplittableRandom seeder = new SplittableRandom(1); Thread[] threads = new Thread[THREAD_TOTAL]; TestThread.clearHashSet(); // Instantiate and start many threads for (int i = 0; i < THREAD_TOTAL; i++) { + Random random = new Random(seeder.nextLong()); threads[i] = new TestThread(new TimeOrderedEpochFactory(random, clock), DEFAULT_LOOP_MAX); threads[i].start(); } @@ -431,7 +431,7 @@ public void testWithFixedClock() { public void testMinAndMax() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0000ffffffffffffL; for (int i = 0; i < 100; i++) { @@ -462,7 +462,7 @@ public void testMinAndMax() { public void testRandom() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0000ffffffffffffL; for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedFactoryTest.java b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedFactoryTest.java index 5e414fe5..63bf6b84 100644 --- a/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedFactoryTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/factory/rfc4122/TimeOrderedFactoryTest.java @@ -15,9 +15,8 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; -import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; public class TimeOrderedFactoryTest extends UuidFactoryTest { @@ -135,18 +134,18 @@ public void testGetTimeOrderedInParallel() throws InterruptedException { @Test public void testGetTimeOrderedWithOptionalArguments() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // Get 46 random bits to generate a date from the year 1970 to 2193. // (2^46 / 10000 / 60 / 60 / 24 / 365.25 + 1970 A.D. = ~2193 A.D.) - final Instant instant = Instant.ofEpochMilli(ThreadLocalRandom.current().nextLong() >>> 24); + final Instant instant = Instant.ofEpochMilli(random.nextLong() >>> 24); // Get 14 random bits random to generate the clock sequence - final int clockseq = ThreadLocalRandom.current().nextInt() & 0x000003ff; + final int clockseq = random.nextInt() & 0x000003ff; // Get 48 random bits for the node identifier, and set the multicast bit to ONE - final long nodeid = ThreadLocalRandom.current().nextLong() & 0x0000ffffffffffffL | 0x0000010000000000L; + final long nodeid = random.nextLong() & 0x0000ffffffffffffL | 0x0000010000000000L; // Create a time-based UUID with those random values UUID uuid = UuidCreator.getTimeOrdered(instant, clockseq, nodeid); @@ -208,7 +207,7 @@ public void testGetTimeOrderedCheckOrder() { public void testMinAndMax() { long time = 0; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); final long mask = 0x0fffffffffffffffL; for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/github/f4b6a3/uuid/util/CombUtilTest.java b/src/test/java/com/github/f4b6a3/uuid/util/CombUtilTest.java index a20f9ef0..01c62d83 100644 --- a/src/test/java/com/github/f4b6a3/uuid/util/CombUtilTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/util/CombUtilTest.java @@ -6,7 +6,7 @@ import java.time.Instant; import java.time.ZoneOffset; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; import org.junit.Test; @@ -37,11 +37,11 @@ public void testGetPrefixWithClock() { final int bits = 48; final long bound = (long) Math.pow(2, bits); - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // instantiate a factory with a Clock that returns a fixed value - final long millis = Math.abs(ThreadLocalRandom.current().nextLong(bound)); + final long millis = Math.abs(random.nextLong(bound)); Clock clock = Clock.fixed(Instant.ofEpochMilli(millis), ZoneOffset.UTC); AbstRandomBasedFactory factory = new PrefixCombFactory(clock); @@ -71,11 +71,11 @@ public void testGetSuffixWithClock() { final int bits = 48; final long bound = (long) Math.pow(2, bits); - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { // instantiate a factory with a Clock that returns a fixed value - final long millis = Math.abs(ThreadLocalRandom.current().nextLong(bound)); + final long millis = Math.abs(random.nextLong(bound)); Clock clock = Clock.fixed(Instant.ofEpochMilli(millis), ZoneOffset.UTC); AbstRandomBasedFactory factory = new SuffixCombFactory(clock); diff --git a/src/test/java/com/github/f4b6a3/uuid/util/UuidBuilderTest.java b/src/test/java/com/github/f4b6a3/uuid/util/UuidBuilderTest.java index 6587570e..c84400b5 100644 --- a/src/test/java/com/github/f4b6a3/uuid/util/UuidBuilderTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/util/UuidBuilderTest.java @@ -6,6 +6,7 @@ import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; import org.junit.Test; @@ -15,7 +16,6 @@ public class UuidBuilderTest { private static final int DEFAULT_LOOP_MAX = 1_000; - private static final Random seeder = new Random(1L); @Test public void testVersionNumber() { @@ -23,8 +23,8 @@ public void testVersionNumber() { assertEquals(0x0L, new UuidBuilder().put((long) 0L).put((long) 0L).build().version()); assertEquals(0xfL, new UuidBuilder().put((long) -1L).put((long) -1L).build().version()); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < 15; i++) { - Random random = new Random(seeder.nextLong()); assertEquals(i, new UuidBuilder(i).put(random.nextLong()).put(random.nextLong()).build().version()); } } @@ -32,9 +32,9 @@ public void testVersionNumber() { @Test public void testPutTwoLongsWithoutVersionNumber() { + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Random random = new Random(seeder.nextLong()); long msb = random.nextLong(); long lsb = random.nextLong(); @@ -50,10 +50,9 @@ public void testPutTwoLongsWithVersion4() { int version = 4; + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Random random = new Random(seeder.nextLong()); - long msb = random.nextLong(); long lsb = random.nextLong(); @@ -69,10 +68,9 @@ public void testPutFourIntsWithVersion4() { int version = 4; + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Random random = new Random(seeder.nextLong()); - int part1 = random.nextInt(); int part2 = random.nextInt(); int part3 = random.nextInt(); @@ -93,15 +91,14 @@ public void testPutSixteenBytesWithVersion4() { int version = 4; + SplittableRandom seeder = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Random random = new Random(seeder.nextLong()); - byte[] bytes1 = new byte[8]; - random.nextBytes(bytes1); + (new Random(seeder.nextLong())).nextBytes(bytes1); byte[] bytes2 = new byte[8]; - random.nextBytes(bytes2); + (new Random(seeder.nextLong())).nextBytes(bytes2); long msb = ByteUtil.toNumber(bytes1); long lsb = ByteUtil.toNumber(bytes2); @@ -117,10 +114,9 @@ public void testPutTwoIntsFourShortsWithVersion8() { int version = 8; + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Random random = new Random(seeder.nextLong()); - int p1 = random.nextInt(); short p2 = (short) random.nextInt(); short p3 = (short) random.nextInt(); diff --git a/src/test/java/com/github/f4b6a3/uuid/util/UuidComparatorTest.java b/src/test/java/com/github/f4b6a3/uuid/util/UuidComparatorTest.java index 5a4368e3..ce956ee3 100644 --- a/src/test/java/com/github/f4b6a3/uuid/util/UuidComparatorTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/util/UuidComparatorTest.java @@ -4,7 +4,7 @@ import java.math.BigInteger; import java.nio.ByteBuffer; -import java.util.Random; +import java.util.SplittableRandom; import java.util.UUID; import org.junit.Test; @@ -75,7 +75,7 @@ public void testCompareDefault() { final long zero = 0L; byte[] bytes = new byte[16]; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { bytes = ByteBuffer.allocate(16).putLong(random.nextLong()).putLong(random.nextLong()).array(); @@ -164,7 +164,7 @@ public void testCompareOpaque() { final long zero = 0L; byte[] bytes = new byte[16]; - Random random = new Random(); + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { bytes = ByteBuffer.allocate(16).putLong(random.nextLong()).putLong(random.nextLong()).array(); diff --git a/src/test/java/com/github/f4b6a3/uuid/util/UuidUtilTest.java b/src/test/java/com/github/f4b6a3/uuid/util/UuidUtilTest.java index fd3b6c47..57a13095 100644 --- a/src/test/java/com/github/f4b6a3/uuid/util/UuidUtilTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/util/UuidUtilTest.java @@ -5,7 +5,7 @@ import java.time.Instant; import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; import org.junit.Test; @@ -78,10 +78,10 @@ public void testCheckCompatibility() { @Test public void testGetTimestamp() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Instant instant = UuidTime.fromGregTimestamp(ThreadLocalRandom.current().nextLong() & GREG_TIMESTAMP_MASK); + Instant instant = UuidTime.fromGregTimestamp(random.nextLong() & GREG_TIMESTAMP_MASK); long unixTimestamp = UuidTime.toUnixTimestamp(instant); long gregTimestamp = UuidTime.toGregTimestamp(instant); @@ -105,10 +105,10 @@ public void testGetTimestamp() { @Test public void testGetInstant() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - Instant instant = UuidTime.fromGregTimestamp(ThreadLocalRandom.current().nextLong() & GREG_TIMESTAMP_MASK); + Instant instant = UuidTime.fromGregTimestamp(random.nextLong() & GREG_TIMESTAMP_MASK); TimeBasedFactory factory1 = TimeBasedFactory.builder().withInstant(instant).build(); UUID uuid1 = factory1.create(); @@ -129,8 +129,9 @@ public void testGetInstant() { @Test public void testGetClockSequence() { + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - long clockSequence1 = ThreadLocalRandom.current().nextLong() & CLOCK_SEQUENCE_MASK; + long clockSequence1 = random.nextLong() & CLOCK_SEQUENCE_MASK; UUID uuid = TimeBasedFactory.builder().withClockSeq(clockSequence1).build().create(); long clockSequence2 = getClockSequence(uuid); assertEquals(clockSequence1, clockSequence2); @@ -139,8 +140,9 @@ public void testGetClockSequence() { @Test public void testGetNodeIdentifier() { + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - long nodeIdentifier1 = ThreadLocalRandom.current().nextLong() & NODE_IDENTIFIER_MASK; + long nodeIdentifier1 = random.nextLong() & NODE_IDENTIFIER_MASK; UUID uuid = TimeBasedFactory.builder().withNodeId(nodeIdentifier1).build().create(); long nodeIdentifier2 = getNodeIdentifier(uuid); assertEquals(nodeIdentifier1, nodeIdentifier2); @@ -149,10 +151,10 @@ public void testGetNodeIdentifier() { @Test public void testGetLocalDomain() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - int localIdentifier1 = ThreadLocalRandom.current().nextInt(); - byte localDomain1 = (byte) ThreadLocalRandom.current().nextInt(); + int localIdentifier1 = random.nextInt(); + byte localDomain1 = (byte) random.nextInt(); UUID uuid = UuidCreator.getDceSecurity(localDomain1, localIdentifier1); byte localDomain2 = getLocalDomain(uuid); assertEquals(localDomain1, localDomain2); @@ -161,10 +163,10 @@ public void testGetLocalDomain() { @Test public void testGetLocalIdentifier() { - + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < DEFAULT_LOOP_MAX; i++) { - int localIdentifier1 = ThreadLocalRandom.current().nextInt(); - byte localDomain1 = (byte) ThreadLocalRandom.current().nextInt(); + int localIdentifier1 = random.nextInt(); + byte localDomain1 = (byte) random.nextInt(); UUID uuid = UuidCreator.getDceSecurity(localDomain1, localIdentifier1); int localIdentifier2 = getLocalIdentifier(uuid); assertEquals(localIdentifier1, localIdentifier2); diff --git a/src/test/java/com/github/f4b6a3/uuid/util/internal/SettingsUtilTest.java b/src/test/java/com/github/f4b6a3/uuid/util/internal/SettingsUtilTest.java index 0d468b8c..c3378a06 100644 --- a/src/test/java/com/github/f4b6a3/uuid/util/internal/SettingsUtilTest.java +++ b/src/test/java/com/github/f4b6a3/uuid/util/internal/SettingsUtilTest.java @@ -2,7 +2,7 @@ import static org.junit.Assert.*; -import java.util.concurrent.ThreadLocalRandom; +import java.util.SplittableRandom; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -22,8 +22,9 @@ public static void afterClass() { @Test public void testSetNodeIdentifier() { + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < 100; i++) { - long number = ThreadLocalRandom.current().nextLong() >>> 16; + long number = random.nextLong() >>> 16; SettingsUtil.setNodeIdentifier(number); long result = SettingsUtil.getNodeIdentifier(); assertEquals(number, result); @@ -32,8 +33,9 @@ public void testSetNodeIdentifier() { @Test public void testSetProperty() { + SplittableRandom random = new SplittableRandom(1); for (int i = 0; i < 100; i++) { - long number = ThreadLocalRandom.current().nextLong() >>> 16; + long number = random.nextLong() >>> 16; String string = Long.toString(number); SettingsUtil.setProperty(SettingsUtil.PROPERTY_NODE, string); long result = SettingsUtil.getNodeIdentifier(); @@ -43,7 +45,8 @@ public void testSetProperty() { @Test public void testSetPropertyWith0x() { - long number = ThreadLocalRandom.current().nextLong() >>> 16; + SplittableRandom random = new SplittableRandom(1); + long number = random.nextLong() >>> 16; String string = "0x" + Long.toHexString(number); SettingsUtil.setProperty(SettingsUtil.PROPERTY_NODE, string); long result = SettingsUtil.getNodeIdentifier();