chia7712 commented on code in PR #19884: URL: https://github.com/apache/kafka/pull/19884#discussion_r2153472694
########## test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/junit/RaftClusterInvocationContext.java: ########## @@ -71,6 +71,41 @@ public class RaftClusterInvocationContext implements TestTemplateInvocationConte private final ClusterConfig clusterConfig; private final boolean isCombined; + // Copied from TestUtils (package-private) + private static final long DEFAULT_POLL_INTERVAL_MS = 100; + private static final long DEFAULT_MAX_WAIT_MS = 15_000; + + /** + * Wait for condition to be met for at most 15 seconds and throw assertion failure otherwise. + * This should be used instead of {@code 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. + */ + public static void waitForCondition(final java.util.function.Supplier<Boolean> testCondition, Review Comment: please use package-private ########## test-common/test-common-runtime/src/main/java/org/apache/kafka/common/test/junit/RaftClusterInvocationContext.java: ########## @@ -71,6 +71,41 @@ public class RaftClusterInvocationContext implements TestTemplateInvocationConte private final ClusterConfig clusterConfig; private final boolean isCombined; + // Copied from TestUtils (package-private) + private static final long DEFAULT_POLL_INTERVAL_MS = 100; + private static final long DEFAULT_MAX_WAIT_MS = 15_000; + + /** + * Wait for condition to be met for at most 15 seconds and throw assertion failure otherwise. + * This should be used instead of {@code 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. + */ + public static void waitForCondition(final java.util.function.Supplier<Boolean> testCondition, + final String conditionDetails) throws InterruptedException { + var maxWaitMs = 15_000L; + long endTime = System.currentTimeMillis() + maxWaitMs; + + while (System.currentTimeMillis() < endTime) { + try { + if (testCondition.get()) { + return; + } + } catch (Exception e) { + if (System.currentTimeMillis() >= endTime) { + throw new AssertionError(String.format("Assertion failed with an exception after %s ms", maxWaitMs), e); + } + } + + if (System.currentTimeMillis() < endTime) { + TimeUnit.MILLISECONDS.sleep(100); + } + } + + String details = conditionDetails != null ? conditionDetails : ""; Review Comment: we don't need to null check as it is impossible in the fewer usages. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org