cuprate-hinto-janai/p2p/p2p-core/tests/handles.rs
Boog900 d5c8eba1d8
Some checks failed
CI / fmt (push) Has been cancelled
CI / typo (push) Has been cancelled
Audit / audit (push) Has been cancelled
CI / ci (macos-latest, stable, bash) (push) Has been cancelled
CI / ci (ubuntu-latest, stable, bash) (push) Has been cancelled
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Has been cancelled
Deny / audit (push) Has been cancelled
Doc / build (push) Has been cancelled
Doc / deploy (push) Has been cancelled
P2P: API Improvements (#168)
* start handshaker builder

* finish builder

* remove borsh requirement

* fix poll_ready :/

* add more docs to builder

* more docs

* fix clippy

* merge fixes

* fix doc test

* fix imports

* cargo fmt

* split `PeerRequest` and `PeerResponse` enums up.

This splits them both into a protocol and admin enum

* add request handler file

* integrate request handler into connection

* fix docs

* doc updates

* add more docs

* fix docs

* misc changes

* review fixes

* fix merge

* add dummy file

* fix docs

* Update p2p/dandelion-tower/src/router.rs

* fix docs
2024-07-04 21:05:22 +01:00

58 lines
1.6 KiB
Rust

use std::{sync::Arc, time::Duration};
use tokio::sync::Semaphore;
use cuprate_p2p_core::handles::HandleBuilder;
#[test]
fn send_ban_signal() {
let (guard, mut connection_handle) = HandleBuilder::default().build();
connection_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 (guard, mut connection_handle) = HandleBuilder::default().build();
connection_handle.ban_peer(Duration::from_secs(300));
connection_handle.ban_peer(Duration::from_secs(301));
connection_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(Some(semaphore.try_acquire_owned().unwrap()))
.build();
assert!(!connection_handle.is_closed());
drop(guard);
assert!(connection_handle.is_closed());
}