Package org.apache.ignite.testframework
Class GridTestUtils
java.lang.Object
org.apache.ignite.testframework.GridTestUtils
Utility class for tests.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRunnable that can throw exceptions.static final classTest parameters scale factor util. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault busy wait sleep interval in milliseconds.static final long -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddTestIfNeeded(List<Class<?>> suite, Class<?> test, Collection<Class> ignoredTests) Adds test class to the list only if it's not inignoredTestsset.static <C extends Collection<T>,T>
voidassertContains(IgniteLogger log, C col, T elem) Checks that collection contains element .static voidassertContains(IgniteLogger log, String str, String substr) Checks that string contains substring .static <C extends Collection<T>,T>
voidassertNotContains(IgniteLogger log, C col, T elem) Checks that collection doesn't contains element .static voidassertNotContains(IgniteLogger log, String str, String substr) Checks that string doesn't contains substring .static <T> voidassertOneToOne(Iterable<T> it, IgnitePredicate<T>... ps) Asserts that each element in iterable has one-to-one correspondence with a predicate from list.static ThrowableassertThrows(IgniteLogger log, Class<? extends Throwable> cls, String msg, Object obj, String mtd, Object... params) Checks whether object's method call throws expected exception or not.static ThrowableassertThrows(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws expected exception or not.static ThrowableassertThrows(IgniteLogger log, GridTestUtils.RunnableX run, Class<? extends Throwable> cls, String msg) Checks whether runnable throws expected exception or not.static voidassertThrowsAnyCause(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws an exception with specified cause.static ThrowableassertThrowsInherited(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws expected exception or its child or not.static ThrowableassertThrowsWithCause(Runnable runnable, Class<? extends Throwable> cls) Checks whether callable throws exception, which is itself of a specified class, or has a cause of the specified class.static ThrowableassertThrowsWithCause(Callable<?> call, Class<? extends Throwable> cls) Checks whether callable throws exception, which is itself of a specified class, or has a cause of the specified class.static <P> ThrowableassertThrowsWithCause(IgniteInClosure<P> call, P p, Class<? extends Throwable> cls) Checks whether closure throws exception, which is itself of a specified class, or has a cause of the specified class.static voidassertTimeout(long timeout, TimeUnit timeUnit, Runnable runnable) Asserts that the specified runnable completes within the specified timeout.static voidassertTimeout(String msg, long timeout, TimeUnit timeUnit, Runnable runnable) Asserts that the specified runnable completes within the specified timeout.static voidstatic voidstatic booleandeepEquals(Object o1, Object o2) static StringfullSimpleName(Class cls) Class.getSimpleName()does not return outer class name prefix for inner classes, for example, getSimpleName() returns "RegularDiscovery" instead of "GridDiscoveryManagerSelfTest$RegularDiscovery" This method return correct simple name for inner classes.static <T extends Annotation>
TgetAnnotation(Class<?> cls, Class<T> annCls) static <T> TgetFieldValue(Object obj, Class cls, String fieldName) Get object field value via reflection.static <T> TgetFieldValue(Object obj, String... fieldNames) Get object field value via reflection.static <T> TgetFieldValueHierarchy(Object obj, String... fieldNames) Get object field value via reflection(including superclass).static Stringstatic <T> Class<T>getInnerClass(Class<?> parentCls, String innerClsName) Get inner class by its name from the enclosing class.static <T> TInvoke method on an object.static <T> Callable<T>makeCallable(Runnable run, T res) Convert runnable tasks with callable.static Set<PosixFilePermission>modeToPermissionSet(int mode) Converts integer permission mode into set ofPosixFilePermission.static StringrandomString(Random rnd, int maxLen) Generate random alphabetical string.static StringrandomString(Random rnd, int minLen, int maxLen) Generate random alphabetical string.static byte[]Reads entire file into byte array.static byte[]readResource(ClassLoader classLoader, String resourceName) Reads resource into byte array.static voidretryAssert(IgniteLogger log, int retries, long retryInterval, GridAbsClosure c) Tries few times to perform some assertion.static org.apache.ignite.internal.IgniteInternalFutureRuns runnable task asyncronously.static org.apache.ignite.internal.IgniteInternalFutureRuns runnable task asyncronously.static <T> org.apache.ignite.internal.IgniteInternalFuture<T>Runs callable task asyncronously.static <T> org.apache.ignite.internal.IgniteInternalFuture<T>Runs callable task asyncronously.static voidrunGC()Prompt to execute garbage collector.static longrunMultiThreaded(Iterable<Callable<?>> calls, String threadName) Runs callable tasks each in separate threads.static longrunMultiThreaded(Iterable<Callable<?>> calls, GridTestSafeThreadFactory threadFactory) Runs callable tasks each in separate threads.static longrunMultiThreaded(Runnable run, int threadNum, String threadName) Runs runnable object in specified number of threads.static longrunMultiThreaded(Callable<?> call, int threadNum, String threadName) Runs callable object in specified number of threads.static longrunMultiThreaded(IgniteInClosure<Integer> call, int threadNum, String threadName) static org.apache.ignite.internal.IgniteInternalFuture<Long>runMultiThreadedAsync(Runnable run, int threadNum, String threadName) Runs runnable object in specified number of threads.static org.apache.ignite.internal.IgniteInternalFuture<Long>runMultiThreadedAsync(Callable<?> call, int threadNum, String threadName) Runs callable object in specified number of threads.static voidsetFieldValue(Object obj, Class cls, String fieldName, Object val) Set object field value via reflection.static voidsetFieldValue(Object obj, String fieldName, Object val) Set object field value via reflection.static intsleepAndIncrement(int sleepDur, int i) Sleeps and increments an integer.static voidstopThreads(IgniteLogger log) Interrupts and waits for termination of all the threads started so far by current test.static voidsuppressException(GridTestUtils.RunnableX runnableX) static voidwaitForAllFutures(org.apache.ignite.internal.IgniteInternalFuture<?>... futs) Wait for all passed futures to complete even if they fail.static booleanwaitForCondition(GridAbsPredicate cond, long timeout) Waits for condition, polling in busy wait loop.static booleanwaitForCondition(GridAbsPredicate cond, BooleanSupplier wait)
-
Field Details
-
DFLT_BUSYWAIT_SLEEP_INTERVAL
public static final long DFLT_BUSYWAIT_SLEEP_INTERVALDefault busy wait sleep interval in milliseconds.- See Also:
-
DFLT_TEST_TIMEOUT
public static final long DFLT_TEST_TIMEOUT- See Also:
-
-
Method Details
-
assertNotContains
Checks that string doesn't contains substring . Logs both strings and throwsAssertionError, if contains.- Parameters:
log- Logger (optional).str- String.substr- Substring.
-
assertContains
Checks that string contains substring . Logs both strings and throwsAssertionError, if not.- Parameters:
log- Logger (optional).str- String.substr- Substring.
-
assertContains
Checks that collection contains element . Logs collection, element and throwsAssertionError, if not.- Parameters:
log- Logger (optional).col- Collection.elem- Element.
-
assertNotContains
Checks that collection doesn't contains element . Logs collection, element and throwsAssertionError, if contains.- Parameters:
log- Logger (optional).col- Collection.elem- Element.
-
assertThrows
public static Throwable assertThrows(IgniteLogger log, GridTestUtils.RunnableX run, Class<? extends Throwable> cls, String msg) Checks whether runnable throws expected exception or not.- Parameters:
log- Logger (optional).run- Runnable.cls- Exception class.msg- Exception message (optional). If provided exception message and this message should be equal.- Returns:
- Thrown throwable.
-
assertThrows
public static Throwable assertThrows(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws expected exception or not.- Parameters:
log- Logger (optional).call- Callable.cls- Exception class.msg- Exception message (optional). If provided exception message and this message should be equal.- Returns:
- Thrown throwable.
-
assertThrowsAnyCause
public static void assertThrowsAnyCause(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws an exception with specified cause.- Parameters:
log- Logger (optional).call- Callable.cls- Exception class.msg- Exception message (optional). If provided exception message and this message should be equal.
-
assertThrowsInherited
public static Throwable assertThrowsInherited(IgniteLogger log, Callable<?> call, Class<? extends Throwable> cls, String msg) Checks whether callable throws expected exception or its child or not.- Parameters:
log- Logger (optional).call- Callable.cls- Exception class.msg- Exception message (optional). If provided exception message and this message should be equal.- Returns:
- Thrown throwable.
-
assertThrowsWithCause
Checks whether callable throws exception, which is itself of a specified class, or has a cause of the specified class.- Parameters:
runnable- Runnable.cls- Expected class.- Returns:
- Thrown throwable.
-
assertThrowsWithCause
Checks whether callable throws exception, which is itself of a specified class, or has a cause of the specified class.- Parameters:
call- Callable.cls- Expected class.- Returns:
- Thrown throwable.
-
assertThrowsWithCause
public static <P> Throwable assertThrowsWithCause(IgniteInClosure<P> call, P p, Class<? extends Throwable> cls) Checks whether closure throws exception, which is itself of a specified class, or has a cause of the specified class.- Parameters:
call- Closure.p- Parameter passed to closure.cls- Expected class.- Returns:
- Thrown throwable.
-
assertTimeout
public static void assertTimeout(String msg, long timeout, TimeUnit timeUnit, Runnable runnable) throws Exception Asserts that the specified runnable completes within the specified timeout.- Parameters:
msg- Assertion message in case of timeout.timeout- Timeout.timeUnit- TimeoutTimeUnit.runnable-Runnableto check.- Throws:
Exception- In case of any exception distinct fromTimeoutException.
-
assertTimeout
public static void assertTimeout(long timeout, TimeUnit timeUnit, Runnable runnable) throws Exception Asserts that the specified runnable completes within the specified timeout.- Parameters:
timeout- Timeout.timeUnit- TimeoutTimeUnit.runnable-Runnableto check.- Throws:
Exception- In case of any exception distinct fromTimeoutException.
-
assertThrows
public static Throwable assertThrows(IgniteLogger log, Class<? extends Throwable> cls, String msg, Object obj, String mtd, Object... params) Checks whether object's method call throws expected exception or not.- Parameters:
log- Logger (optional).cls- Exception class.msg- Exception message (optional). If provided exception message and this message should be equal.obj- Object to invoke method for.mtd- Object's method to invoke.params- Method parameters.- Returns:
- Thrown throwable.
-
assertOneToOne
Asserts that each element in iterable has one-to-one correspondence with a predicate from list.- Parameters:
it- Input iterable of elements.ps- Array of predicates (by number of elements in iterable).
-
runMultiThreaded
public static long runMultiThreaded(Runnable run, int threadNum, String threadName) throws Exception Runs runnable object in specified number of threads.- Parameters:
run- Target runnable.threadNum- Number of threads.threadName- Thread name.- Returns:
- Execution time in milliseconds.
- Throws:
Exception- Thrown if at least one runnable execution failed.
-
runMultiThreadedAsync
public static org.apache.ignite.internal.IgniteInternalFuture<Long> runMultiThreadedAsync(Runnable run, int threadNum, String threadName) Runs runnable object in specified number of threads.- Parameters:
run- Target runnable.threadNum- Number of threads.threadName- Thread name.- Returns:
- Future for the run. Future returns execution time in milliseconds.
-
runMultiThreaded
public static long runMultiThreaded(Callable<?> call, int threadNum, String threadName) throws Exception Runs callable object in specified number of threads.- Parameters:
call- Callable.threadNum- Number of threads.threadName- Thread names.- Returns:
- Execution time in milliseconds.
- Throws:
Exception- If failed.
-
runMultiThreaded
public static long runMultiThreaded(IgniteInClosure<Integer> call, int threadNum, String threadName) throws Exception - Parameters:
call- Closure that receives thread index.threadNum- Number of threads.threadName- Thread names.- Returns:
- Execution time in milliseconds.
- Throws:
Exception- If failed.
-
runMultiThreadedAsync
public static org.apache.ignite.internal.IgniteInternalFuture<Long> runMultiThreadedAsync(Callable<?> call, int threadNum, String threadName) Runs callable object in specified number of threads.- Parameters:
call- Callable.threadNum- Number of threads.threadName- Thread names.- Returns:
- Future for the run. Future returns execution time in milliseconds.
-
runMultiThreaded
public static long runMultiThreaded(Iterable<Callable<?>> calls, String threadName) throws Exception Runs callable tasks each in separate threads.- Parameters:
calls- Callable tasks.threadName- Thread name.- Returns:
- Execution time in milliseconds.
- Throws:
Exception- If failed.
-
runMultiThreaded
public static long runMultiThreaded(Iterable<Callable<?>> calls, GridTestSafeThreadFactory threadFactory) throws Exception Runs callable tasks each in separate threads.- Parameters:
calls- Callable tasks.threadFactory- Thread factory.- Returns:
- Execution time in milliseconds.
- Throws:
Exception- If failed.
-
runAsync
Runs runnable task asyncronously.- Parameters:
task- Runnable.- Returns:
- Future with task result.
-
runAsync
public static org.apache.ignite.internal.IgniteInternalFuture runAsync(Runnable task, String threadName) Runs runnable task asyncronously.- Parameters:
task- Runnable.- Returns:
- Future with task result.
-
runAsync
Runs callable task asyncronously.- Parameters:
task- Callable.- Returns:
- Future with task result.
-
runAsync
public static <T> org.apache.ignite.internal.IgniteInternalFuture<T> runAsync(Callable<T> task, String threadName) Runs callable task asyncronously.- Parameters:
task- Callable.threadName- Thread name.- Returns:
- Future with task result.
-
waitForAllFutures
public static void waitForAllFutures(org.apache.ignite.internal.IgniteInternalFuture<?>... futs) Wait for all passed futures to complete even if they fail.- Parameters:
futs- Futures.- Throws:
AssertionError- Suppresses underlying exceptions if some futures failed.
-
stopThreads
Interrupts and waits for termination of all the threads started so far by current test.- Parameters:
log- Logger.
-
getIgniteHome
- Returns:
- Ignite home.
- Throws:
Exception- If failed.
-
getAnnotation
- Type Parameters:
T- Type.- Parameters:
cls- Class.annCls- Annotation class.- Returns:
- Annotation.
-
makeCallable
Convert runnable tasks with callable.- Type Parameters:
T- The result type of method call, alwaysnull.- Parameters:
run- Runnable task to convert into callable one.res- Callable result.- Returns:
- Callable task around the specified runnable one.
-
getFieldValue
Get object field value via reflection.- Type Parameters:
T- Expected field class.- Parameters:
obj- Object or class to get field value from.cls- Class.fieldName- Field names to get value for.- Returns:
- Field value.
- Throws:
IgniteException- In case of error.
-
getFieldValue
Get object field value via reflection.- Type Parameters:
T- Expected field class.- Parameters:
obj- Object or class to get field value from.fieldNames- Field names to get value for: obj->field1->field2->...->fieldN.- Returns:
- Field value.
- Throws:
IgniteException- In case of error.
-
getFieldValueHierarchy
Get object field value via reflection(including superclass).- Type Parameters:
T- Expected field class.- Parameters:
obj- Object or class to get field value from.fieldNames- Field names to get value for: obj->field1->field2->...->fieldN.- Returns:
- Field value.
- Throws:
IgniteException- In case of error.
-
getInnerClass
Get inner class by its name from the enclosing class.- Parameters:
parentCls- Parent class to resolve inner class for.innerClsName- Name of the inner class.- Returns:
- Inner class.
-
setFieldValue
Set object field value via reflection.- Parameters:
obj- Object to set field value to.fieldName- Field name to set value for.val- New field value.- Throws:
IgniteException- In case of error.
-
setFieldValue
public static void setFieldValue(Object obj, Class cls, String fieldName, Object val) throws IgniteException Set object field value via reflection.- Parameters:
obj- Object to set field value to.cls- Class to get field from.fieldName- Field name to set value for.val- New field value.- Throws:
IgniteException- In case of error.
-
invoke
Invoke method on an object.- Parameters:
obj- Object to call method on.mtd- Method to invoke.params- Parameters of the method.- Returns:
- Method invocation result.
- Throws:
Exception- If failed.
-
retryAssert
public static void retryAssert(IgniteLogger log, int retries, long retryInterval, GridAbsClosure c) throws org.apache.ignite.internal.IgniteInterruptedCheckedException Tries few times to perform some assertion. In the worst caseassertionclosure will be executedretries+ 1 times and thread will spend approximatelyretries*retryIntervalsleeping.- Parameters:
log- Log.retries- Number of retries.retryInterval- Interval between retries in milliseconds.c- Closure with assertion. AllAssertionErrors thrown from this closure will be ignoredretriestimes.- Throws:
org.apache.ignite.internal.IgniteInterruptedCheckedException- If interrupted.
-
readFile
Reads entire file into byte array.- Parameters:
file- File to read.- Returns:
- Content of file in byte array.
- Throws:
IOException- If failed.
-
readResource
Reads resource into byte array.- Parameters:
classLoader- Classloader.resourceName- Resource name.- Returns:
- Content of resorce in byte array.
- Throws:
IOException- If failed.
-
sleepAndIncrement
public static int sleepAndIncrement(int sleepDur, int i) throws org.apache.ignite.internal.IgniteInterruptedCheckedException Sleeps and increments an integer.Allows for loops like the following:
for busy-waiting limited number of iterations.for (int i = 0; i < 20 && !condition; i = sleepAndIncrement(200, i)) { ... }- Parameters:
sleepDur- Sleep duration in milliseconds.i- Integer to increment.- Returns:
- Incremented value.
- Throws:
org.apache.ignite.internal.IgniteInterruptedCheckedException- If sleep was interrupted.
-
waitForCondition
public static boolean waitForCondition(GridAbsPredicate cond, long timeout) throws org.apache.ignite.internal.IgniteInterruptedCheckedException Waits for condition, polling in busy wait loop.- Parameters:
cond- Condition to wait for.timeout- Max time to wait in milliseconds.- Returns:
trueif condition was achieved,falseotherwise.- Throws:
org.apache.ignite.internal.IgniteInterruptedCheckedException- If interrupted.
-
waitForCondition
public static boolean waitForCondition(GridAbsPredicate cond, BooleanSupplier wait) throws org.apache.ignite.internal.IgniteInterruptedCheckedException - Parameters:
cond- Condition to wait for.wait- Wait predicate.- Returns:
trueif condition was achieved,falseotherwise.- Throws:
org.apache.ignite.internal.IgniteInterruptedCheckedException- If interrupted.
-
deepEquals
- Parameters:
o1- Object 1.o2- Object 2.- Returns:
- Equals or not.
-
modeToPermissionSet
Converts integer permission mode into set ofPosixFilePermission.- Parameters:
mode- File mode.- Returns:
- Set of
PosixFilePermission.
-
benchmark
- Parameters:
name- Name.run- Run.
-
benchmark
- Parameters:
name- Name.warmup- Warmup.executionTime- Time.run- Run.
-
runGC
public static void runGC()Prompt to execute garbage collector.System.gc();is not guaranteed to garbage collection, this method try to fill memory to crowd out dead objects. -
fullSimpleName
Class.getSimpleName()does not return outer class name prefix for inner classes, for example, getSimpleName() returns "RegularDiscovery" instead of "GridDiscoveryManagerSelfTest$RegularDiscovery" This method return correct simple name for inner classes.- Parameters:
cls- Class- Returns:
- Simple name with outer class prefix.
-
addTestIfNeeded
public static void addTestIfNeeded(List<Class<?>> suite, Class<?> test, Collection<Class> ignoredTests) Adds test class to the list only if it's not inignoredTestsset.- Parameters:
suite- List where to place the test class.test- Test.ignoredTests- Tests to ignore. If test contained in the collection it is not included in suite
-
randomString
Generate random alphabetical string.- Parameters:
rnd- Random object.maxLen- Maximal length of string.- Returns:
- Random string object.
-
randomString
Generate random alphabetical string.- Parameters:
rnd- Random object.minLen- Minimum length of string.maxLen- Maximal length of string.- Returns:
- Random string object.
-
suppressException
- Parameters:
runnableX- Runnable with exception.
-