From 7cae064ed68d02a9f8a5eef5a456620b8546517a Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 25 Jun 2022 13:25:43 +0000 Subject: [PATCH] unittest: Fix hyper panic --- proc_macros/proc_macros.rs | 58 ++++++++++++++++++++++++ src/fanbox_api.rs | 92 ++++---------------------------------- 2 files changed, 67 insertions(+), 83 deletions(-) diff --git a/proc_macros/proc_macros.rs b/proc_macros/proc_macros.rs index 4839244..53dedaa 100644 --- a/proc_macros/proc_macros.rs +++ b/proc_macros/proc_macros.rs @@ -1,9 +1,14 @@ use proc_macro::TokenStream; use quote::quote; +use syn::parse::Parse; use syn::parse_macro_input; +use syn::token; +use syn::Expr; use syn::Ident; use syn::ItemFn; +use syn::Lit; use syn::LitInt; +use syn::LitStr; #[proc_macro] pub fn define_struct_reader_fn(item: TokenStream) -> TokenStream { @@ -105,3 +110,56 @@ pub fn async_timeout_test(attr: TokenStream, item: TokenStream) -> TokenStream { }; stream.into() } + +struct FanboxApiTest { + pub name: Ident, + pub expr: Expr, + pub errmsg: LitStr, +} + +impl Parse for FanboxApiTest { + fn parse(input: syn::parse::ParseStream) -> syn::Result { + let name = Ident::parse(input)?; + token::Comma::parse(input)?; + let expr = Expr::parse(input)?; + token::Comma::parse(input)?; + let errmsg = Lit::parse(input)?; + match errmsg { + Lit::Str(errmsg) => Ok(Self { name, expr, errmsg }), + _ => Err(syn::Error::new(errmsg.span(), "Failed to parse string.")), + } + } +} + +#[proc_macro] +pub fn fanbox_api_quick_test(item: TokenStream) -> TokenStream { + let FanboxApiTest { name, expr, errmsg } = parse_macro_input!(item as FanboxApiTest); + let stream = quote! { + #[proc_macros::async_timeout_test(120s)] + #[tokio::test(flavor = "multi_thread")] + async fn #name() { + match std::env::var("FANBOX_COOKIES_FILE") { + Ok(path) => { + let client = FanboxClient::new(); + if !client.init(Some(path)) { + panic!("Failed to initiailze the client."); + } + if !client.check_login().await { + println!("The client is not logined. Skip test."); + return; + } + match #expr.await { + Some(_) => {} + None => { + panic!("{}", #errmsg); + } + } + } + Err(_) => { + println!("No cookies files specified, skip test.") + } + } + } + }; + stream.into() +} diff --git a/src/fanbox_api.rs b/src/fanbox_api.rs index 54ea143..5a56c6e 100644 --- a/src/fanbox_api.rs +++ b/src/fanbox_api.rs @@ -1,20 +1,13 @@ use crate::ext::atomic::AtomicQuick; -#[cfg(test)] -use crate::ext::replace::ReplaceWith; use crate::ext::replace::ReplaceWith2; use crate::ext::rw_lock::GetRwLock; use crate::gettext; use crate::opthelper::get_helper; use crate::parser::metadata::MetaDataParser; use crate::webclient::WebClient; -#[cfg(test)] -use futures_util::lock::Mutex; use json::JsonValue; -#[cfg(test)] -use std::ops::Deref; +use proc_macros::fanbox_api_quick_test; use std::sync::atomic::AtomicBool; -#[cfg(test)] -use std::sync::Arc; use std::sync::RwLock; /// Fanbox API client @@ -215,90 +208,23 @@ impl FanboxClient { } } -#[cfg(test)] -lazy_static! { - #[doc(hidden)] - static ref TEST_CLIENT: Arc = Arc::new(FanboxClient::new()); - #[doc(hidden)] - /// Used to lock initilize process. The data is set to true after trying check login. - static ref LOCK: Mutex = Mutex::new(false); -} - -#[cfg(test)] -async fn init_test_client(path: String) -> bool { - let _ = LOCK.lock().await; - if !TEST_CLIENT.is_inited() { - if !TEST_CLIENT.init(Some(path)) { - return false; - } - } - true -} - -#[cfg(test)] -async fn check_login() -> bool { - let mut ctx = LOCK.lock().await; - if !ctx.deref() { - ctx.replace_with(true); - if !TEST_CLIENT.check_login().await { - return false; - } - } - TEST_CLIENT.logined() -} - -#[cfg(test)] -macro_rules! quick_test { - ($name:ident, $exp:expr, $err:literal) => { - #[proc_macros::async_timeout_test(120s)] - #[tokio::test(flavor = "multi_thread")] - async fn $name() { - match std::env::var("FANBOX_COOKIES_FILE") { - Ok(path) => { - let re = init_test_client(path).await; - if !re { - panic!("Failed to initiailze the client."); - } - if !check_login().await { - println!("The client is not logined. Skip test."); - return; - } - match $exp.await { - Some(_) => {} - None => { - panic!("{}", $err); - } - } - } - Err(_) => { - println!("No cookies files specified, skip test.") - } - } - } - }; -} - -#[cfg(test)] -quick_test!( +fanbox_api_quick_test!( test_list_home_post, - TEST_CLIENT.list_home_post(10), + client.list_home_post(10), "Failed to list home page's posts." ); -#[cfg(test)] -quick_test!( +fanbox_api_quick_test!( test_list_supporting_post, - TEST_CLIENT.list_supporting_post(10), + client.list_supporting_post(10), "Failed to list supported creators' posts." ); -#[cfg(test)] -quick_test!( +fanbox_api_quick_test!( test_list_supporting_plan, - TEST_CLIENT.list_supporting_plan(), + client.list_supporting_plan(), "Failed to list all supporting plans." ); -#[cfg(test)] -quick_test!( +fanbox_api_quick_test!( test_get_creator, - TEST_CLIENT.get_creator("mozukun43"), + client.get_creator("mozukun43"), "Failed to list all supporting plans." );