Skip to content

Commit 910954b

Browse files
committed
test(p2p): update testing doc
1 parent cd92ff2 commit 910954b

File tree

2 files changed

+74
-66
lines changed

2 files changed

+74
-66
lines changed

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

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -357,70 +357,6 @@ impl ConnectToInitialPeers {
357357
}
358358
}
359359

360-
/// Node should repeat connecting to unavailable initial peer.
361-
#[derive(documented::Documented, Default, Clone, Copy)]
362-
pub struct ConnectToUnavailableInitialPeers;
363-
364-
impl ConnectToUnavailableInitialPeers {
365-
pub async fn run<'cluster>(self, runner: ClusterRunner<'cluster>) {
366-
const MAX: u16 = 2;
367-
const RETRIES: u8 = 3;
368-
369-
let mut driver = Driver::new(runner);
370-
371-
let (initial_peers, peer_ids): (Vec<_>, Vec<_>) = (0..MAX)
372-
.into_iter()
373-
.map(|i| {
374-
let port: u16 = 11200 + i;
375-
let peer_id = SecretKey::rand().public_key().peer_id();
376-
let addr = format!(
377-
"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}",
378-
peer_id = peer_id.clone().to_libp2p_string()
379-
)
380-
.parse()
381-
.unwrap();
382-
(addr, peer_id)
383-
})
384-
.unzip();
385-
386-
let (node_ut, _) = driver
387-
.add_rust_node(RustNodeTestingConfig::berkeley_default().initial_peers(initial_peers));
388-
389-
let mut peer_retries =
390-
BTreeMap::from_iter(peer_ids.into_iter().map(|peer_id| (peer_id, 0_u8)));
391-
392-
// matches event "the node established connection with peer"
393-
let pred = |node_id, event: &_, _state: &_| {
394-
if node_id != node_ut {
395-
false
396-
} else if let Some((peer_id, res)) = as_connection_finalized_event(event) {
397-
assert!(res.is_err(), "connection to {peer_id} should succeed");
398-
let retries = peer_retries.get_mut(&peer_id).unwrap();
399-
*retries += 1;
400-
if *retries >= RETRIES {
401-
peer_retries.remove(&peer_id);
402-
}
403-
peer_retries.is_empty()
404-
} else {
405-
false
406-
}
407-
};
408-
409-
let satisfied = driver
410-
.run_until(Duration::from_secs(1 * 60), pred)
411-
.await
412-
.unwrap();
413-
414-
println!("{:#?}", driver.inner().node(node_ut).unwrap().state().p2p);
415-
416-
assert!(
417-
satisfied,
418-
"did not reach retry limit for peers: {:?}",
419-
peer_retries
420-
);
421-
}
422-
}
423-
424360
/// Node should be able to connect to all initial peers after they become ready.
425361
#[derive(documented::Documented, Default, Clone, Copy)]
426362
pub struct ConnectToInitialPeersBecomeReady;
@@ -488,3 +424,67 @@ impl ConnectToInitialPeersBecomeReady {
488424
assert!(satisfied, "did not connect to peers: {:?}", peer_ids);
489425
}
490426
}
427+
428+
/// Node should repeat connecting to unavailable initial peer.
429+
#[derive(documented::Documented, Default, Clone, Copy)]
430+
pub struct ConnectToUnavailableInitialPeers;
431+
432+
impl ConnectToUnavailableInitialPeers {
433+
pub async fn run<'cluster>(self, runner: ClusterRunner<'cluster>) {
434+
const MAX: u16 = 2;
435+
const RETRIES: u8 = 3;
436+
437+
let mut driver = Driver::new(runner);
438+
439+
let (initial_peers, peer_ids): (Vec<_>, Vec<_>) = (0..MAX)
440+
.into_iter()
441+
.map(|i| {
442+
let port: u16 = 11200 + i;
443+
let peer_id = SecretKey::rand().public_key().peer_id();
444+
let addr = format!(
445+
"/ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}",
446+
peer_id = peer_id.clone().to_libp2p_string()
447+
)
448+
.parse()
449+
.unwrap();
450+
(addr, peer_id)
451+
})
452+
.unzip();
453+
454+
let (node_ut, _) = driver
455+
.add_rust_node(RustNodeTestingConfig::berkeley_default().initial_peers(initial_peers));
456+
457+
let mut peer_retries =
458+
BTreeMap::from_iter(peer_ids.into_iter().map(|peer_id| (peer_id, 0_u8)));
459+
460+
// matches event "the node established connection with peer"
461+
let pred = |node_id, event: &_, _state: &_| {
462+
if node_id != node_ut {
463+
false
464+
} else if let Some((peer_id, res)) = as_connection_finalized_event(event) {
465+
assert!(res.is_err(), "connection to {peer_id} should succeed");
466+
let retries = peer_retries.get_mut(&peer_id).unwrap();
467+
*retries += 1;
468+
if *retries >= RETRIES {
469+
peer_retries.remove(&peer_id);
470+
}
471+
peer_retries.is_empty()
472+
} else {
473+
false
474+
}
475+
};
476+
477+
let satisfied = driver
478+
.run_until(Duration::from_secs(1 * 60), pred)
479+
.await
480+
.unwrap();
481+
482+
println!("{:#?}", driver.inner().node(node_ut).unwrap().state().p2p);
483+
484+
assert!(
485+
satisfied,
486+
"did not reach retry limit for peers: {:?}",
487+
peer_retries
488+
);
489+
}
490+
}

p2p/testing.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
If two nodes are connecting to each other at the same time, they should be
88
connected, so each one has exactly one connection.
99

10-
- - [`p2p_basic_connections(simultaneous_connections)`](../node/testing/src/scenarios/p2p/basic_connection_handling.rs#L25)
10+
- [`p2p_basic_connections(simultaneous_connections)`](../node/testing/src/scenarios/p2p/basic_connection_handling.rs#L25)
11+
12+
### Two Rust nodes shouldn't be disconnected for a while after they are connected
13+
14+
- [`p2p_basic_connections(connection_stbility)`](../node/testing/src/scenarios/p2p/basic_connection_handling.rs#L342)
1115

1216

1317
### All connections should be tracked by the state machine
@@ -22,7 +26,7 @@ should be present in the state machine.
2226
### Number of active peers should not exceed configured maximum number
2327

2428
**Tests:**
25-
- [`p2p_basic_connections(max_number_of_peers)`](../node/testing/src/scenarios/p2p/basic_connection_handling.rs#L242)
29+
- [`p2p_basic_connections(max_number_of_peers)`](../node/testing/src/scenarios/p2p/basic_connection_handling.rs#L248)
2630

2731

2832
## Incoming Connections
@@ -78,6 +82,10 @@ TODO: what if the number of initial peers exceeds the max number of peers?
7882
- [`p2p_basic_outgoing(connect_to_all_initial_peers)`](../node/testing/src/scenarios/p2p/basic_outgoing_connections.rs#L293)
7983
- [multi_node_initial_joining](../node/testing/src/scenarios/multi_node/basic_connectivity_initial_joining.rs) (partially?)
8084

85+
### Node should retry connecting to unavailable initial peers
86+
87+
- [`p2p_basic_outgoing(connect_to_unavailableinitial_peers)`](../node/testing/src/scenarios/p2p/basic_outgoing_connections.rs#L433)
88+
8189
### Node should be able to connect to initial peers eventually, even if initially they are not available.
8290

8391
If, for some reason, the node can't connect to enough peers (e.g. it is the first

0 commit comments

Comments
 (0)