mod common;
use common::*;
use deadline::deadline;
use peak_alloc::PeakAlloc;
use snarkos_node_network::PeerPoolHandling;
use snarkos_node_router::{Outbound, Routing};
use snarkos_node_tcp::protocols::{Disconnect, Handshake, OnConnect};
use snarkvm::{prelude::Rng, utilities::TestRng};
use core::time::Duration;
#[global_allocator]
static PEAK_ALLOC: PeakAlloc = PeakAlloc;
#[tokio::test]
async fn test_connection_cleanups() {
const NUM_CONNECTIONS: usize = 10;
let mut rng = TestRng::default();
let mut nodes = Vec::with_capacity(2);
for _ in 0..2 {
let node = match rng.gen_range(0..=1) {
0 => client(0, 1, &mut rng).await,
1 => prover(0, 1, &mut rng).await,
_ => unreachable!(),
};
nodes.push(node);
}
nodes[0].enable_handshake().await;
nodes[1].enable_handshake().await;
nodes[0].enable_disconnect().await;
nodes[1].enable_disconnect().await;
nodes[0].enable_on_connect().await;
nodes[1].enable_on_connect().await;
nodes[0].enable_listener().await;
nodes[1].enable_listener().await;
let mut heap_after_one_conn = None;
for i in 0..NUM_CONNECTIONS {
let _ = nodes[1].connect(nodes[0].local_ip());
let node0 = nodes[0].clone();
let node1 = nodes[1].clone();
deadline!(Duration::from_secs(3), move || node0.router().number_of_connected_peers() == 1
&& node1.router().number_of_connected_peers() == 1);
nodes[0].disconnect(nodes[1].local_ip());
nodes[1].disconnect(nodes[0].local_ip());
let node0 = nodes[0].clone();
let node1 = nodes[1].clone();
deadline!(Duration::from_secs(3), move || node0.router().number_of_connected_peers() == 0
&& node1.router().number_of_connected_peers() == 0);
if i == 0 {
heap_after_one_conn = Some(PEAK_ALLOC.current_usage());
}
}
let heap_after_loop = PEAK_ALLOC.current_usage();
assert_eq!(heap_after_one_conn.unwrap(), heap_after_loop);
}