Class RandomizedContext

java.lang.Object
com.carrotsearch.randomizedtesting.RandomizedContext

public final class RandomizedContext extends Object
Context variables for an execution of a test suite (hooks and tests) running under a RandomizedRunner.
  • Field Details

  • Constructor Details

  • Method Details

    • getTargetClass

      public Class<?> getTargetClass()
      The class (suite) being tested.
    • getRunnerSeed

      long getRunnerSeed()
      Runner's seed.
    • getRunnerSeedAsString

      public String getRunnerSeedAsString()
      Returns the runner's master seed, formatted.
    • getRandomness

      public Randomness getRandomness()
      Source of randomness for the context's thread.
    • getRandomnesses

      Randomness[] getRandomnesses()
      Return all Randomness on the stack for the current thread. The most recent (currently used) randomness comes last in this array.
    • getRandom

      public Random getRandom()
      A shorthand for calling getRandomness() and then Randomness.getRandom().
    • isNightly

      public boolean isNightly()
      Return true if Nightly test group is explicitly enabled.
      See Also:
    • current

      public static RandomizedContext current()
      Returns:
      Returns the context for the calling thread or throws an IllegalStateException if the thread is out of scope.
      Throws:
      IllegalStateException - If context is not available.
    • getRunner

      public RandomizedRunner getRunner()
      Access to the runner governing this context.
    • closeAtEnd

      public <T extends Closeable> T closeAtEnd(T resource, LifecycleScope scope)
      Dispose the given resource at the end of a given lifecycle scope. If the Closeable throws an exception, the test case or suite will end in a failure.
      Returns:
      resource (for call chaining).
    • getGroupEvaluator

      public GroupEvaluator getGroupEvaluator()
      Provide access to GroupEvaluator.
    • runWithPrivateRandomness

      public <T> T runWithPrivateRandomness(Randomness randomness, Callable<T> callable) throws Exception
      Pushes the given randomness to the top of the stack, runs the Callable and disposes the randomness before the this method returns.

      This utility method can be used to initialize resources in a reproducible way since all calls to utility methods like RandomizedTest.randomInt() et.al. are forwarded to the current RandomContext which uses the provided randomness from the top of the stack.

      Type Parameters:
      T - the return type of the callable
      Parameters:
      randomness - the randomness to push to the top of the stack
      callable - the callable to execute
      Returns:
      the result of the call to Callable.call()
      Throws:
      Exception - if Callable.call() throws an exception
    • runWithPrivateRandomness

      public <T> T runWithPrivateRandomness(long seed, Callable<T> callable) throws Exception
      Pushes the given randomness to the top of the stack, runs the Callable and disposes the randomness before the this method returns.

      This utility method can be used to initialize resources in a reproducible way since all calls to utility methods like RandomizedTest.randomInt() et.al. are forwarded to the current RandomContext which uses the provided randomness from the top of the stack.

      Type Parameters:
      T - the return type of the callable
      Parameters:
      seed - The initial seed for the new, private randomness
      callable - the callable to execute
      Returns:
      the result of the call to Callable.call()
      Throws:
      Exception - if Callable.call() throws an exception
    • closeResources

      void closeResources(ObjectProcedure<CloseableResourceInfo> consumer, LifecycleScope scope)
      Dispose of any resources registered in the given scope.
    • context

      static RandomizedContext context(Thread thread)
    • create

      static RandomizedContext create(ThreadGroup tg, Class<?> suiteClass, RandomizedRunner runner)
      Create a new context bound to a thread group.
    • dispose

      void dispose()
      Dispose of the context.
    • push

      void push(Randomness rnd)
      Push a new randomness on top of the stack.
    • popAndDestroy

      void popAndDestroy()
      Pop a randomness off the stack and dispose it.
    • getPerThread

      private RandomizedContext.PerThreadResources getPerThread()
      Return per-thread resources associated with the current thread.
    • checkDisposed

      private void checkDisposed()
      Throw an exception if disposed.
    • cloneFor

      static void cloneFor(Thread t)
      Clone context information between the current thread and another thread. This is for internal use only to propagate context information when forking.
    • setTargetMethod

      void setTargetMethod(Method method)
    • getTargetMethod

      public Method getTargetMethod()
      Returns:
      Return the currently executing test case method (the thread may still be within test rules and may never actually hit the method). This method may return null if called from the static context (no test case is being executed at the moment).