Class RandomizedContext
java.lang.Object
com.carrotsearch.randomizedtesting.RandomizedContext
Context variables for an execution of a test suite (hooks and tests) running
under a
RandomizedRunner.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classPer thread assigned resources. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ObjectCoordination at context level.private static final ObjectCoordination at global level.(package private) static final IdentityHashMap<ThreadGroup, RandomizedContext> All thread groups we're currently tracking contexts for.private Methodprivate EnumMap<LifecycleScope, List<CloseableResourceInfo>> Disposable resources.private booleanThe context and all of its resources are no longer usable.(package private) final WeakHashMap<Thread, RandomizedContext.PerThreadResources> Per thread resources for each context.private final RandomizedRunnerThe runner to which we're bound.private final Class<?> private final ThreadGroupA thread group that shares this context. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateRandomizedContext(ThreadGroup tg, Class<?> suiteClass, RandomizedRunner runner) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidThrow an exception if disposed.(package private) static voidClone context information between the current thread and another thread.<T extends 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 RandomizedContext(package private) static RandomizedContextcreate(ThreadGroup tg, Class<?> suiteClass, RandomizedRunner runner) Create a new context bound to a thread group.static RandomizedContextcurrent()(package private) voiddispose()Dispose of the context.Provide access toGroupEvaluator.Return per-thread resources associated with the current thread.A shorthand for callinggetRandomness()and thenRandomness.getRandom().Source of randomness for the context's thread.(package private) Randomness[]Return allRandomnesson the stack for the current thread.Access to the runner governing this context.(package private) longRunner's seed.Returns the runner's master seed, formatted.Class<?> The class (suite) being tested.booleanReturntrueifNightlytest group is explicitly enabled.(package private) voidPop 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, 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, 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(Method method)
-
Field Details
-
_globalLock
Coordination at global level. -
_contextLock
Coordination at context level. -
contexts
All thread groups we're currently tracking contexts for. -
perThreadResources
Per thread resources for each context. Allow GCing of threads. -
threadGroup
A thread group that shares this context. -
suiteClass
- See Also:
-
runner
The runner to which we're bound. -
disposed
private volatile boolean disposedThe context and all of its resources are no longer usable. -
disposableResources
Disposable resources. -
currentMethod
-
-
Constructor Details
-
RandomizedContext
-
-
Method Details
-
getTargetClass
The class (suite) being tested. -
getRunnerSeed
long getRunnerSeed()Runner's seed. -
getRunnerSeedAsString
Returns the runner's master seed, formatted. -
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
A shorthand for callinggetRandomness()and thenRandomness.getRandom(). -
isNightly
-
current
- Returns:
- Returns the context for the calling thread or throws an
IllegalStateExceptionif the thread is out of scope. - Throws:
IllegalStateException- If context is not available.
-
getRunner
Access to the runner governing this context. -
closeAtEnd
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
Provide access toGroupEvaluator. -
runWithPrivateRandomness
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:
Exception- ifCallable.call()throws an exception
-
runWithPrivateRandomness
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:
seed- The initial seed for the new, private randomnesscallable- the callable to execute- Returns:
- the result of the call to
Callable.call() - Throws:
Exception- ifCallable.call()throws an exception
-
closeResources
Dispose of any resources registered in the given scope. -
context
-
create
Create a new context bound to a thread group. -
dispose
void dispose()Dispose of the context. -
push
Push a new randomness on top of the stack. -
popAndDestroy
void popAndDestroy()Pop a randomness off the stack and dispose it. -
getPerThread
Return per-thread resources associated with the current thread. -
checkDisposed
private void checkDisposed()Throw an exception if disposed. -
cloneFor
Clone context information between the current thread and another thread. This is for internal use only to propagate context information when forking. -
setTargetMethod
-
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).
-