Package org.apache.flink.core.testutils
Class CommonTestUtils
- java.lang.Object
-
- org.apache.flink.core.testutils.CommonTestUtils
-
public class CommonTestUtils extends Object
This class contains reusable utility methods for unit tests.
-
-
Constructor Summary
Constructors Constructor Description CommonTestUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertThrows(String msg, Class<? extends Exception> expected, Callable<?> code)Checks whether an exception with a message occurs when running a piece of code.static voidblockForeverNonInterruptibly()Permanently blocks the current thread.static booleancontainsCause(Throwable throwable, Class<? extends Throwable> cause)Checks whether the given throwable contains the given cause as a cause.static <T extends Serializable>
TcreateCopySerializable(T original)Creates a copy of an object via Java Serialization.static StringcreateTempFile(String contents)Creates a temporary file that contains the given string.static voidsetEnv(Map<String,String> newenv)static voidsetEnv(Map<String,String> newenv, boolean clearExisting)static voidwaitUtil(java.util.function.Supplier<Boolean> condition, java.time.Duration timeout, String errorMsg)Wait util the given condition is met or timeout.static voidwaitUtil(java.util.function.Supplier<Boolean> condition, java.time.Duration timeout, java.time.Duration pause, String errorMsg)Wait util the given condition is met or timeout.
-
-
-
Method Detail
-
createCopySerializable
public static <T extends Serializable> T createCopySerializable(T original) throws IOException
Creates a copy of an object via Java Serialization.- Parameters:
original- The original object.- Returns:
- The copied object.
- Throws:
IOException
-
createTempFile
public static String createTempFile(String contents) throws IOException
Creates a temporary file that contains the given string. The file is written with the platform's default encoding.The temp file is automatically deleted on JVM exit.
- Parameters:
contents- The contents to be written to the file.- Returns:
- The temp file URI.
- Throws:
IOException
-
blockForeverNonInterruptibly
public static void blockForeverNonInterruptibly()
Permanently blocks the current thread. The thread cannot be woken up viaThread.interrupt().
-
containsCause
public static boolean containsCause(Throwable throwable, Class<? extends Throwable> cause)
Checks whether the given throwable contains the given cause as a cause. The cause is not checked on equality but on type equality.- Parameters:
throwable- Throwable to check for the causecause- Cause to look for- Returns:
- True if the given Throwable contains the given cause (type equality); otherwise false
-
assertThrows
public static void assertThrows(String msg, Class<? extends Exception> expected, Callable<?> code)
Checks whether an exception with a message occurs when running a piece of code.
-
waitUtil
public static void waitUtil(java.util.function.Supplier<Boolean> condition, java.time.Duration timeout, java.time.Duration pause, String errorMsg) throws TimeoutException, InterruptedException
Wait util the given condition is met or timeout.- Parameters:
condition- the condition to wait for.timeout- the maximum time to wait for the condition to become true.pause- delay between condition checks.errorMsg- the error message to include in theTimeoutExceptionif the condition was not met before timeout.- Throws:
TimeoutException- if the condition is not met before timeout.InterruptedException- if the thread is interrupted.
-
waitUtil
public static void waitUtil(java.util.function.Supplier<Boolean> condition, java.time.Duration timeout, String errorMsg) throws TimeoutException, InterruptedException
Wait util the given condition is met or timeout.- Parameters:
condition- the condition to wait for.timeout- the maximum time to wait for the condition to become true.errorMsg- the error message to include in theTimeoutExceptionif the condition was not met before timeout.- Throws:
TimeoutException- if the condition is not met before timeout.InterruptedException- if the thread is interrupted.
-
-