From 0ce8dda269e489071feda37cd55d271eaeace2e6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 6 Mar 2023 20:41:56 +0800 Subject: [PATCH] test: rust assets module support tests --- src/assets/src/fetch.rs | 96 ++++++++++++++++++++++++++++++++++++++++- src/assets/src/ffi.rs | 12 ++---- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/assets/src/fetch.rs b/src/assets/src/fetch.rs index 15b135d..ca399ff 100644 --- a/src/assets/src/fetch.rs +++ b/src/assets/src/fetch.rs @@ -95,5 +95,99 @@ pub(crate) async fn asset_fetch(name: &str, sources: &Vec) -> Option(func: Pin>>) { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(func); + } + + fn gen_test_file() { + let mut fp = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open("/tmp/assets_test_file") + .unwrap(); + fp.write_all(TEST_DATA.as_ref()).expect("test file create error"); + } + + #[test] + fn asset() { + // test http_fetch function + run_async(Box::pin(async { + assert!(http_fetch("invalid url", 10).await.is_err()); + assert_eq!( + http_fetch("https://gstatic.com/generate_204", 10).await, + Ok(vec![]) + ); + })); + + // test local_fetch function + gen_test_file(); + run_async(Box::pin(async { + assert!(local_fetch("/").await.is_err()); + assert_eq!( + local_fetch("/tmp/assets_test_file").await, + Ok(vec![ + String::from("abc"), + String::from("123"), + String::from("456"), + String::from("abc"), + String::from("789"), + ]) + ); + })); + + // test combine asset_fetch function + run_async(Box::pin(async { + assert!( + asset_fetch("", &vec![]).await.unwrap().is_empty() + ); + assert!( + asset_fetch("", &vec![String::from("...")]).await.is_none() + ); + assert_eq!( + asset_fetch("", &vec![ + String::from("/tmp/assets_test_file"), + String::from("https://gstatic.com/generate_204") + ]).await, + Some(vec![ + String::from("abc"), + String::from("123"), + String::from("456"), + String::from("789"), + ]) + ); + })); + fs::remove_file("/tmp/assets_test_file").expect("test file delete error"); + } } diff --git a/src/assets/src/ffi.rs b/src/assets/src/ffi.rs index 1512d9f..6cb3663 100644 --- a/src/assets/src/ffi.rs +++ b/src/assets/src/ffi.rs @@ -84,17 +84,11 @@ pub async unsafe extern "C" fn asset_update( .open(&file) { // open target file Ok(mut fp) => { match fp.write_all(content.as_ref()) { - Err(err) => { - warn!("File `{}` save error: {}", file, err); - } - _ => { - debug!("File `{}` save success", file); - }, + Err(err) => warn!("File `{}` save error: {}", file, err), + _ => debug!("File `{}` save success", file), } }, - Err(err) => { - warn!("File `{}` open failed: {}", file, err); - }, + Err(err) => warn!("File `{}` open failed: {}", file, err), }; TRUE // asset fetch success },