From 498004e45ecf3a274ad8bae1b2d36eb14be4ba1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 21 Feb 2024 17:33:40 +0100 Subject: [PATCH] chore: Write more tests, specifically for error cases --- Cargo.lock | 7 +++++ car-mirror/Cargo.toml | 1 + car-mirror/src/common.rs | 60 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 01fa289..43a717c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,6 +78,12 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "assert_matches" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" + [[package]] name = "async-attributes" version = "1.1.2" @@ -521,6 +527,7 @@ name = "car-mirror" version = "0.1.0" dependencies = [ "anyhow", + "assert_matches", "async-std", "async-stream", "bytes", diff --git a/car-mirror/Cargo.toml b/car-mirror/Cargo.toml index 4392bbd..06f99c3 100644 --- a/car-mirror/Cargo.toml +++ b/car-mirror/Cargo.toml @@ -37,6 +37,7 @@ tracing = "0.1" wnfs-common = { workspace = true } [dev-dependencies] +assert_matches = "1.5.0" async-std = { version = "1.11", features = ["attributes"] } car-mirror = { path = ".", features = ["quick_cache", "test_utils"] } proptest = "1.1" diff --git a/car-mirror/src/common.rs b/car-mirror/src/common.rs index 1cc22f9..c757177 100644 --- a/car-mirror/src/common.rs +++ b/car-mirror/src/common.rs @@ -622,8 +622,9 @@ impl std::fmt::Debug for ReceiverState { pub(crate) mod tests { use super::*; use crate::{cache::NoCache, test_utils::assert_cond_send_sync}; + use assert_matches::assert_matches; use testresult::TestResult; - use wnfs_common::MemoryBlockStore; + use wnfs_common::{MemoryBlockStore, CODEC_RAW}; #[allow(clippy::unreachable, unused)] fn test_assert_send() { @@ -660,4 +661,61 @@ pub(crate) mod tests { Ok(()) } + + #[test_log::test(async_std::test)] + async fn test_stream_car_frame_empty() -> TestResult { + let car_frames = stream_car_frames(futures::stream::empty().boxed()).await?; + let frames: Vec = car_frames.try_collect().await?; + + assert!(frames.is_empty()); + + Ok(()) + } + + #[test_log::test(async_std::test)] + async fn test_write_blocks_into_car_empty() -> TestResult { + let car_file = + write_blocks_into_car(Vec::new(), &mut futures::stream::empty().boxed(), None).await?; + + assert!(car_file.is_empty()); + + Ok(()) + } + + #[test_log::test(async_std::test)] + async fn test_block_receive_block_stream_block_size_exceeded() -> TestResult { + let store = &MemoryBlockStore::new(); + + let block_small: Bytes = b"This one is small".to_vec().into(); + let block_big: Bytes = b"This one is very very very big".to_vec().into(); + let root_small = store.put_block(block_small.clone(), CODEC_RAW).await?; + let root_big = store.put_block(block_big.clone(), CODEC_RAW).await?; + + let config = &Config { + max_block_size: 20, + ..Config::default() + }; + + block_receive_block_stream( + root_small, + &mut futures::stream::iter(vec![Ok((root_small, block_small))]).boxed(), + config, + MemoryBlockStore::new(), + NoCache, + ) + .await?; + + let result = block_receive_block_stream( + root_small, + &mut futures::stream::iter(vec![Ok((root_big, block_big))]).boxed(), + config, + MemoryBlockStore::new(), + NoCache, + ) + .await; + + assert_matches!(result, Err(Error::BlockSizeExceeded { .. })); + + Ok(()) + } }