From 49c565f7923c61d6ebc40b16fd63c8442ccc3f39 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Mon, 19 Aug 2024 14:49:56 -0400 Subject: [PATCH] Use tokio::test instead of tokio-test --- Cargo.lock | 14 --- attic/Cargo.toml | 2 +- attic/src/chunking/mod.rs | 35 ++++--- attic/src/nix_store/tests/mod.rs | 159 +++++++++++++++---------------- attic/src/stream.rs | 29 +++--- server/Cargo.toml | 3 - 6 files changed, 105 insertions(+), 137 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a04bbc..b56abab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -334,7 +334,6 @@ dependencies = [ "serde_with", "sha2", "tokio", - "tokio-test", "tokio-util", "toml", "tower-http", @@ -4748,19 +4747,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-test" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7" -dependencies = [ - "async-stream", - "bytes", - "futures-core", - "tokio", - "tokio-stream", -] - [[package]] name = "tokio-util" version = "0.7.11" diff --git a/attic/Cargo.toml b/attic/Cargo.toml index 756519b..2f632e2 100644 --- a/attic/Cargo.toml +++ b/attic/Cargo.toml @@ -35,6 +35,7 @@ optional = true features = [ "fs", "io-util", + "macros", "process", "sync", ] @@ -43,7 +44,6 @@ features = [ criterion = { version = "0.5", features = ["html_reports", "async_tokio"] } fastcdc = { version = "*", features = ["tokio"] } serde_json = "1.0.96" -tokio-test = "0.4.2" [build-dependencies] cc = "1.1.13" diff --git a/attic/src/chunking/mod.rs b/attic/src/chunking/mod.rs index 031aa96..e054219 100644 --- a/attic/src/chunking/mod.rs +++ b/attic/src/chunking/mod.rs @@ -72,33 +72,30 @@ mod tests { use std::io::Cursor; use futures::StreamExt; - use tokio_test::block_on; use crate::testing::get_fake_data; /// Chunks and reconstructs a file. - #[test] - fn test_chunking_basic() { - fn case(size: usize) { - block_on(async move { - let test_file = get_fake_data(size); // 32 MiB - let mut reconstructed_file = Vec::new(); + #[tokio::test] + async fn test_chunking_basic() { + async fn case(size: usize) { + let test_file = get_fake_data(size); // 32 MiB + let mut reconstructed_file = Vec::new(); - let cursor = Cursor::new(&test_file); - let mut chunks = chunk_stream(cursor, 8 * 1024, 16 * 1024, 32 * 1024); + let cursor = Cursor::new(&test_file); + let mut chunks = chunk_stream(cursor, 8 * 1024, 16 * 1024, 32 * 1024); - while let Some(chunk) = chunks.next().await { - let chunk = chunk.unwrap(); - eprintln!("Got a {}-byte chunk", chunk.len()); - reconstructed_file.extend(chunk); - } + while let Some(chunk) = chunks.next().await { + let chunk = chunk.unwrap(); + eprintln!("Got a {}-byte chunk", chunk.len()); + reconstructed_file.extend(chunk); + } - assert_eq!(reconstructed_file, test_file); - }); + assert_eq!(reconstructed_file, test_file); } - case(32 * 1024 * 1024 - 1); - case(32 * 1024 * 1024); - case(32 * 1024 * 1024 + 1); + case(32 * 1024 * 1024 - 1).await; + case(32 * 1024 * 1024).await; + case(32 * 1024 * 1024 + 1).await; } } diff --git a/attic/src/nix_store/tests/mod.rs b/attic/src/nix_store/tests/mod.rs index 3d23922..ac5e2f9 100644 --- a/attic/src/nix_store/tests/mod.rs +++ b/attic/src/nix_store/tests/mod.rs @@ -6,7 +6,6 @@ use std::os::unix::ffi::OsStrExt; use std::process::Command; use serde::de::DeserializeOwned; -use tokio_test::block_on; pub mod test_nar; @@ -143,113 +142,105 @@ fn test_store_path_hash() { StorePathHash::new(h).unwrap_err(); } -#[test] -fn test_nar_streaming() { +#[tokio::test] +async fn test_nar_streaming() { let store = NixStore::connect().expect("Failed to connect to the Nix store"); - block_on(async move { - let test_nar = test_nar::NO_DEPS; - test_nar.import().await.expect("Could not import test NAR"); + let test_nar = test_nar::NO_DEPS; + test_nar.import().await.expect("Could not import test NAR"); - let target = test_nar.get_target().expect("Could not create dump target"); - let writer = target.get_writer().await.expect("Could not get writer"); + let target = test_nar.get_target().expect("Could not create dump target"); + let writer = target.get_writer().await.expect("Could not get writer"); - let store_path = store.parse_store_path(test_nar.path()).unwrap(); + let store_path = store.parse_store_path(test_nar.path()).unwrap(); - let stream = store.nar_from_path(store_path); - stream.write_all(writer).await.unwrap(); + let stream = store.nar_from_path(store_path); + stream.write_all(writer).await.unwrap(); - target - .validate() - .await - .expect("Could not validate resulting dump"); - }); + target + .validate() + .await + .expect("Could not validate resulting dump"); } -#[test] -fn test_compute_fs_closure() { +#[tokio::test] +async fn test_compute_fs_closure() { + use test_nar::{WITH_DEPS_A, WITH_DEPS_B, WITH_DEPS_C}; + let store = NixStore::connect().expect("Failed to connect to the Nix store"); - block_on(async move { - use test_nar::{WITH_DEPS_A, WITH_DEPS_B, WITH_DEPS_C}; + for nar in [WITH_DEPS_C, WITH_DEPS_B, WITH_DEPS_A] { + nar.import().await.expect("Could not import test NAR"); - for nar in [WITH_DEPS_C, WITH_DEPS_B, WITH_DEPS_A] { - nar.import().await.expect("Could not import test NAR"); - - let path = store - .parse_store_path(nar.path()) - .expect("Could not parse store path"); - - let actual: HashSet = store - .compute_fs_closure(path, false, false, false) - .await - .expect("Could not compute closure") - .into_iter() - .collect(); - - assert_eq!(nar.closure(), actual); - } - }); -} - -#[test] -fn test_compute_fs_closure_multi() { - let store = NixStore::connect().expect("Failed to connect to the Nix store"); - - block_on(async move { - use test_nar::{NO_DEPS, WITH_DEPS_A, WITH_DEPS_B, WITH_DEPS_C}; - - for nar in [NO_DEPS, WITH_DEPS_C, WITH_DEPS_B, WITH_DEPS_A] { - nar.import().await.expect("Could not import test NAR"); - } - - let mut expected = NO_DEPS.closure(); - expected.extend(WITH_DEPS_A.closure()); - - let paths = vec![ - store.parse_store_path(WITH_DEPS_A.path()).unwrap(), - store.parse_store_path(NO_DEPS.path()).unwrap(), - ]; + let path = store + .parse_store_path(nar.path()) + .expect("Could not parse store path"); let actual: HashSet = store - .compute_fs_closure_multi(paths, false, false, false) + .compute_fs_closure(path, false, false, false) .await .expect("Could not compute closure") .into_iter() .collect(); - eprintln!("Closure: {:#?}", actual); - - assert_eq!(expected, actual); - }); + assert_eq!(nar.closure(), actual); + } } -#[test] -fn test_query_path_info() { +#[tokio::test] +async fn test_compute_fs_closure_multi() { + use test_nar::{NO_DEPS, WITH_DEPS_A, WITH_DEPS_B, WITH_DEPS_C}; + let store = NixStore::connect().expect("Failed to connect to the Nix store"); - block_on(async move { - use test_nar::{WITH_DEPS_B, WITH_DEPS_C}; + for nar in [NO_DEPS, WITH_DEPS_C, WITH_DEPS_B, WITH_DEPS_A] { + nar.import().await.expect("Could not import test NAR"); + } - for nar in [WITH_DEPS_C, WITH_DEPS_B] { - nar.import().await.expect("Could not import test NAR"); - } + let mut expected = NO_DEPS.closure(); + expected.extend(WITH_DEPS_A.closure()); - let nar = WITH_DEPS_B; - let path = store.parse_store_path(nar.path()).unwrap(); - let path_info = store - .query_path_info(path) - .await - .expect("Could not query path info"); + let paths = vec![ + store.parse_store_path(WITH_DEPS_A.path()).unwrap(), + store.parse_store_path(NO_DEPS.path()).unwrap(), + ]; - eprintln!("Path info: {:?}", path_info); + let actual: HashSet = store + .compute_fs_closure_multi(paths, false, false, false) + .await + .expect("Could not compute closure") + .into_iter() + .collect(); - assert_eq!(nar.nar().len() as u64, path_info.nar_size); - assert_eq!( - vec![PathBuf::from( - "3k1wymic8p7h5pfcqfhh0jan8ny2a712-attic-test-with-deps-c-final" - ),], - path_info.references - ); - }); + eprintln!("Closure: {:#?}", actual); + + assert_eq!(expected, actual); +} + +#[tokio::test] +async fn test_query_path_info() { + use test_nar::{WITH_DEPS_B, WITH_DEPS_C}; + + let store = NixStore::connect().expect("Failed to connect to the Nix store"); + + for nar in [WITH_DEPS_C, WITH_DEPS_B] { + nar.import().await.expect("Could not import test NAR"); + } + + let nar = WITH_DEPS_B; + let path = store.parse_store_path(nar.path()).unwrap(); + let path_info = store + .query_path_info(path) + .await + .expect("Could not query path info"); + + eprintln!("Path info: {:?}", path_info); + + assert_eq!(nar.nar().len() as u64, path_info.nar_size); + assert_eq!( + vec![PathBuf::from( + "3k1wymic8p7h5pfcqfhh0jan8ny2a712-attic-test-with-deps-c-final" + ),], + path_info.references + ); } diff --git a/attic/src/stream.rs b/attic/src/stream.rs index fede7b8..077b021 100644 --- a/attic/src/stream.rs +++ b/attic/src/stream.rs @@ -176,10 +176,9 @@ mod tests { use bytes::{BufMut, BytesMut}; use futures::future; use tokio::io::AsyncReadExt; - use tokio_test::block_on; - #[test] - fn test_stream_hasher() { + #[tokio::test] + async fn test_stream_hasher() { let expected = b"hello world"; let expected_sha256 = hex::decode("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9") @@ -191,10 +190,10 @@ mod tests { // force multiple reads let mut buf = vec![0u8; 100]; let mut bytes_read = 0; - bytes_read += block_on(read.read(&mut buf[bytes_read..bytes_read + 5])).unwrap(); - bytes_read += block_on(read.read(&mut buf[bytes_read..bytes_read + 5])).unwrap(); - bytes_read += block_on(read.read(&mut buf[bytes_read..bytes_read + 5])).unwrap(); - bytes_read += block_on(read.read(&mut buf[bytes_read..bytes_read + 5])).unwrap(); + bytes_read += read.read(&mut buf[bytes_read..bytes_read + 5]).await.unwrap(); + bytes_read += read.read(&mut buf[bytes_read..bytes_read + 5]).await.unwrap(); + bytes_read += read.read(&mut buf[bytes_read..bytes_read + 5]).await.unwrap(); + bytes_read += read.read(&mut buf[bytes_read..bytes_read + 5]).await.unwrap(); assert_eq!(expected.len(), bytes_read); assert_eq!(expected, &buf[..bytes_read]); @@ -206,8 +205,8 @@ mod tests { eprintln!("finalized = {:x?}", finalized); } - #[test] - fn test_merge_chunks() { + #[tokio::test] + async fn test_merge_chunks() { let chunk_a: BoxStream> = { let s = stream! { yield Ok(Bytes::from_static(b"Hello")); @@ -236,13 +235,11 @@ mod tests { let streamer = |c, _| future::ok(c); let mut merged = merge_chunks(chunks, streamer, (), 2); - let bytes = block_on(async move { - let mut bytes = BytesMut::with_capacity(100); - while let Some(item) = merged.next().await { - bytes.put(item.unwrap()); - } - bytes.freeze() - }); + let mut bytes = BytesMut::with_capacity(100); + while let Some(item) = merged.next().await { + bytes.put(item.unwrap()); + } + let bytes = bytes.freeze(); assert_eq!(&*bytes, b"Hello, world!"); } diff --git a/server/Cargo.toml b/server/Cargo.toml index 3fcf108..977ba2d 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -94,6 +94,3 @@ features = [ "rt-multi-thread", "sync", ] - -[dev-dependencies] -tokio-test = "0.4.2"