Skip to content

Commit cd92ff2

Browse files
committed
test(p2p): add connection stability test
1 parent 805ad6d commit cd92ff2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

node/testing/src/scenarios/p2p/basic_connection_handling.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::time::Duration;
33
use node::{
44
event_source::Event,
55
p2p::{
6-
connection::outgoing::P2pConnectionOutgoingInitOpts, P2pEvent, P2pPeerState, P2pPeerStatus,
7-
P2pState, PeerId,
6+
connection::outgoing::P2pConnectionOutgoingInitOpts, P2pConnectionEvent, P2pEvent,
7+
P2pPeerState, P2pPeerStatus, P2pState, PeerId,
88
},
99
};
1010

@@ -131,7 +131,10 @@ impl AllNodesConnectionsAreSymmetric {
131131
.await
132132
.unwrap()
133133
{
134-
assert!(std::time::Instant::now() < timeout, "cluster should stop generating events");
134+
assert!(
135+
std::time::Instant::now() < timeout,
136+
"cluster should stop generating events"
137+
);
135138
}
136139

137140
// Check that for each peer, if it is in the node's peer list, then the node is in the peer's peer list
@@ -324,7 +327,51 @@ impl MaxNumberOfPeers {
324327
.filter_map(|peer| peer.state().p2p.peers.get(&nut_peer_id))
325328
.filter(|state| matches!(state.status, P2pPeerStatus::Ready(..)))
326329
.count();
327-
assert!(peers_connected <= MAX.into(), "peers connections to the node exceed the max number of connections: {peers_connected}");
330+
assert!(
331+
peers_connected <= MAX.into(),
332+
"peers connections to the node exceed the max number of connections: {peers_connected}"
333+
);
334+
}
335+
}
336+
337+
/// Two nodes should stay connected for a long period of time.
338+
#[derive(documented::Documented, Default, Clone, Copy)]
339+
pub struct ConnectionStability;
340+
341+
impl ConnectionStability {
342+
pub async fn run<'cluster>(self, runner: ClusterRunner<'cluster>) {
343+
const CONNECTED_TIME_SEC: u64 = 1 * 60;
344+
let mut driver = Driver::new(runner);
345+
346+
let (node1, _) = driver.add_rust_node(RustNodeTestingConfig::berkeley_default().max_peers(1));
347+
let (node2, _) = driver.add_rust_node(RustNodeTestingConfig::berkeley_default().max_peers(1));
348+
349+
assert!(
350+
wait_for_nodes_listening_on_localhost(&mut driver, Duration::from_secs(30), [node2])
351+
.await
352+
.unwrap(),
353+
"nodes should be listening"
354+
);
355+
356+
driver
357+
.exec_step(crate::scenario::ScenarioStep::ConnectNodes {
358+
dialer: node1,
359+
listener: crate::scenario::ListenerNode::Rust(node2),
360+
})
361+
.await
362+
.expect("connect event should be dispatched");
363+
364+
// Run the cluster while there are events
365+
let disconnected = driver
366+
.run_until(Duration::from_secs(CONNECTED_TIME_SEC), |_, event, _| {
367+
matches!(
368+
event,
369+
Event::P2p(P2pEvent::Connection(P2pConnectionEvent::Closed(_)))
370+
)
371+
})
372+
.await
373+
.unwrap();
328374

375+
assert!(!disconnected, "there shouldn't be a disconnection");
329376
}
330377
}

node/testing/tests/p2p_basic_connections.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use openmina_node_testing::scenarios::p2p::basic_connection_handling::{
2-
AllNodesConnectionsAreSymmetric, MaxNumberOfPeers, SeedConnectionsAreSymmetric,
3-
SimultaneousConnections,
2+
AllNodesConnectionsAreSymmetric, ConnectionStability, MaxNumberOfPeers,
3+
SeedConnectionsAreSymmetric, SimultaneousConnections,
44
};
55

66
mod common;
@@ -22,3 +22,8 @@ scenario_test!(
2222
);
2323

2424
scenario_test!(max_number_of_peers, MaxNumberOfPeers, MaxNumberOfPeers);
25+
scenario_test!(
26+
connection_stability,
27+
ConnectionStability,
28+
ConnectionStability
29+
);

0 commit comments

Comments
 (0)