#![allow(clippy::unwrap_used, clippy::expect_used)]
mod support;
use ant_core::data::{Client, ClientConfig};
use bytes::Bytes;
use serial_test::serial;
use std::sync::Arc;
use support::MiniTestnet;
const BOOTSTRAP_CACHE_TEST_NODES: usize = 12;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn test_bootstrap_cache_grows_after_client_activity() {
let testnet = MiniTestnet::start(BOOTSTRAP_CACHE_TEST_NODES).await;
let node = testnet.node(3).expect("Node 3 should exist");
let client = Client::from_node(Arc::clone(&node), ClientConfig::default())
.with_wallet(testnet.wallet().clone());
let before = node.cached_peer_count().await;
let content = Bytes::from("bootstrap-cache e2e payload");
let address = client
.chunk_put(content.clone())
.await
.expect("chunk_put should succeed with payment");
let retrieved = client
.chunk_get(&address)
.await
.expect("chunk_get should succeed")
.expect("chunk should be retrievable");
assert_eq!(retrieved.content.as_ref(), content.as_ref());
let after = node.cached_peer_count().await;
assert!(
after > before,
"cache should grow after peer interactions: before={before} after={after}"
);
drop(client);
testnet.teardown().await;
}
#[tokio::test]
async fn test_bootstrap_cache_roundtrip_through_disk() {
use saorsa_core::{BootstrapConfig, BootstrapManager};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
let cache_dir = tempfile::TempDir::new().expect("create temp cache dir");
let config = BootstrapConfig {
cache_dir: cache_dir.path().to_path_buf(),
..BootstrapConfig::default()
};
let peer_count = 15;
{
let mgr = BootstrapManager::with_config(config.clone())
.await
.expect("construct populating BootstrapManager");
for i in 0..peer_count {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, i as u8 + 1)), 9000);
mgr.add_peer_trusted(&addr, vec![addr]).await;
}
assert_eq!(mgr.peer_count().await, peer_count, "in-memory populate");
mgr.save()
.await
.expect("save should succeed above threshold");
}
let reloaded = BootstrapManager::with_config(config)
.await
.expect("construct reloading BootstrapManager");
let reloaded_count = reloaded.peer_count().await;
assert_eq!(
reloaded_count, peer_count,
"all {peer_count} peers should reload from disk, got {reloaded_count}"
);
}