Class RandomizedContext
- java.lang.Object
-
- com.carrotsearch.randomizedtesting.RandomizedContext
-
public final class RandomizedContext extends java.lang.ObjectContext variables for an execution of a test suite (hooks and tests) running under aRandomizedRunner.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classRandomizedContext.PerThreadResourcesPer thread assigned resources.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object_contextLockCoordination at context level.private static java.lang.Object_globalLockCoordination at global level.(package private) static java.util.IdentityHashMap<java.lang.ThreadGroup,RandomizedContext>contextsAll thread groups we're currently tracking contexts for.private java.lang.reflect.MethodcurrentMethodprivate java.util.EnumMap<LifecycleScope,java.util.List<CloseableResourceInfo>>disposableResourcesDisposable resources.private booleandisposedThe context and all of its resources are no longer usable.(package private) java.util.WeakHashMap<java.lang.Thread,RandomizedContext.PerThreadResources>perThreadResourcesPer thread resources for each context.private RandomizedRunnerrunnerThe runner to which we're bound.private java.lang.Class<?>suiteClassprivate java.lang.ThreadGroupthreadGroupA thread group that shares this context.
-
Constructor Summary
Constructors Modifier Constructor Description privateRandomizedContext(java.lang.ThreadGroup tg, java.lang.Class<?> suiteClass, RandomizedRunner runner)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckDisposed()Throw an exception if disposed.(package private) static voidcloneFor(java.lang.Thread t)Clone context information between the current thread and another thread.<T extends java.io.Closeable>
TcloseAtEnd(T resource, LifecycleScope scope)Dispose the given resource at the end of a given lifecycle scope.(package private) voidcloseResources(ObjectProcedure<CloseableResourceInfo> consumer, LifecycleScope scope)Dispose of any resources registered in the given scope.(package private) static RandomizedContextcontext(java.lang.Thread thread)(package private) static RandomizedContextcreate(java.lang.ThreadGroup tg, java.lang.Class<?> suiteClass, RandomizedRunner runner)Create a new context bound to a thread group.static RandomizedContextcurrent()(package private) voiddispose()Dispose of the context.GroupEvaluatorgetGroupEvaluator()Provide access toGroupEvaluator.private RandomizedContext.PerThreadResourcesgetPerThread()Return per-thread resources associated with the current thread.java.util.RandomgetRandom()A shorthand for callinggetRandomness()and thenRandomness.getRandom().RandomnessgetRandomness()Source of randomness for the context's thread.(package private) Randomness[]getRandomnesses()Return allRandomnesson the stack for the current thread.RandomizedRunnergetRunner()Access to the runner governing this context.(package private) longgetRunnerSeed()Runner's seed.java.lang.StringgetRunnerSeedAsString()Returns the runner's master seed, formatted.java.lang.Class<?>getTargetClass()The class (suite) being tested.java.lang.reflect.MethodgetTargetMethod()booleanisNightly()ReturntrueifNightlytest group is explicitly enabled.(package private) voidpopAndDestroy()Pop a randomness off the stack and dispose it.(package private) voidpush(Randomness rnd)Push a new randomness on top of the stack.<T> TrunWithPrivateRandomness(long seed, java.util.concurrent.Callable<T> callable)Pushes the given randomness to the top of the stack, runs theCallableand disposes the randomness before the this method returns.<T> TrunWithPrivateRandomness(Randomness randomness, java.util.concurrent.Callable<T> callable)Pushes the given randomness to the top of the stack, runs theCallableand disposes the randomness before the this method returns.(package private) voidsetTargetMethod(java.lang.reflect.Method method)
-
-
-
Field Detail
-
_globalLock
private static final java.lang.Object _globalLock
Coordination at global level.
-
_contextLock
private final java.lang.Object _contextLock
Coordination at context level.
-
contexts
static final java.util.IdentityHashMap<java.lang.ThreadGroup,RandomizedContext> contexts
All thread groups we're currently tracking contexts for.
-
perThreadResources
final java.util.WeakHashMap<java.lang.Thread,RandomizedContext.PerThreadResources> perThreadResources
Per thread resources for each context. Allow GCing of threads.
-
threadGroup
private final java.lang.ThreadGroup threadGroup
A thread group that shares this context.
-
suiteClass
private final java.lang.Class<?> suiteClass
- See Also:
getTargetClass()
-
runner
private final RandomizedRunner runner
The runner to which we're bound.
-
disposed
private volatile boolean disposed
The context and all of its resources are no longer usable.
-
disposableResources
private java.util.EnumMap<LifecycleScope,java.util.List<CloseableResourceInfo>> disposableResources
Disposable resources.
-
currentMethod
private java.lang.reflect.Method currentMethod
-
-
Constructor Detail
-
RandomizedContext
private RandomizedContext(java.lang.ThreadGroup tg, java.lang.Class<?> suiteClass, RandomizedRunner runner)
-
-
Method Detail
-
getTargetClass
public java.lang.Class<?> getTargetClass()
The class (suite) being tested.
-
getRunnerSeed
long getRunnerSeed()
Runner's seed.
-
getRunnerSeedAsString
public java.lang.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 allRandomnesson the stack for the current thread. The most recent (currently used) randomness comes last in this array.
-
getRandom
public java.util.Random getRandom()
A shorthand for callinggetRandomness()and thenRandomness.getRandom().
-
isNightly
public boolean isNightly()
ReturntrueifNightlytest group is explicitly enabled.
-
current
public static RandomizedContext current()
- Returns:
- Returns the context for the calling thread or throws an
IllegalStateExceptionif the thread is out of scope. - Throws:
java.lang.IllegalStateException- If context is not available.
-
getRunner
public RandomizedRunner getRunner()
Access to the runner governing this context.
-
closeAtEnd
public <T extends java.io.Closeable> T closeAtEnd(T resource, LifecycleScope scope)Dispose the given resource at the end of a given lifecycle scope. If theCloseablethrows an exception, the test case or suite will end in a failure.- Returns:
resource(for call chaining).
-
getGroupEvaluator
public GroupEvaluator getGroupEvaluator()
Provide access toGroupEvaluator.
-
runWithPrivateRandomness
public <T> T runWithPrivateRandomness(Randomness randomness, java.util.concurrent.Callable<T> callable) throws java.lang.Exception
Pushes the given randomness to the top of the stack, runs theCallableand 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 stackcallable- the callable to execute- Returns:
- the result of the call to
Callable.call() - Throws:
java.lang.Exception- ifCallable.call()throws an exception
-
runWithPrivateRandomness
public <T> T runWithPrivateRandomness(long seed, java.util.concurrent.Callable<T> callable) throws java.lang.ExceptionPushes the given randomness to the top of the stack, runs theCallableand 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 randomnesscallable- the callable to execute- Returns:
- the result of the call to
Callable.call() - Throws:
java.lang.Exception- ifCallable.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(java.lang.Thread thread)
-
create
static RandomizedContext create(java.lang.ThreadGroup tg, java.lang.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(java.lang.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(java.lang.reflect.Method method)
-
getTargetMethod
public java.lang.reflect.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
nullif called from the static context (no test case is being executed at the moment).
-
-