Class TestUtils

java.lang.Object
org.apache.kafka.common.test.TestUtils

public class TestUtils extends Object
Helper functions for writing unit tests
  • Field Details

  • Constructor Details

    • TestUtils

      public TestUtils()
  • Method Details

    • tempFile

      public static File tempFile() throws IOException
      Create an empty file in the default temporary-file directory, using `kafka` as the prefix and `tmp` as the suffix to generate its name.
      Throws:
      IOException
    • randomString

      public static String randomString(int len)
      Generate a random string of letters and digits of the given length
      Parameters:
      len - The length of the string
      Returns:
      The random string
    • waitForCondition

      public static void waitForCondition(Supplier<Boolean> testCondition, String conditionDetails) throws InterruptedException
      uses default value of 15 seconds for timeout
      Throws:
      InterruptedException
    • waitForCondition

      public static void waitForCondition(Supplier<Boolean> testCondition, long maxWaitMs, String conditionDetails) throws InterruptedException
      Wait for condition to be met for at most maxWaitMs and throw assertion failure otherwise. This should be used instead of Thread.sleep whenever possible as it allows a longer timeout to be used without unnecessarily increasing test time (as the condition is checked frequently). The longer timeout is needed to avoid transient failures due to slow or overloaded machines.
      Throws:
      InterruptedException
    • waitUntilLeaderIsElectedOrChangedWithAdmin

      public static int waitUntilLeaderIsElectedOrChangedWithAdmin(org.apache.kafka.clients.admin.Admin admin, String topic, int partitionNumber, long timeoutMs) throws Exception
      Throws:
      Exception