|
1 |
| -use std::{collections::BTreeSet, time::Duration}; |
| 1 | +use std::{ |
| 2 | + collections::{BTreeMap, BTreeSet}, |
| 3 | + time::Duration, |
| 4 | +}; |
2 | 5 |
|
3 | 6 | use node::p2p::{identity::SecretKey, P2pPeerState, P2pPeerStatus, PeerId};
|
4 | 7 |
|
@@ -354,6 +357,70 @@ impl ConnectToInitialPeers {
|
354 | 357 | }
|
355 | 358 | }
|
356 | 359 |
|
| 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 | + |
357 | 424 | /// Node should be able to connect to all initial peers after they become ready.
|
358 | 425 | #[derive(documented::Documented, Default, Clone, Copy)]
|
359 | 426 | pub struct ConnectToInitialPeersBecomeReady;
|
@@ -384,7 +451,10 @@ impl ConnectToInitialPeersBecomeReady {
|
384 | 451 | let (node_ut, _) = driver
|
385 | 452 | .add_rust_node(RustNodeTestingConfig::berkeley_default().initial_peers(initial_peers));
|
386 | 453 |
|
387 |
| - driver.wait_for(Duration::from_secs(10), |_, _, _| false).await.unwrap(); |
| 454 | + driver |
| 455 | + .wait_for(Duration::from_secs(10), |_, _, _| false) |
| 456 | + .await |
| 457 | + .unwrap(); |
388 | 458 |
|
389 | 459 | let (_peers, mut peer_ids): (Vec<ClusterNodeId>, BTreeSet<PeerId>) = port_bytes
|
390 | 460 | .into_iter()
|
|
0 commit comments