test_utils.rs (1466B)
1 use poll_promise::Promise; 2 use std::thread; 3 use std::time::Duration; 4 5 pub fn promise_wait<'a, T: Send + 'a>(promise: &'a Promise<T>) -> &'a T { 6 let mut count = 1; 7 loop { 8 if let Some(result) = promise.ready() { 9 println!("quieried promise num times: {}", count); 10 return result; 11 } else { 12 count += 1; 13 thread::sleep(Duration::from_millis(10)); 14 } 15 } 16 } 17 18 /// `promise_assert` macro 19 /// 20 /// This macro is designed to emulate the nature of immediate mode asynchronous code by repeatedly calling 21 /// promise.ready() for a promise, sleeping for a short period of time, and repeating until the promise is ready. 22 /// 23 /// Arguments: 24 /// - `$assertion_closure`: the assertion closure which takes two arguments: the actual result of the promise and 25 /// the expected value. This macro is used as an assertion closure to compare the actual and expected values. 26 /// - `$expected`: The expected value of type `T` that the promise's result is compared against. 27 /// - `$asserted_promise`: A `Promise<T>` that returns a value of type `T` when the promise is satisfied. This 28 /// represents the asynchronous operation whose result will be tested. 29 /// 30 #[macro_export] 31 macro_rules! promise_assert { 32 ($assertion_closure:ident, $expected:expr, $asserted_promise:expr) => { 33 let result = $crate::test_utils::promise_wait($asserted_promise); 34 $assertion_closure!(*result, $expected); 35 }; 36 }