Class SharedSplittableRandomDetector

java.lang.Object
se.deversity.asynctest.diagnostics.SharedSplittableRandomDetector

public final class SharedSplittableRandomDetector extends Object
Detects SplittableRandom and JEP 356 RandomGenerator instances (L64X128MixRandom, Xoshiro256PlusPlus, …) shared across threads. These generators are documented not thread-safe: their state transition is a plain, non-atomic read-modify-write, so concurrent nextLong() calls interleave it — duplicated values, broken statistical guarantees, no exception. The failure is silent, which is what separates this from SHARED_RANDOM: java.util.Random is thread-safe but contended, these are simply corrupted.

java.util.Random and its subclasses are deliberately out of scope here — Random belongs to SHARED_RANDOM, SecureRandom to SHARED_SECURE_RANDOM, and ThreadLocalRandom to THREAD_LOCAL_RANDOM_MISUSE. One instanceof Random check excludes all three.

Synchronization awareness is partial. An access recorded while the accessing thread holds the generator's own monitor - the synchronized (generator) idiom - counts as guarded, and a generator whose every access was guarded produces no finding. A guard on any other lock counts once the test declares it with AsyncTestContext.holdingLock(...) or the agent sees it taken. A lock that was never declared is invisible and still fires; treat such a finding as a prompt to verify the synchronization, or to split() per thread.

Usage:


 @AsyncTest(threads = 4, includes = DetectorType.SHARED_SPLITTABLE_RANDOM)
 void testGeneratorSharing() {
     var d = AsyncTestContext.sharedSplittableRandomDetector();
     d.registerGenerator(splittable, "ids");
     long v = splittable.nextLong();
     d.recordAccess(splittable, "ids", "nextLong");
 }
 
Since:
1.8.0
  • Constructor Details

    • SharedSplittableRandomDetector

      public SharedSplittableRandomDetector()
  • Method Details

    • registerGenerator

      public void registerGenerator(RandomGenerator generator, String name)
      Register a generator for monitoring. java.util.Random subclasses are ignored — they belong to the dedicated detectors named in the class Javadoc.
      Parameters:
      generator - the generator to monitor (null-safe)
      name - a descriptive name for reporting (may be null)
    • recordAccess

      public void recordAccess(RandomGenerator generator, String name, String methodName)
      Record a generator access from the calling thread. Auto-registers unknown instances.
      Parameters:
      generator - the generator instance (null-safe)
      name - the generator name (should match registration; may be null)
      methodName - the method called (nextLong, nextInt, split, …)
    • analyze

      Evaluate the observed state and produce a report. Must be idempotent: calling it N times on quiescent state yields N identical reports.