2024-09-21 00:37:06 +00:00
|
|
|
#![expect(unused_crate_dependencies, reason = "external test module")]
|
|
|
|
|
2023-12-08 15:03:01 +00:00
|
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
|
|
|
|
use tokio::sync::Semaphore;
|
|
|
|
|
2024-06-24 01:30:47 +00:00
|
|
|
use cuprate_p2p_core::handles::HandleBuilder;
|
2023-12-08 15:03:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn send_ban_signal() {
|
2024-07-04 20:05:22 +00:00
|
|
|
let (guard, mut connection_handle) = HandleBuilder::default().build();
|
2023-12-08 15:03:01 +00:00
|
|
|
|
2024-03-20 20:58:12 +00:00
|
|
|
connection_handle.ban_peer(Duration::from_secs(300));
|
2023-12-08 15:03:01 +00:00
|
|
|
|
|
|
|
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() {
|
2024-07-04 20:05:22 +00:00
|
|
|
let (guard, mut connection_handle) = HandleBuilder::default().build();
|
2023-12-08 15:03:01 +00:00
|
|
|
|
2024-03-20 20:58:12 +00:00
|
|
|
connection_handle.ban_peer(Duration::from_secs(300));
|
|
|
|
connection_handle.ban_peer(Duration::from_secs(301));
|
|
|
|
connection_handle.ban_peer(Duration::from_secs(302));
|
2023-12-08 15:03:01 +00:00
|
|
|
|
|
|
|
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));
|
2024-03-20 20:58:12 +00:00
|
|
|
let (guard, connection_handle) = HandleBuilder::default()
|
2024-07-04 20:05:22 +00:00
|
|
|
.with_permit(Some(semaphore.try_acquire_owned().unwrap()))
|
2023-12-08 15:03:01 +00:00
|
|
|
.build();
|
|
|
|
|
|
|
|
assert!(!connection_handle.is_closed());
|
|
|
|
drop(guard);
|
|
|
|
assert!(connection_handle.is_closed());
|
|
|
|
}
|