Skip to content

Commit

Permalink
test(p2p): reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Sep 24, 2024
1 parent 515804f commit 78270d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 135 deletions.
7 changes: 4 additions & 3 deletions crates/p2p/src/test_utils/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ impl Config {
max_inbound_direct_peers: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
low_watermark: 10,
ip_whitelist: vec!["::/0".parse().unwrap(), "0.0.0.0/0".parse().unwrap()],
bootstrap_period: Duration::from_secs(2 * 60),
// Don't open connections automatically.
low_watermark: 0,
ip_whitelist: vec!["::1/0".parse().unwrap(), "0.0.0.0/0".parse().unwrap()],
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
Expand Down
141 changes: 9 additions & 132 deletions crates/p2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,9 @@ async fn periodic_bootstrap() {

const BOOTSTRAP_PERIOD: Duration = Duration::from_millis(500);
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
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: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
low_watermark: 3,
bootstrap_period: BOOTSTRAP_PERIOD,
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};
let mut boot = TestPeer::new(cfg.clone());
let mut peer1 = TestPeer::new(cfg.clone());
Expand Down Expand Up @@ -311,21 +298,7 @@ async fn reconnect_too_quickly() {
const CONNECTION_TIMEOUT: Duration = Duration::from_secs(1);
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: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
low_watermark: 0,
bootstrap_period: Duration::from_secs(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};

let mut peer1 = TestPeer::new(cfg.clone());
Expand Down Expand Up @@ -412,22 +385,7 @@ async fn duplicate_connection() {
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: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};
let keypair = Keypair::generate_ed25519();
let mut peer1 = TestPeer::with_keypair(keypair.clone(), cfg.clone());
Expand Down Expand Up @@ -497,23 +455,10 @@ async fn duplicate_connection() {
#[test_log::test(tokio::test)]
async fn outbound_peer_eviction() {
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
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: 2,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};

let mut peer = TestPeer::new(cfg.clone());
Expand Down Expand Up @@ -626,23 +571,10 @@ async fn outbound_peer_eviction() {
#[test_log::test(tokio::test)]
async fn inbound_peer_eviction() {
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
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: 25,
max_inbound_relayed_peers: 0,
max_outbound_peers: 100,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};

let mut peer = TestPeer::new(cfg.clone());
Expand Down Expand Up @@ -712,23 +644,10 @@ async fn inbound_peer_eviction() {
#[test_log::test(tokio::test)]
async fn evicted_peer_reconnection() {
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
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: 1000,
max_inbound_relayed_peers: 0,
max_outbound_peers: 1,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};

let mut peer1 = TestPeer::new(cfg.clone());
Expand Down Expand Up @@ -803,23 +722,8 @@ async fn evicted_peer_reconnection() {
#[test_log::test(tokio::test)]
async fn ip_whitelist() {
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
relay_connection_timeout: Duration::from_secs(0),
ip_whitelist: vec!["127.0.0.2/32".parse().unwrap()],
max_inbound_direct_peers: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};
let mut peer1 = TestPeer::new(cfg.clone());
let peer2 = TestPeer::new(cfg.clone());
Expand All @@ -836,23 +740,8 @@ async fn ip_whitelist() {

// Start another peer accepting connections from 127.0.0.1.
let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
relay_connection_timeout: Duration::from_secs(0),
ip_whitelist: vec!["127.0.0.1/32".parse().unwrap()],
max_inbound_direct_peers: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 1000,
interval: Duration::from_secs(1),
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};
let mut peer3 = TestPeer::new(cfg);

Expand All @@ -870,23 +759,11 @@ async fn rate_limit() {
const RATE_LIMIT_INTERVAL: Duration = Duration::from_secs(1);

let cfg = Config {
direct_connection_timeout: Duration::from_secs(0),
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: 10,
max_inbound_relayed_peers: 10,
max_outbound_peers: 10,
// Don't open connections automatically.
low_watermark: 0,
bootstrap_period: Duration::from_millis(500),
eviction_timeout: Duration::from_secs(15 * 60),
inbound_connections_rate_limit: RateLimit {
max: 2,
interval: RATE_LIMIT_INTERVAL,
},
kad_name: Default::default(),
stream_timeout: Duration::from_secs(10),
max_concurrent_streams: 100,
..Config::for_test()
};

let mut peer1 = TestPeer::new(cfg.clone());
Expand Down

0 comments on commit 78270d0

Please sign in to comment.