Package org.apache.kafka.common.test
Class TestUtils
java.lang.Object
org.apache.kafka.common.test.TestUtils
Helper functions for writing unit tests
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringrandomString(int len) Generate a random string of letters and digits of the given lengthstatic FiletempFile()Create an empty file in the default temporary-file directory, using `kafka` as the prefix and `tmp` as the suffix to generate its name.static voidwaitForCondition(Supplier<Boolean> testCondition, long maxWaitMs, String conditionDetails) Wait for condition to be met for at mostmaxWaitMsand throw assertion failure otherwise.static voidwaitForCondition(Supplier<Boolean> testCondition, String conditionDetails) uses default value of 15 seconds for timeoutstatic intwaitUntilLeaderIsElectedOrChangedWithAdmin(org.apache.kafka.clients.admin.Admin admin, String topic, int partitionNumber, long timeoutMs)
-
Field Details
-
SEEDED_RANDOM
-
LETTERS
- See Also:
-
DIGITS
- See Also:
-
LETTERS_AND_DIGITS
- See Also:
-
-
Constructor Details
-
TestUtils
public TestUtils()
-
-
Method Details
-
tempFile
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
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 mostmaxWaitMsand throw assertion failure otherwise. This should be used instead ofThread.sleepwhenever 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
-