Skip to content

Conversation

@210-reverof
Copy link
Contributor

Summary

Implement DeterministicRandom class with seed-based Random instance caching for deterministic test reproducibility.

Description

  • Created DeterministicRandom class that extends Random
    • Automatically caches Random instances using nextLong() generated values as keys
    • Provides getRandomInstance(long seed) to retrieve cached Random instances by seed
    • Provides getCurrentSeedRandom() to get the Random instance for the current seed
    • Implements cache size management with a limit of 32 entries to prevent memory leaks
  • Added 7 comprehensive property-based tests using JQwik to verify caching behavior and requirements

How Has This Been Tested?

  • Each test runs 1000 iterations with different seed values (7000 total test cases)
  • Build verification: ./gradlew :fixture-monkey-api:test passes successfully
  • Verified: cache creation on nextLong(), instance retrieval, cache size limit (≤32), deterministic behavior, and seed independence

Is the Document updated?

Not yet.

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

@API(since = "1.1.16", status = Status.MAINTAINED)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be EXPERIMENTAL

this.currentSeed = value;

if (!SEED_CACHE.containsKey(value)) {
manageCacheSize();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to extract the method; it would be better to inline it.

import org.apiguardian.api.API.Status;

@API(since = "1.1.16", status = Status.MAINTAINED)
public class DeterministicRandom extends Random {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to create a new interface for obtaining the Random instance and the seed value.

It is to avoid using the DeterministicRandom directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants