1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#[macro_export]
macro_rules! loop_fn {
($expr:expr, $dur:expr) => {{
let mut rt_wrap = None;
loop {
let fn_rt = $expr;
match fn_rt {
std::result::Result::Ok(val) => {
if rt_wrap.is_none() {
rt_wrap = Some(val);
}
break;
}
std::result::Result::Err(err) => {
error!("loop function({}) error. {}", stringify!($expr), err);
std::thread::sleep($dur);
}
}
}
rt_wrap.unwrap()
}};
}
#[macro_export]
macro_rules! async_delay {
($delay:expr) => {{
if $idle_counter > 3 {
if $idle_counter < 10 {
tokio::time::delay_for(std::time::Duration::MILLISECOND).await;
} else if $idle_counter < 20 {
tokio::time::delay_for(std::time::Duration::from_millis(50)).await;
} else if $idle_counter < 30 {
tokio::time::delay_for(std::time::Duration::from_millis(500)).await;
} else {
tokio::time::delay_for(std::time::Duration::from_secs(2)).await;
}
}
}};
}
#[cfg(test)]
mod tests {
use std::borrow::BorrowMut;
fn mock(value: &mut i64) -> Result<String, std::io::Error> {
println!("======");
*value += 1;
if *value < 100 {
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "a"))
} else {
Ok("V".to_string())
}
}
#[test]
pub fn loop_result_test() {
let mut value = 0i64;
let n: String = loop_fn!(
mock(value.borrow_mut()),
std::time::Duration::from_millis(5)
);
assert_eq!(n, "V");
assert_eq!(value, 100i64);
}
}