Skip to content

Commit 5a8e54f

Browse files
committed
Rely on #10 and update Tokio
1 parent 5bb673a commit 5a8e54f

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

rusty-fork-macro/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ syn = { version = "1", features = ["full"] }
2323

2424
[dev-dependencies]
2525
rusty-fork = { path = "../", features = ["macro", "timeout"] }
26-
tokio = { version = "0.2", features = ["rt-threaded", "macros"] }
26+
tokio = { version = "1", features = ["macros", "rt"] }

rusty-fork-macro/src/lib.rs

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn fork_test(args: TokenStream, item: TokenStream) -> TokenStream {
7777

7878
// defaults
7979
let mut crate_name = quote::quote! { rusty_fork };
80-
let mut timeout = quote::quote! { 0 };
80+
let mut timeout = quote::quote! {};
8181

8282
// may be changed by the user
8383
for arg in args {
@@ -86,7 +86,7 @@ pub fn fork_test(args: TokenStream, item: TokenStream) -> TokenStream {
8686
match ident.to_string().as_str() {
8787
"timeout_ms" => {
8888
if let Lit::Int(int) = name_value.lit {
89-
timeout = int.to_token_stream();
89+
timeout = quote::quote! { #![rusty_fork(timeout_ms = #int)] }
9090
}
9191
}
9292
"crate" => {
@@ -113,16 +113,16 @@ pub fn fork_test(args: TokenStream, item: TokenStream) -> TokenStream {
113113

114114
let item = syn::parse_macro_input!(item as ItemFn);
115115

116-
let fn_attrs = &item.attrs;
117-
let fn_sig = &item.sig;
118-
let fn_body = &item.block;
119-
let fn_name = &fn_sig.ident;
116+
let fn_attrs = item.attrs;
117+
let fn_vis = item.vis;
118+
let fn_sig = item.sig;
119+
let fn_body = item.block;
120120

121121
// the default is that we add the `#[test]` for the use
122122
let mut test = quote::quote! { #[test] };
123123

124124
// we should still support a use case where the user adds it himself
125-
for attr in fn_attrs {
125+
for attr in &fn_attrs {
126126
if let Some(ident) = attr.path.get_ident() {
127127
if ident == "test" {
128128
test = quote::quote! {};
@@ -141,45 +141,13 @@ pub fn fork_test(args: TokenStream, item: TokenStream) -> TokenStream {
141141
.into();
142142
}
143143

144-
// support returning `Result`
145-
let body_fn = if let ReturnType::Type(_, ret_ty) = &fn_sig.output {
146-
quote::quote! {
147-
fn body_fn() {
148-
fn body_fn() -> #ret_ty #fn_body
149-
body_fn().unwrap();
150-
}
151-
}
152-
} else {
153-
quote::quote! {
154-
fn body_fn() #fn_body
155-
}
156-
};
157-
158144
(quote::quote! {
159-
#test
160-
#(#fn_attrs)*
161-
fn #fn_name() {
162-
// Eagerly convert everything to function pointers so that all
163-
// tests use the same instantiation of `fork`.
164-
#body_fn
165-
let body: fn () = body_fn;
166-
167-
fn supervise_fn(
168-
child: &mut ::#crate_name::ChildWrapper,
169-
_file: &mut ::std::fs::File
170-
) {
171-
::#crate_name::fork_test::supervise_child(child, #timeout)
172-
}
173-
let supervise:
174-
fn (&mut ::#crate_name::ChildWrapper, &mut ::std::fs::File) =
175-
supervise_fn;
176-
::#crate_name::fork(
177-
::#crate_name::rusty_fork_test_name!(#fn_name),
178-
::#crate_name::rusty_fork_id!(),
179-
::#crate_name::fork_test::no_configure_child,
180-
supervise,
181-
body
182-
).expect("forking test failed")
145+
::#crate_name::rusty_fork_test! {
146+
#timeout
147+
148+
#test
149+
#(#fn_attrs)*
150+
#fn_vis #fn_sig #fn_body
183151
}
184152
})
185153
.into()
@@ -235,7 +203,20 @@ mod test {
235203

236204
#[tokio::test]
237205
#[fork_test]
238-
async fn my_test() {
239-
assert!(true);
206+
async fn async_test() {
207+
tokio::task::spawn(async {
208+
println!("hello from child");
209+
})
210+
.await
211+
.unwrap();
212+
}
213+
214+
#[tokio::test]
215+
#[fork_test]
216+
async fn async_return_test() -> std::result::Result<(), tokio::task::JoinError> {
217+
tokio::task::spawn(async {
218+
println!("hello from child");
219+
})
220+
.await
240221
}
241222
}

0 commit comments

Comments
 (0)