Skip to content

Commit

Permalink
Refactor: test monoio functions
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Feb 19, 2024
1 parent 3de4dbe commit 36c2bfe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]

resolver = "1"
resolver = "2"

members = [
"async-entry",
Expand Down
21 changes: 9 additions & 12 deletions async-entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -490,12 +489,10 @@ type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]
pub fn test(args: TokenStream, item: TokenStream) -> 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.
Expand Down Expand Up @@ -674,15 +671,15 @@ 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(())
}

/// Parse rust source code in str and produce a TokenStream
fn str_to_p2tokens(s: &str, span: Span) -> Result<proc_macro2::TokenStream, syn::Error> {
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)
}

Expand Down
22 changes: 22 additions & 0 deletions async-entry/tests/test_rt.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 36c2bfe

Please sign in to comment.