cuprate-hinto-janai/p2p/monero-p2p/tests/handles.rs
Boog900 2c4cc1fb93
move address book to separate crate.
Also changes the address book to use the network zone trait
2023-12-08 15:03:01 +00:00

64 lines
1.8 KiB
Rust

use std::{sync::Arc, time::Duration};
use tokio::sync::Semaphore;
use monero_p2p::handles::HandleBuilder;
#[test]
fn send_ban_signal() {
let semaphore = Arc::new(Semaphore::new(5));
let (guard, mut connection_handle, mut peer_handle) = HandleBuilder::default()
.with_permit(semaphore.try_acquire_owned().unwrap())
.build();
peer_handle.ban_peer(Duration::from_secs(300));
let Some(ban_time) = connection_handle.check_should_ban() else {
panic!("ban signal not received!");
};
assert_eq!(ban_time.0, Duration::from_secs(300));
connection_handle.send_close_signal();
assert!(guard.should_shutdown());
guard.connection_closed();
assert!(connection_handle.is_closed());
}
#[test]
fn multiple_ban_signals() {
let semaphore = Arc::new(Semaphore::new(5));
let (guard, mut connection_handle, mut peer_handle) = HandleBuilder::default()
.with_permit(semaphore.try_acquire_owned().unwrap())
.build();
peer_handle.ban_peer(Duration::from_secs(300));
peer_handle.ban_peer(Duration::from_secs(301));
peer_handle.ban_peer(Duration::from_secs(302));
let Some(ban_time) = connection_handle.check_should_ban() else {
panic!("ban signal not received!");
};
// only the first will be seen
assert_eq!(ban_time.0, Duration::from_secs(300));
connection_handle.send_close_signal();
assert!(guard.should_shutdown());
guard.connection_closed();
assert!(connection_handle.is_closed());
}
#[test]
fn dropped_guard_sends_disconnect_signal() {
let semaphore = Arc::new(Semaphore::new(5));
let (guard, connection_handle, _) = HandleBuilder::default()
.with_permit(semaphore.try_acquire_owned().unwrap())
.build();
assert!(!connection_handle.is_closed());
drop(guard);
assert!(connection_handle.is_closed());
}