Skip to content

Commit

Permalink
better test case err report
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Sep 20, 2024
1 parent 2093798 commit ce47949
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions crates/storage/provider/src/providers/static_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ mod tests {
BlockHash, Header, Receipt, TransactionSignedNoHash,
};
use reth_testing_utils::generators::{self, random_header_range};
use std::{fs, ops::Range, path::Path};
use std::{fmt::Debug, fs, ops::Range, path::Path};

fn assert_eyre<T: PartialEq + Debug>(got: T, expected: T, msg: &str) -> eyre::Result<()> {
if got != expected {
eyre::bail!("{msg} | got: {got:?} expected: {expected:?})");
}
Ok(())
}

#[test]
fn test_snap() {
Expand Down Expand Up @@ -186,18 +193,25 @@ mod tests {
prune_count: u64,
expected_tip: Option<u64>,
expected_file_count: u64,
) {
writer.prune_headers(prune_count).unwrap();
writer.commit().unwrap();
) -> eyre::Result<()> {
writer.prune_headers(prune_count)?;
writer.commit()?;

// Validate the highest block after pruning
assert_eq!(
assert_eyre(
sf_rw.get_highest_static_file_block(StaticFileSegment::Headers),
expected_tip
);
expected_tip,
"mismatch tip",
)?;

// Validate the number of files remaining in the directory
assert_eq!(fs::read_dir(static_dir).unwrap().count(), expected_file_count as usize);
assert_eyre(
fs::read_dir(static_dir).unwrap().count(),
expected_file_count as usize,
"mismatch file count",
)?;

Ok(())
}

// [ Test Cases ]
Expand All @@ -206,31 +220,31 @@ mod tests {
type ExpectedFileCount = u64;
let mut tmp_tip = tip;
let test_cases: Vec<(PruneCount, Option<ExpectedTip>, ExpectedFileCount)> = vec![
// Case 1: Pruning 1 header
// Case 0: Pruning 1 header
{
tmp_tip -= 1;
(1, Some(tmp_tip), initial_file_count)
},
// Case 2: Pruning remaining rows from file should result in its deletion
// Case 1: Pruning remaining rows from file should result in its deletion
{
tmp_tip -= blocks_per_file - 1;
(blocks_per_file - 1, Some(tmp_tip), initial_file_count - files_per_range)
},
// Case 3: Pruning more headers than a single file has (tip reduced by
// Case 2: Pruning more headers than a single file has (tip reduced by
// blocks_per_file + 1) should result in a file set deletion
{
tmp_tip -= blocks_per_file + 1;
(blocks_per_file + 1, Some(tmp_tip), initial_file_count - files_per_range * 2)
},
// Case 4: Pruning all remaining headers from the file except the genesis header
// Case 3: Pruning all remaining headers from the file except the genesis header
{
(
tmp_tip,
Some(0), // Only genesis block remains
files_per_range + 1, // The file set with block 0 should remain
)
},
// Case 5: Pruning the genesis header (should not delete the file set with block 0)
// Case 4: Pruning the genesis header (should not delete the file set with block 0)
{
(
1,
Expand All @@ -254,15 +268,19 @@ mod tests {

let mut header_writer = sf_rw.latest_writer(StaticFileSegment::Headers).unwrap();

for (prune_count, expected_tip, expected_file_count) in test_cases {
for (case, (prune_count, expected_tip, expected_file_count)) in
test_cases.into_iter().enumerate()
{
prune_and_validate(
&mut header_writer,
&sf_rw,
&static_dir,
prune_count,
expected_tip,
expected_file_count,
);
)
.map_err(|err| eyre::eyre!("Test case {case}: {err}"))
.unwrap();
}
}
}
Expand Down

0 comments on commit ce47949

Please sign in to comment.