Class AssertingRandom

  • All Implemented Interfaces:
    java.io.Serializable

    public final class AssertingRandom
    extends java.util.Random
    A random with a delegate, preventing Random.setSeed(long) and locked to be used by a single thread.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.StackTraceElement[] allocationStack  
      private static boolean assertionsEnabled
      Enable paranoid mode when assertions are enabled.
      private java.util.Random delegate  
      private java.lang.String ownerName  
      private java.lang.ref.WeakReference<java.lang.Thread> ownerRef  
      private boolean valid
      Track out-of-context use of this Random instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      AssertingRandom​(java.lang.Thread owner, java.util.Random delegate)
      Creates an instance to be used by owner thread and delegating to delegate until destroy()ed.
    • Field Detail

      • delegate

        private final java.util.Random delegate
      • ownerRef

        private final java.lang.ref.WeakReference<java.lang.Thread> ownerRef
      • ownerName

        private final java.lang.String ownerName
      • allocationStack

        private final java.lang.StackTraceElement[] allocationStack
      • valid

        private volatile boolean valid
        Track out-of-context use of this Random instance. This introduces memory barriers and scheduling side-effects but there's no other way to do it in any other way and sharing randoms across threads or test cases is very bad and worth tracking.
      • assertionsEnabled

        private static final boolean assertionsEnabled
        Enable paranoid mode when assertions are enabled.
    • Constructor Detail

      • AssertingRandom

        public AssertingRandom​(java.lang.Thread owner,
                               java.util.Random delegate)
        Creates an instance to be used by owner thread and delegating to delegate until destroy()ed.
    • Method Detail

      • next

        protected int next​(int bits)
        Overrides:
        next in class java.util.Random
      • nextBoolean

        public boolean nextBoolean()
        Overrides:
        nextBoolean in class java.util.Random
      • nextBytes

        public void nextBytes​(byte[] bytes)
        Overrides:
        nextBytes in class java.util.Random
      • nextDouble

        public double nextDouble()
        Overrides:
        nextDouble in class java.util.Random
      • nextFloat

        public float nextFloat()
        Overrides:
        nextFloat in class java.util.Random
      • nextGaussian

        public double nextGaussian()
        Overrides:
        nextGaussian in class java.util.Random
      • nextInt

        public int nextInt()
        Overrides:
        nextInt in class java.util.Random
      • nextInt

        public int nextInt​(int n)
        Overrides:
        nextInt in class java.util.Random
      • nextLong

        public long nextLong()
        Overrides:
        nextLong in class java.util.Random
      • setSeed

        public void setSeed​(long seed)
        Overrides:
        setSeed in class java.util.Random
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • destroy

        public void destroy()
        This object will no longer be usable after this method is called.
      • checkValid

        private final void checkValid()
      • clone

        protected java.lang.Object clone()
                                  throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • isVerifying

        public static boolean isVerifying()
        Returns:
        Return true if this class is verifying sharing and lifecycle assertions.
        See Also:
        "https://github.com/randomizedtesting/randomizedtesting/issues/234"
      • noSetSeed

        static java.lang.RuntimeException noSetSeed()