Class TestUtils

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

public class TestUtils extends Object
Helper functions for writing unit tests.

This class is public because test-common-runtime is packaged as a main source set library JAR — not a test source set — so that other modules (e.g. k2-core-integration, k2proxy/core) can depend on it for shared test infrastructure like JUnit extensions. Classes in other packages within this module (e.g. org.apache.kafka.common.test.junit.ClusterTestExtensions) need access to utilities like waitForCondition(Supplier, String), and Java package-private visibility does not extend to other packages regardless of shared naming prefix. The similar org.apache.kafka.test.TestUtils in clients/src/test/ cannot be used here because main source sets cannot depend on test source sets.

  • 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, Supplier<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
    • 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