diff --git a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestUtils.java b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestUtils.java index e3780a7eeaf32..fc424bbd03aad 100644 --- a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestUtils.java +++ b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestUtils.java @@ -223,13 +223,18 @@ public static void waitUntil(Supplier condition, String message) */ public static void waitUntil(Supplier condition, Duration timeout, String message) throws InterruptedException, TimeoutException { - long startTime = System.currentTimeMillis(); - while (!condition.get() && System.currentTimeMillis() < startTime + timeout.toMillis()) { + long deadline = System.currentTimeMillis() + timeout.toMillis(); + while (true) { + // Latch the result so that a condition that is satisfied once does not fail the + // wait if it becomes false again before a re-evaluation. + if (condition.get()) { + return; + } + if (System.currentTimeMillis() >= deadline) { + throw new TimeoutException(message); + } Thread.sleep(1); } - if (!condition.get()) { - throw new TimeoutException(message); - } } private static boolean allVerticesRunning(Map states) { diff --git a/flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/test/util/TestUtilsTest.java b/flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/test/util/TestUtilsTest.java new file mode 100644 index 0000000000000..bcc42e5a8fc61 --- /dev/null +++ b/flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/test/util/TestUtilsTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.test.util; + +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link TestUtils}. */ +class TestUtilsTest { + + @Test + void testWaitUntilReturnsWhenConditionIsMet() { + assertThatCode(() -> TestUtils.waitUntil(() -> true, Duration.ofSeconds(5), "msg")) + .doesNotThrowAnyException(); + } + + @Test + void testWaitUntilThrowsOnTimeout() { + assertThatThrownBy(() -> TestUtils.waitUntil(() -> false, Duration.ofMillis(50), "msg")) + .isInstanceOf(TimeoutException.class) + .hasMessage("msg"); + } + + @Test + void testWaitUntilSucceedsForConditionSatisfiedOnlyOnce() { + // A non-monotonic condition may become true and then false again. Once it has been + // observed as true, waitUntil must return successfully instead of re-evaluating it + // and throwing a spurious TimeoutException. + final AtomicBoolean firstCall = new AtomicBoolean(true); + assertThatCode( + () -> + TestUtils.waitUntil( + () -> firstCall.getAndSet(false), + Duration.ofSeconds(5), + "msg")) + .doesNotThrowAnyException(); + } +}