fail-rs icon indicating copy to clipboard operation
fail-rs copied to clipboard

Support async return

Open waynexia opened this issue 4 years ago • 7 comments

Support for defining fail points with async blocks. E.g.:

  async fn async_fn() {
      fail_point!("async_return", |s| async {
          (async {}).await;
          s.map_or(2, |s| s.parse().unwrap())
      });
  }

waynexia avatar Sep 28 '21 15:09 waynexia

Interesting. What if user wants async move {}?

BusyJay avatar Sep 29 '21 05:09 BusyJay

Sorry for the late reply :P

For move before the closure param list, we can match it with ident. But I can't figure out how to match those after async. Here I use another rule to handle this. Please let me know if there is a better approach!

waynexia avatar Oct 08 '21 17:10 waynexia

How about:

    ($name:expr, await $e:expr) => {{
        if let Some(res) = $crate::eval($name, $e) {
            return res.await;
        }
    }};

The rule is simple. And it matches all cases without depending on the appearance of any keywords in the expression. One drawback is it moves await ahead, which is opposite to the syntax of await.

BusyJay avatar Oct 09 '21 12:10 BusyJay

/cc @brson @lucab @sticnarf I also would like to hear your advice.

BusyJay avatar Oct 09 '21 12:10 BusyJay

It is a good point to make the rules simple by not following the formal grammar. Wondering what others think.

waynexia avatar Oct 11 '21 03:10 waynexia

Speaking of async, I do wonder whether it makes sense to have a dedicated async_fail_point!() instead. I currently see a mismatch with actions like sleep and yield in an async context, which will block the executor instead of behaving more gracefully. I'm not really a macro expert, but perhaps the implementation could end up being easier if we clearly split the sync-vs-async domains through different macros?

lucab avatar Oct 11 '21 07:10 lucab

I defined a new macro async_fail_point as @lucab said. It does make things more straightforward 👍 PTAL @BusyJay @lucab

waynexia avatar Oct 25 '23 03:10 waynexia