Skip to content

Commit

Permalink
chore: Write more tests, specifically for error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Feb 21, 2024
1 parent abf3819 commit 498004e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions car-mirror/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
60 changes: 59 additions & 1 deletion car-mirror/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<Bytes> = 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(())
}
}

0 comments on commit 498004e

Please sign in to comment.