tests_fuzz/utils/wait.rs
1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::time::Duration;
16
17use futures::future::BoxFuture;
18
19/// Waits for a condition to be met by repeatedly checking it within a given timeout period.
20///
21/// This function repeatedly calls the `check` function and applies the `condition` function to the result.
22/// If the condition is met within the timeout period, the function returns. Otherwise, it waits for the retry
23/// interval and checks again, until the timeout period elapses.
24pub async fn wait_condition_fn<F, T, U>(
25 timeout: Duration,
26 check: F,
27 condition: U,
28 retry_interval: Duration,
29) where
30 F: Fn() -> BoxFuture<'static, T>,
31 U: Fn(T) -> bool,
32{
33 tokio::time::timeout(timeout, async move {
34 loop {
35 if condition(check().await) {
36 break;
37 }
38 tokio::time::sleep(retry_interval).await
39 }
40 })
41 .await
42 .unwrap();
43}