Skip to content

Commit

Permalink
Fix set seed indirectly to prepare deterministic testing (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Dec 9, 2024
1 parent 3b63f0d commit 913ed4f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
6 changes: 6 additions & 0 deletions docs/content/v1.1.x-kor/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ menu:
docs:
weight: 100
---
sectionStart
## v.1.1.6
Deprecate `Randoms.create(String)`, use `Randoms.setSeed(long)` instead.

sectionEnd

sectionStart
## v.1.1.5
Fix keep equivalence with `ElemenetProperty` and `MapKeyElementProperty` and `MapValueElementProperty`.
Expand Down
6 changes: 6 additions & 0 deletions docs/content/v1.1.x/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ menu:
docs:
weight: 100
---
sectionStart
## v.1.1.6
Deprecate `Randoms.create(String)`, use `Randoms.setSeed(long)` instead.

sectionEnd

sectionStart
## v.1.1.5
Fix keep equivalence with `ElemenetProperty` and `MapKeyElementProperty` and `MapValueElementProperty`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE")
public abstract class Randoms {
private static final boolean USE_JQWIK_ENGINE;
private static final ThreadLocal<Random> CURRENT = new ThreadLocal<>();
private static final ThreadLocal<Long> SEED = new ThreadLocal<>();
private static final ThreadLocal<Random> CURRENT;
private static final ThreadLocal<Long> SEED;

static {
boolean useJqwikEngine;
Expand All @@ -47,23 +47,22 @@ public abstract class Randoms {
useJqwikEngine = false;
}
USE_JQWIK_ENGINE = useJqwikEngine;
SEED = ThreadLocal.withInitial(System::nanoTime);
CURRENT = ThreadLocal.withInitial(() -> Randoms.create(SEED.get()));
}

/**
* It is deprecated. Do not use this method.
* Use {@link #setSeed(long)} instead.
*/
@Deprecated
public static Random create(String seed) {
if (USE_JQWIK_ENGINE) {
SEED.set(Long.parseLong(seed));
return SourceOfRandomness.create(seed);
}
setSeed(Long.parseLong(seed));
return CURRENT.get();
}

try {
long actualSeed = Long.parseLong(seed);
Random random = newRandom(actualSeed);
CURRENT.set(random);
SEED.set(actualSeed);
return random;
} catch (NumberFormatException nfe) {
throw new JqwikException(String.format("[%s] is not a valid random seed.", seed));
}
public static void setSeed(long seed) {
SEED.set(seed);
}

public static Random current() {
Expand All @@ -80,6 +79,22 @@ public static int nextInt(int bound) {
return current().nextInt(bound);
}

private static Random create(long seed) {
if (USE_JQWIK_ENGINE) {
SEED.set(seed);
return SourceOfRandomness.create(String.valueOf(seed));
}

try {
Random random = newRandom(seed);
CURRENT.set(random);
SEED.set(seed);
return random;
} catch (NumberFormatException nfe) {
throw new JqwikException(String.format("[%s] is not a valid random seed.", seed));
}
}

private static Random newRandom(final long seed) {
return USE_JQWIK_ENGINE
? SourceOfRandomness.newRandom(seed)
Expand All @@ -89,7 +104,7 @@ private static Random newRandom(final long seed) {
/**
* A faster but not thread safe implementation of {@linkplain java.util.Random}.
* It also has a period of 2^n - 1 and better statistical randomness.
*
* <p>
* See for details: https://www.javamex.com/tutorials/random_numbers/xorshift.shtml
*
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterTestExecution(ExtensionContext context) throws Exception {
* Sets the seed for generating random numbers.
**/
private void setSeed(long seed) {
Randoms.create(String.valueOf(seed));
Randoms.setSeed(seed);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public FixtureMonkey build() {
fixtureMonkeyOptions.getDecomposedContainerValueFactory()
);

Randoms.create(String.valueOf(seed));
Randoms.setSeed(seed);
return new FixtureMonkey(
fixtureMonkeyOptions,
manipulatorOptimizer,
Expand Down

0 comments on commit 913ed4f

Please sign in to comment.