mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
test: ensure port is unused for udp and tcp (#2731)
This commit is contained in:
@ -19,28 +19,39 @@ pub fn enr_to_peer_id(enr: Enr<SigningKey>) -> PeerId {
|
||||
/// Does not guarantee that the given port is unused after the function exists, just that it was
|
||||
/// unused before the function started (i.e., it does not reserve a port).
|
||||
pub fn unused_port() -> u16 {
|
||||
unused_tcp_addr().port()
|
||||
}
|
||||
|
||||
/// Finds an unused tcp address
|
||||
pub fn unused_tcp_addr() -> SocketAddr {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0")
|
||||
.expect("Failed to create TCP listener to find unused port");
|
||||
listener.local_addr().expect("Failed to read TCP listener local_addr to find unused port")
|
||||
}
|
||||
|
||||
let local_addr =
|
||||
listener.local_addr().expect("Failed to read TCP listener local_addr to find unused port");
|
||||
local_addr.port()
|
||||
/// Finds an unused udp port
|
||||
pub fn unused_udp_port() -> u16 {
|
||||
unused_udp_addr().port()
|
||||
}
|
||||
/// Finds an unused udp address
|
||||
pub fn unused_udp_addr() -> SocketAddr {
|
||||
let udp_listener = std::net::UdpSocket::bind("127.0.0.1:0")
|
||||
.expect("Failed to create UDP listener to find unused port");
|
||||
udp_listener.local_addr().expect("Failed to read UDP listener local_addr to find unused port")
|
||||
}
|
||||
|
||||
/// Finds a single port that is unused for both TCP and UDP.
|
||||
pub fn unused_tcp_and_udp_port() -> u16 {
|
||||
loop {
|
||||
let port = unused_port();
|
||||
if std::net::UdpSocket::bind(format!("127.0.0.1:{port}")).is_ok() {
|
||||
return port
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates two unused SocketAddrs, intended for use as the p2p (TCP) and discovery ports (UDP) for
|
||||
/// new reth instances.
|
||||
pub fn unused_tcp_udp() -> (SocketAddr, SocketAddr) {
|
||||
let tcp_listener = std::net::TcpListener::bind("127.0.0.1:0")
|
||||
.expect("Failed to create TCP listener to find unused port");
|
||||
let tcp_addr = tcp_listener
|
||||
.local_addr()
|
||||
.expect("Failed to read TCP listener local_addr to find unused port");
|
||||
|
||||
let udp_listener = std::net::UdpSocket::bind("127.0.0.1:0")
|
||||
.expect("Failed to create UDP listener to find unused port");
|
||||
let udp_addr = udp_listener
|
||||
.local_addr()
|
||||
.expect("Failed to read UDP listener local_addr to find unused port");
|
||||
|
||||
(tcp_addr, udp_addr)
|
||||
(unused_tcp_addr(), unused_udp_addr())
|
||||
}
|
||||
|
||||
@ -5,5 +5,8 @@
|
||||
mod init;
|
||||
mod testnet;
|
||||
|
||||
pub use init::{enr_to_peer_id, unused_port, unused_tcp_udp, GETH_TIMEOUT};
|
||||
pub use init::{
|
||||
enr_to_peer_id, unused_port, unused_tcp_addr, unused_tcp_and_udp_port, unused_tcp_udp,
|
||||
unused_udp_addr, unused_udp_port, GETH_TIMEOUT,
|
||||
};
|
||||
pub use testnet::{NetworkEventStream, PeerConfig, Testnet};
|
||||
|
||||
Reference in New Issue
Block a user