Skip to content

Commit

Permalink
fix port range
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Sep 23, 2024
1 parent 5703ab7 commit 5685f40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions tests/bruno/environments/test.bru
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
vars {
CKB_RPC_URL: http://127.0.0.1:8114
NODE1_RPC_URL: http://127.0.0.1:41714
NODE2_RPC_URL: http://127.0.0.1:41715
NODE3_RPC_URL: http://127.0.0.1:41716
NODE1_RPC_URL: http://127.0.0.1:21714
NODE2_RPC_URL: http://127.0.0.1:21715
NODE3_RPC_URL: http://127.0.0.1:21716
NODE1_ADDR: /ip4/127.0.0.1/tcp/8344/p2p/QmbvRjJHAQDmj3cgnUBGQ5zVnGxUKwb2qJygwNs2wk41h8
NODE2_ADDR: /ip4/127.0.0.1/tcp/8345/p2p/QmSRcPqUn4aQrKHXyCDjGn2qBVf43tWBDS2Wj9QDUZXtZp
NODE3_ADDR: /ip4/127.0.0.1/tcp/8346/p2p/QmaFDJb9CkMrXy7nhTWBY5y9mvuykre3EzzRsCJUAVXprZ
Expand Down
20 changes: 17 additions & 3 deletions tests/deploy/udt-init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ckb_types::{packed::CellDep, prelude::Builder};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::net::TcpListener;

use std::{error::Error as StdErr, str::FromStr};

Expand Down Expand Up @@ -198,13 +199,26 @@ struct UdtInfos {
infos: Vec<UdtInfo>,
}

fn is_port_available(port: u16) -> bool {
match TcpListener::bind(("127.0.0.1", port)) {
Ok(listener) => {
drop(listener); // Close the listener
true
}
Err(_) => false,
}
}

fn generate_ports(num_ports: usize) -> Vec<u16> {
let mut ports = HashSet::new();
let mut rng = rand::thread_rng();

while ports.len() < num_ports {
let port: u16 = rng.gen_range(1024..=65535);
ports.insert(port);
// avoid https://en.wikipedia.org/wiki/Ephemeral_port
let port: u16 = rng.gen_range(1024..32768);
if is_port_available(port) {
ports.insert(port);
}
}

ports.into_iter().collect()
Expand Down Expand Up @@ -246,7 +260,7 @@ fn genrate_nodes_config() {
for (i, config_dir) in config_dirs.iter().enumerate() {
let use_gen_port = on_github_action && i != 0;
let default_fiber_port = (8343 + i) as u16;
let default_rpc_port = (41713 + i) as u16;
let default_rpc_port = (21713 + i) as u16;
let (fiber_port, rpc_port) = if use_gen_port {
(*ports_iter.next().unwrap(), *ports_iter.next().unwrap())
} else {
Expand Down

0 comments on commit 5685f40

Please sign in to comment.