Skip to content

Commit

Permalink
add cache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron committed Sep 28, 2023
1 parent 2806a9f commit 7bf1dba
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions crates/hotshot/src/traits/networking/combined_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl Cache {
fn contains(&self, hash: u64) -> bool {
self.cache.contains(&hash)
}

/// Get the number of items in the cache
#[cfg(test)]
fn len(&self) -> usize {
self.cache.len()
}
}

/// Helper function to calculate a hash of a type that implements Hash
Expand Down Expand Up @@ -406,3 +412,76 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, MEMBERSHIP: Membership<TYPES
Box::new(move |network| CombinedCommChannel::new(network))
}
}

#[cfg(test)]
mod test {
use crate::block_impl::VIDTransaction;

use super::*;
use tracing::instrument;

/// cache eviction test
#[cfg_attr(
async_executor_impl = "tokio",
tokio::test(flavor = "multi_thread", worker_threads = 2)
)]
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
#[instrument]
async fn test_cache_eviction() {
let mut cache = Cache::new(3);
cache.insert(1);
cache.insert(2);
cache.insert(3);
cache.insert(4);
assert_eq!(cache.cache.len(), 3);
assert_eq!(cache.hashes.len(), 3);
assert!(!cache.cache.contains(&1));
assert!(cache.cache.contains(&2));
assert!(cache.cache.contains(&3));
assert!(cache.cache.contains(&4));
assert!(!cache.hashes.contains(&1));
assert!(cache.hashes.contains(&2));
assert!(cache.hashes.contains(&3));
assert!(cache.hashes.contains(&4));
}

#[cfg_attr(
async_executor_impl = "tokio",
tokio::test(flavor = "multi_thread", worker_threads = 2)
)]
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
#[instrument]
async fn test_hash_calculation() {
let message1 = VIDTransaction(vec![0; 32]);
let message2 = VIDTransaction(vec![1; 32]);

assert_eq!(calculate_hash_of(&message1), calculate_hash_of(&message1));
assert_ne!(calculate_hash_of(&message1), calculate_hash_of(&message2));
}

#[cfg_attr(
async_executor_impl = "tokio",
tokio::test(flavor = "multi_thread", worker_threads = 2)
)]
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
#[instrument]
async fn test_cache_integrity() {
let message1 = VIDTransaction(vec![0; 32]);
let message2 = VIDTransaction(vec![1; 32]);

let mut cache = Cache::new(3);

// test insertion integrity
cache.insert(calculate_hash_of(&message1));
cache.insert(calculate_hash_of(&message2));

assert!(cache.contains(calculate_hash_of(&message1)));
assert!(cache.contains(calculate_hash_of(&message2)));

// check that the cache is not modified on duplicate entries
cache.insert(calculate_hash_of(&message1));
assert!(cache.contains(calculate_hash_of(&message1)));
assert!(cache.contains(calculate_hash_of(&message2)));
assert_eq!(cache.len(), 2);
}
}

0 comments on commit 7bf1dba

Please sign in to comment.