Skip to content

Commit

Permalink
Fix handler test parallelisation
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 6, 2024
1 parent c5d47bb commit 9138018
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/handler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,24 @@ struct MockService {
}

async fn build_handler<P: ProtocolIdentity>() -> (Handler, MockService) {
let config = Discv5ConfigBuilder::new(ListenConfig::default()).build();
build_handler_with_listen_config::<P>(ListenConfig::default()).await
}

async fn build_handler_with_listen_config<P: ProtocolIdentity>(
listen_config: ListenConfig,
) -> (Handler, MockService) {
let listen_port = listen_config
.ipv4_port()
.expect("listen config should default to ipv4");
let config = Discv5ConfigBuilder::new(listen_config).build();
let key = CombinedKey::generate_secp256k1();
let enr = EnrBuilder::new("v4")
.ip4(Ipv4Addr::LOCALHOST)
.udp4(9000)
.udp4(listen_port)
.build(&key)
.unwrap();
let mut listen_sockets = SmallVec::default();
listen_sockets.push((Ipv4Addr::LOCALHOST, 9000).into());
listen_sockets.push((Ipv4Addr::LOCALHOST, listen_port).into());
let node_id = enr.node_id();
let filter_expected_responses = Arc::new(RwLock::new(HashMap::new()));

Expand Down Expand Up @@ -480,7 +489,9 @@ async fn relay() {
init();

// Relay
let (mut handler, mock_service) = build_handler::<DefaultProtocolId>().await;
let listen_config = ListenConfig::default().with_ipv4(Ipv4Addr::LOCALHOST, 9901);
let (mut handler, mock_service) =
build_handler_with_listen_config::<DefaultProtocolId>(listen_config).await;
let relay_addr = handler.enr.read().udp4_socket().unwrap().into();
let relay_node_id = handler.enr.read().node_id();
let mut dummy_session = build_dummy_session();
Expand Down
36 changes: 36 additions & 0 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ pub enum ListenConfig {
},
}

impl ListenConfig {
pub fn ipv4(&self) -> Option<Ipv4Addr> {
match self {
ListenConfig::Ipv4 { ip, .. } | ListenConfig::DualStack { ipv4: ip, .. } => Some(*ip),
_ => None,
}
}

pub fn ipv6(&self) -> Option<Ipv6Addr> {
match self {
ListenConfig::Ipv6 { ip, .. } | ListenConfig::DualStack { ipv6: ip, .. } => Some(*ip),
_ => None,
}
}

pub fn ipv4_port(&self) -> Option<u16> {
match self {
ListenConfig::Ipv4 { port, .. }
| ListenConfig::DualStack {
ipv4_port: port, ..
} => Some(*port),
_ => None,
}
}

pub fn ipv6_port(&self) -> Option<u16> {
match self {
ListenConfig::Ipv6 { port, .. }
| ListenConfig::DualStack {
ipv6_port: port, ..
} => Some(*port),
_ => None,
}
}
}

/// Convenience objects for setting up the recv handler.
pub struct SocketConfig {
/// The executor to spawn the tasks.
Expand Down

0 comments on commit 9138018

Please sign in to comment.