Class GridTestUtils

java.lang.Object
org.apache.ignite.testframework.GridTestUtils

public final class GridTestUtils extends Object
Utility class for tests.
  • Field Details

    • DFLT_BUSYWAIT_SLEEP_INTERVAL

      public static final long DFLT_BUSYWAIT_SLEEP_INTERVAL
      Default busy wait sleep interval in milliseconds.
      See Also:
    • DFLT_TEST_TIMEOUT

      public static final long DFLT_TEST_TIMEOUT
      See Also:
  • Method Details

    • assertNotContains

      public static void assertNotContains(IgniteLogger log, String str, String substr)
      Checks that string doesn't contains substring . Logs both strings and throws AssertionError, if contains.
      Parameters:
      log - Logger (optional).
      str - String.
      substr - Substring.
    • assertContains

      public static void assertContains(IgniteLogger log, String str, String substr)
      Checks that string contains substring . Logs both strings and throws AssertionError, if not.
      Parameters:
      log - Logger (optional).
      str - String.
      substr - Substring.
    • assertContains

      public static <C extends Collection<T>, T> void assertContains(IgniteLogger log, C col, T elem)
      Checks that collection contains element . Logs collection, element and throws AssertionError, if not.
      Parameters:
      log - Logger (optional).
      col - Collection.
      elem - Element.
    • assertNotContains

      public static <C extends Collection<T>, T> void assertNotContains(IgniteLogger log, C col, T elem)
      Checks that collection doesn't contains element . Logs collection, element and throws AssertionError, 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

      public static Throwable assertThrowsWithCause(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.
      Parameters:
      runnable - Runnable.
      cls - Expected class.
      Returns:
      Thrown throwable.
    • assertThrowsWithCause

      public static Throwable assertThrowsWithCause(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.
      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 - Timeout TimeUnit.
      runnable - Runnable to check.
      Throws:
      Exception - In case of any exception distinct from TimeoutException.
    • 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 - Timeout TimeUnit.
      runnable - Runnable to check.
      Throws:
      Exception - In case of any exception distinct from TimeoutException.
    • 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

      public static <T> void assertOneToOne(Iterable<T> it, IgnitePredicate<T>... ps)
      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

      public static org.apache.ignite.internal.IgniteInternalFuture runAsync(Runnable task)
      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

      public static <T> org.apache.ignite.internal.IgniteInternalFuture<T> runAsync(Callable<T> task)
      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

      public static void stopThreads(IgniteLogger log)
      Interrupts and waits for termination of all the threads started so far by current test.
      Parameters:
      log - Logger.
    • getIgniteHome

      public static String getIgniteHome() throws Exception
      Returns:
      Ignite home.
      Throws:
      Exception - If failed.
    • getAnnotation

      public static <T extends Annotation> T getAnnotation(Class<?> cls, Class<T> annCls)
      Type Parameters:
      T - Type.
      Parameters:
      cls - Class.
      annCls - Annotation class.
      Returns:
      Annotation.
    • makeCallable

      public static <T> Callable<T> makeCallable(Runnable run, T res)
      Convert runnable tasks with callable.
      Type Parameters:
      T - The result type of method call, always null.
      Parameters:
      run - Runnable task to convert into callable one.
      res - Callable result.
      Returns:
      Callable task around the specified runnable one.
    • getFieldValue

      public static <T> T getFieldValue(Object obj, Class cls, String fieldName) throws IgniteException
      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

      public static <T> T getFieldValue(Object obj, String... fieldNames) throws IgniteException
      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

      public static <T> T getFieldValueHierarchy(Object obj, String... fieldNames) throws IgniteException
      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

      public static <T> Class<T> getInnerClass(Class<?> parentCls, String innerClsName)
      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

      public static void setFieldValue(Object obj, String fieldName, Object val) throws IgniteException
      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

      public static <T> T invoke(Object obj, String mtd, Object... params) throws Exception
      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 case assertion closure will be executed retries + 1 times and thread will spend approximately retries * retryInterval sleeping.
      Parameters:
      log - Log.
      retries - Number of retries.
      retryInterval - Interval between retries in milliseconds.
      c - Closure with assertion. All AssertionErrors thrown from this closure will be ignored retries times.
      Throws:
      org.apache.ignite.internal.IgniteInterruptedCheckedException - If interrupted.
    • readFile

      public static byte[] readFile(File file) throws IOException
      Reads entire file into byte array.
      Parameters:
      file - File to read.
      Returns:
      Content of file in byte array.
      Throws:
      IOException - If failed.
    • readResource

      public static byte[] readResource(ClassLoader classLoader, String resourceName) throws IOException
      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 (int i = 0; i < 20 && !condition; i = sleepAndIncrement(200, i)) {
               ...
           }
       
      for busy-waiting limited number of iterations.
      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:
      true if condition was achieved, false otherwise.
      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:
      true if condition was achieved, false otherwise.
      Throws:
      org.apache.ignite.internal.IgniteInterruptedCheckedException - If interrupted.
    • deepEquals

      public static boolean deepEquals(Object o1, Object o2)
      Parameters:
      o1 - Object 1.
      o2 - Object 2.
      Returns:
      Equals or not.
    • modeToPermissionSet

      public static Set<PosixFilePermission> modeToPermissionSet(int mode)
      Converts integer permission mode into set of PosixFilePermission.
      Parameters:
      mode - File mode.
      Returns:
      Set of PosixFilePermission.
    • benchmark

      public static void benchmark(String name, Runnable run)
      Parameters:
      name - Name.
      run - Run.
    • benchmark

      public static void benchmark(String name, long warmup, long executionTime, Runnable run)
      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

      public static String fullSimpleName(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.
      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 in ignoredTests set.
      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

      public static String randomString(Random rnd, int maxLen)
      Generate random alphabetical string.
      Parameters:
      rnd - Random object.
      maxLen - Maximal length of string.
      Returns:
      Random string object.
    • randomString

      public static String randomString(Random rnd, int minLen, int maxLen)
      Generate random alphabetical string.
      Parameters:
      rnd - Random object.
      minLen - Minimum length of string.
      maxLen - Maximal length of string.
      Returns:
      Random string object.
    • suppressException

      public static void suppressException(GridTestUtils.RunnableX runnableX)
      Parameters:
      runnableX - Runnable with exception.