Skip to content

Commit

Permalink
integration test of network state change
Browse files Browse the repository at this point in the history
  • Loading branch information
int88 committed Feb 19, 2025
1 parent 29f4ca2 commit b21d28d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/net/network-types/src/peers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ impl PeersConfig {
self
}

/// Configure how long to refill outbound slots
pub const fn with_refill_slots_interval(mut self, interval: Duration) -> Self {
self.refill_slots_interval = interval;
self
}

/// Maximum allowed outbound connections.
pub const fn with_max_outbound(mut self, max_outbound: usize) -> Self {
self.connection_info.max_outbound = max_outbound;
Expand Down
42 changes: 42 additions & 0 deletions crates/net/network/tests/it/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,48 @@ async fn test_trusted_peer_only() {
assert_eq!(handle.num_connected_peers(), 2);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_network_state_change() {
let net = Testnet::create(1).await;
let secret_key = SecretKey::new(&mut rand::thread_rng());
let peers_config =
PeersConfig::default().with_refill_slots_interval(Duration::from_millis(500));

let config = NetworkConfigBuilder::eth(secret_key)
.listener_port(0)
.disable_discovery()
.peer_config(peers_config)
.build(NoopProvider::default());

let network = NetworkManager::new(config).await.unwrap();

let handle = network.handle().clone();
tokio::task::spawn(network);

let mut handles = net.handles();
let handle0 = handles.next().unwrap();

drop(handles);
let _handle = net.spawn();

// Set network state to Hibernate.
handle.set_network_hibernate();

handle.add_peer(*handle0.peer_id(), handle0.local_addr());

// wait 2 seconds, the number of connections is still 0, because network is Hibernate.
tokio::time::sleep(Duration::from_secs(2)).await;
assert_eq!(handle.num_connected_peers(), 0);

// Set network state to Active.
handle.set_network_active();

// wait 2 seconds, the number of connections should be 1, because network is Active and outbound
// slot should be filled.
tokio::time::sleep(Duration::from_secs(2)).await;
assert_eq!(handle.num_connected_peers(), 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_exceed_outgoing_connections() {
let net = Testnet::create(2).await;
Expand Down

0 comments on commit b21d28d

Please sign in to comment.