Skip to content

Commit 975ca54

Browse files
committed
test(p2p): add proper timeout to a test
1 parent 31b753c commit 975ca54

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

node/testing/src/scenarios/driver.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::Duration;
1+
use std::time::{Duration, Instant};
22

33
use libp2p::Multiaddr;
44
use node::{
@@ -348,3 +348,16 @@ where
348348
.map(|_| driver.add_rust_node_with(config.clone(), &mut f))
349349
.unzip()
350350
}
351+
352+
/// Runs cluster until there is a `quiet_dur` period of no events, returning
353+
/// `Ok(true)` in this case. If there is no such period for `timeout` period of
354+
/// time, then returns `Ok(false)`
355+
pub async fn run_until_no_events<'cluster>(driver: &mut Driver<'cluster>, quiet_dur: Duration, timeout: Duration) -> anyhow::Result<bool> {
356+
let timeout = Instant::now() + timeout;
357+
while driver.run_until(quiet_dur, |_, _, _| true).await? {
358+
if Instant::now() >= timeout {
359+
return Ok(false);
360+
}
361+
}
362+
Ok(true)
363+
}

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use node::{
1010

1111
use crate::{
1212
node::RustNodeTestingConfig,
13-
scenarios::{add_rust_nodes, wait_for_nodes_listening_on_localhost, ClusterRunner, Driver},
13+
scenarios::{
14+
add_rust_nodes, run_until_no_events, wait_for_nodes_listening_on_localhost, ClusterRunner,
15+
Driver,
16+
},
1417
};
1518

1619
fn has_active_peer(p2p_state: &P2pState, peer_id: &PeerId) -> bool {
@@ -55,11 +58,17 @@ impl SimultaneousConnections {
5558
.expect("connect event should be dispatched");
5659

5760
// Run the cluster while there are events
58-
while driver
59-
.run_until(Duration::from_secs(30), |_, _, _| true)
60-
.await
61-
.unwrap()
62-
{}
61+
let quiet = run_until_no_events(
62+
&mut driver,
63+
Duration::from_secs(30),
64+
Duration::from_secs(60),
65+
)
66+
.await
67+
.unwrap();
68+
assert!(
69+
quiet,
70+
"no quiet period with no events since nodes are connected"
71+
);
6372

6473
let p2p_state1 = &driver.inner().node(node1).unwrap().state().p2p;
6574
let p2p_state2 = &driver.inner().node(node2).unwrap().state().p2p;
@@ -125,17 +134,17 @@ impl AllNodesConnectionsAreSymmetric {
125134
.collect();
126135

127136
// Run the cluster while there are events
128-
let timeout = std::time::Instant::now() + Duration::from_secs(2 * 60);
129-
while driver
130-
.run_until(Duration::from_secs(30), |_, _, _| true)
131-
.await
132-
.unwrap()
133-
{
134-
assert!(
135-
std::time::Instant::now() < timeout,
136-
"cluster should stop generating events"
137-
);
138-
}
137+
let quiet = run_until_no_events(
138+
&mut driver,
139+
Duration::from_secs(30),
140+
Duration::from_secs(2 * 60),
141+
)
142+
.await
143+
.unwrap();
144+
assert!(
145+
quiet,
146+
"no quiet period with no events since nodes are connected"
147+
);
139148

140149
// Check that for each peer, if it is in the node's peer list, then the node is in the peer's peer list
141150
for (peer1, peer_id1) in &peers {
@@ -343,8 +352,10 @@ impl ConnectionStability {
343352
const CONNECTED_TIME_SEC: u64 = 1 * 60;
344353
let mut driver = Driver::new(runner);
345354

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));
355+
let (node1, _) =
356+
driver.add_rust_node(RustNodeTestingConfig::berkeley_default().max_peers(1));
357+
let (node2, _) =
358+
driver.add_rust_node(RustNodeTestingConfig::berkeley_default().max_peers(1));
348359

349360
assert!(
350361
wait_for_nodes_listening_on_localhost(&mut driver, Duration::from_secs(30), [node2])

0 commit comments

Comments
 (0)