Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(p2p): remove flaky test #1835

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 0 additions & 125 deletions crates/p2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,131 +578,6 @@ async fn duplicate_connection() {
assert!(peer1.connected().await.contains_key(&peer2.peer_id));
}

/// Test that each peer accepts at most one connection from any other peer, and duplicate
/// connections are closed.
#[test_log::test(tokio::test)]
async fn max_inbound_connections() {
const CONNECTION_TIMEOUT: Duration = Duration::from_millis(50);
let cfg = Config {
direct_connection_timeout: CONNECTION_TIMEOUT,
relay_connection_timeout: Duration::from_secs(0),
ip_whitelist: vec!["::1/0".parse().unwrap(), "0.0.0.0/0".parse().unwrap()],
max_inbound_direct_peers: 2,
max_inbound_relayed_peers: 0,
max_outbound_peers: 10,
// Don't open connections automatically.
low_watermark: 0,
bootstrap: BootstrapConfig {
period: Duration::from_millis(500),
start_offset: Duration::from_secs(10),
},
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
};
let mut peer1 = TestPeer::new(cfg.clone(), Keypair::generate_ed25519());
let mut peer2 = TestPeer::new(cfg.clone(), Keypair::generate_ed25519());
let mut peer3 = TestPeer::new(cfg.clone(), Keypair::generate_ed25519());
let mut peer4 = TestPeer::new(cfg, Keypair::generate_ed25519());

let addr1 = peer1.start_listening().await.unwrap();
tracing::info!(%peer1.peer_id, %addr1);
let addr4 = peer4.start_listening().await.unwrap();
tracing::info!(%peer4.peer_id, %addr4);

// Open the connection.
peer2
.client
.dial(peer1.peer_id, addr1.clone())
.await
.unwrap();

wait_for_event(&mut peer1.event_receiver, |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer2.peer_id => {
Some(())
}
_ => None,
})
.await;

wait_for_event(&mut peer2.event_receiver, |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer1.peer_id => {
Some(())
}
_ => None,
})
.await;

// Ensure that the connection timeout has passed, so this is not the reason why the connection
// would be closed.
tokio::time::sleep(CONNECTION_TIMEOUT).await;

// Open another inbound connection to the peer. Since the limit is 2, this is allowed.
peer3
.client
.dial(peer1.peer_id, addr1.clone())
.await
.unwrap();

wait_for_event(&mut peer1.event_receiver, |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer3.peer_id => {
Some(())
}
_ => None,
})
.await;

wait_for_event(&mut peer3.event_receiver, |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer1.peer_id => {
Some(())
}
_ => None,
})
.await;

// Ensure that the connection timeout has passed, so this is not the reason why the connection
// would be closed.
tokio::time::sleep(CONNECTION_TIMEOUT).await;

// Open another inbound connection to the peer. Since the limit is 2, and there are already 2
// inbound connections, this is not allowed.
let result = peer4.client.dial(peer1.peer_id, addr1.clone()).await;
assert!(result.is_err());
assert!(peer4.connected().await.is_empty());

// The restriction does not apply to outbound connections, so peer 1 can still open a connection
// to peer 4.

let peer4_id = peer4.peer_id;
let mut peer_1_connection_established =
filter_events(peer1.event_receiver, move |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer4_id => {
Some(())
}
_ => None,
});

let peer1_id = peer1.peer_id;
let mut peer_4_connection_established =
filter_events(peer4.event_receiver, move |event| match event {
Event::Test(TestEvent::ConnectionEstablished { remote, .. }) if remote == peer1_id => {
Some(())
}
_ => None,
});

peer1
.client
.dial(peer4.peer_id, addr4.clone())
.await
.unwrap();

peer_1_connection_established.recv().await;
peer_4_connection_established.recv().await;
}

/// Ensure that outbound peers marked as not useful get evicted if new outbound connections
/// are attempted.
#[test_log::test(tokio::test)]
Expand Down
Loading