Skip to content

Commit

Permalink
test: move from block output cache to lru cache (#29)
Browse files Browse the repository at this point in the history
* test: move from block output cache to lru cache

* chore: rename config toml

* ci: fix tarpaulin

* ci: install tarpaulin force
  • Loading branch information
rafaelcr authored Jul 11, 2024
1 parent f019623 commit 2c24a91
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
File renamed without changes.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ jobs:
run: |
rustup update
- name: Install and run cargo-tarpaulin
- name: Run tests
run: |
cargo install cargo-tarpaulin
cargo --version
cargo install --force cargo-tarpaulin
cargo tarpaulin --out lcov -- --test-threads=1
- name: Upload coverage reports to Codecov
Expand Down
49 changes: 48 additions & 1 deletion src/db/cache/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ pub async fn input_rune_balances_from_tx_inputs(
}

/// Moves data from the current block's output cache to the long-term LRU output cache. Clears the block output cache when done.
///
/// # Arguments
///
/// * `block_output_cache` - Block output cache
/// * `output_cache` - Output LRU cache
pub fn move_block_output_cache_to_output_cache(
block_output_cache: &mut HashMap<(String, u32), HashMap<RuneId, Vec<InputRuneBalance>>>,
output_cache: &mut LruCache<(String, u32), HashMap<RuneId, Vec<InputRuneBalance>>>,
Expand Down Expand Up @@ -692,7 +697,9 @@ mod test {
use crate::db::{
cache::{
input_rune_balance::InputRuneBalance, utils::input_rune_balances_from_tx_inputs,
}, models::{db_ledger_entry::DbLedgerEntry, db_ledger_operation::DbLedgerOperation}, pg_insert_ledger_entries, pg_test_client, pg_test_roll_back_migrations
},
models::{db_ledger_entry::DbLedgerEntry, db_ledger_operation::DbLedgerOperation},
pg_insert_ledger_entries, pg_test_client, pg_test_roll_back_migrations,
};

#[tokio::test]
Expand Down Expand Up @@ -888,4 +895,44 @@ mod test {
assert_eq!(results.len(), 0);
}
}

mod cache_move {
use std::num::NonZeroUsize;

use lru::LruCache;
use maplit::hashmap;
use ordinals::RuneId;

use crate::db::cache::{
input_rune_balance::InputRuneBalance, utils::move_block_output_cache_to_output_cache,
};

#[test]
fn moves_to_lru_output_cache_and_clears() {
let rune_id = RuneId::new(840000, 25).unwrap();
let mut block_output_cache = hashmap! {
("045fe33f1174d6a72084e751735a89746a259c6d3e418b65c03ec0740f924c7b"
.to_string(), 1) => hashmap! {
rune_id => vec![InputRuneBalance { address: None, amount: 2000 }]
}
};
let mut output_cache = LruCache::new(NonZeroUsize::new(1).unwrap());

move_block_output_cache_to_output_cache(&mut block_output_cache, &mut output_cache);

let moved_val = output_cache
.get(&(
"045fe33f1174d6a72084e751735a89746a259c6d3e418b65c03ec0740f924c7b".to_string(),
1,
))
.unwrap();
assert_eq!(moved_val.len(), 1);
let balances = moved_val.get(&rune_id).unwrap();
assert_eq!(balances.len(), 1);
let balance = balances.get(0).unwrap();
assert_eq!(balance.address, None);
assert_eq!(balance.amount, 2000);
assert_eq!(block_output_cache.len(), 0);
}
}
}

0 comments on commit 2c24a91

Please sign in to comment.