From 36c2bfe1bdaf684e114317a4d2b05ca66816dab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Mon, 19 Feb 2024 13:45:17 +0800 Subject: [PATCH] Refactor: test monoio functions --- Cargo.toml | 2 +- async-entry/src/lib.rs | 21 +++++++++------------ async-entry/tests/test_rt.rs | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 async-entry/tests/test_rt.rs diff --git a/Cargo.toml b/Cargo.toml index a1cc20f..a68d587 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] -resolver = "1" +resolver = "2" members = [ "async-entry", diff --git a/async-entry/src/lib.rs b/async-entry/src/lib.rs index a13ed69..ac7341c 100644 --- a/async-entry/src/lib.rs +++ b/async-entry/src/lib.rs @@ -11,12 +11,11 @@ use syn::ItemFn; fn get_runtime_name() -> &'static str { if cfg!(feature = "tokio") { - return "tokio"; - } - if cfg!(feature = "monoio") { - return "monoio"; + "tokio" + } else if cfg!(feature = "monoio") { + "monoio" } else { - return "tokio"; + "tokio" } } @@ -490,12 +489,10 @@ type AttributeArgs = syn::punctuated::Punctuated TokenStream { let tokens = entry_test(args, item.clone()); - let res = match tokens { + match tokens { Ok(x) => x, - Err(e) => return token_stream_with_error(item, e), - }; - - res + Err(e) => token_stream_with_error(item, e), + } } /// Entry of async test fn. @@ -674,7 +671,7 @@ fn check_dup_test_attr(input: &ItemFn) -> Result<(), syn::Error> { let mut attrs = input.attrs.iter(); let found = attrs.find(|a| a.path.is_ident("test")); if let Some(attr) = found { - return Err(syn::Error::new_spanned(&attr, "dup test")); + return Err(syn::Error::new_spanned(attr, "dup test")); } Ok(()) @@ -682,7 +679,7 @@ fn check_dup_test_attr(input: &ItemFn) -> Result<(), syn::Error> { /// Parse rust source code in str and produce a TokenStream fn str_to_p2tokens(s: &str, span: Span) -> Result { - let toks = proc_macro2::TokenStream::from_str(s).map_err(|e| syn::Error::new(span, &e))?; + let toks = proc_macro2::TokenStream::from_str(s).map_err(|e| syn::Error::new(span, e))?; Ok(toks) } diff --git a/async-entry/tests/test_rt.rs b/async-entry/tests/test_rt.rs new file mode 100644 index 0000000..af9e1cf --- /dev/null +++ b/async-entry/tests/test_rt.rs @@ -0,0 +1,22 @@ +#[cfg(feature = "monoio")] +mod test_monoio { + + use std::sync::{Arc, Mutex}; + + #[async_entry::test] + async fn test_monoio_spawn() { + let m = Arc::new(Mutex::new(0u64)); + + let m2 = m.clone(); + + let join_handle = monoio::spawn(async move { + let mut v = m2.lock().unwrap(); + *v = 5; + }); + + join_handle.await; + + let got = *(m.lock().unwrap()); + assert_eq!(got, 5); + } +}