Skip to content

Commit

Permalink
chore: tunnel test get random port pair randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Nov 30, 2024
1 parent b617e85 commit 23e6538
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tests/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ use shadowsocks_service::{
run_local, run_server,
};

fn random_local_tcp_port() -> u16 {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.local_addr().unwrap().port()
fn random_local_tcp_port_pair() -> (u16, u16) {
let listener1 = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port1 = listener1.local_addr().unwrap().port();

let listener2 = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port2 = listener2.local_addr().unwrap().port();

(port1, port2)
}

#[tokio::test]
async fn tcp_tunnel() {
let _ = env_logger::try_init();

let local_port = random_local_tcp_port();
let server_port = random_local_tcp_port();
let (local_port, server_port) = random_local_tcp_port_pair();
let local_config = Config::load_from_str(
&format!(
r#"{{
Expand Down Expand Up @@ -101,8 +105,7 @@ async fn udp_tunnel() {

time::sleep(Duration::from_secs(1)).await;

let local_port = random_local_tcp_port();
let server_port = random_local_tcp_port();
let (local_port, server_port) = random_local_tcp_port_pair();
let local_config = Config::load_from_str(
&format!(
r#"{{
Expand Down

0 comments on commit 23e6538

Please sign in to comment.