|
90 | 90 | rc::Rc,
|
91 | 91 | result::Result,
|
92 | 92 | sync::{
|
93 |
| - atomic::{AtomicBool, Ordering}, |
| 93 | + atomic::{AtomicBool, AtomicU16, Ordering}, |
94 | 94 | Arc, Mutex, RwLock, RwLockReadGuard,
|
95 | 95 | },
|
96 | 96 | thread::{sleep, Builder, JoinHandle},
|
@@ -2405,29 +2405,51 @@ pub struct Node {
|
2405 | 2405 | pub sockets: Sockets,
|
2406 | 2406 | }
|
2407 | 2407 |
|
| 2408 | +const NODE_BASE_PORT: u16 = 40000; |
2408 | 2409 | impl Node {
|
| 2410 | + /// create localhost node for tests |
2409 | 2411 | pub fn new_localhost() -> Self {
|
2410 | 2412 | let pubkey = solana_pubkey::new_rand();
|
2411 | 2413 | Self::new_localhost_with_pubkey(&pubkey)
|
2412 | 2414 | }
|
2413 | 2415 |
|
| 2416 | + fn localhost_port_range_for_tests() -> (u16, u16) { |
| 2417 | + static SLICE: AtomicU16 = AtomicU16::new(NODE_BASE_PORT); |
| 2418 | + let start = match std::env::var("NEXTEST_TEST_GLOBAL_SLOT") { |
| 2419 | + Ok(slot) => { |
| 2420 | + let slot: u16 = slot.parse().unwrap(); |
| 2421 | + NODE_BASE_PORT + slot * 25 |
| 2422 | + } |
| 2423 | + Err(_) => SLICE.fetch_add(100, Ordering::Relaxed), |
| 2424 | + }; |
| 2425 | + assert!(start < 65500, "ran out of port numbers!"); |
| 2426 | + (start, start + 25) |
| 2427 | + } |
| 2428 | + |
| 2429 | + /// create localhost node for tests with provided pubkey |
| 2430 | + /// unlike the public IP version, this will also bind RPC sockets. |
2414 | 2431 | pub fn new_localhost_with_pubkey(pubkey: &Pubkey) -> Self {
|
2415 |
| - let localhost_ip_addr = IpAddr::V4(Ipv4Addr::LOCALHOST); |
2416 |
| - let port_range = (1024, 65535); |
2417 |
| - let gossip_port = find_available_port_in_range(localhost_ip_addr, port_range) |
| 2432 | + let addr = IpAddr::V4(Ipv4Addr::LOCALHOST); |
| 2433 | + let port_range = Self::localhost_port_range_for_tests(); |
| 2434 | + let gossip_port = find_available_port_in_range(addr, port_range) |
2418 | 2435 | .expect("At least one open port should be available");
|
2419 | 2436 | let config = NodeConfig {
|
2420 |
| - gossip_addr: SocketAddr::new(localhost_ip_addr, gossip_port), |
| 2437 | + gossip_addr: SocketAddr::new(addr, gossip_port), |
2421 | 2438 | port_range,
|
2422 |
| - bind_ip_addr: localhost_ip_addr, |
| 2439 | + bind_ip_addr: addr, |
2423 | 2440 | public_tpu_addr: None,
|
2424 | 2441 | public_tpu_forwards_addr: None,
|
2425 | 2442 | num_tvu_receive_sockets: NonZero::new(1).unwrap(),
|
2426 | 2443 | num_tvu_retransmit_sockets: NonZero::new(1).unwrap(),
|
2427 | 2444 | num_quic_endpoints: NonZero::new(DEFAULT_QUIC_ENDPOINTS)
|
2428 | 2445 | .expect("Number of QUIC endpoints can not be zero"),
|
2429 | 2446 | };
|
2430 |
| - Self::new_with_external_ip(pubkey, config) |
| 2447 | + let mut node = Self::new_with_external_ip(pubkey, config); |
| 2448 | + let rpc_port = find_available_port_in_range(addr, port_range).unwrap(); |
| 2449 | + let rpc_pubsub_port = find_available_port_in_range(addr, port_range).unwrap(); |
| 2450 | + node.info.set_rpc((addr, rpc_port)).unwrap(); |
| 2451 | + node.info.set_rpc_pubsub((addr, rpc_pubsub_port)).unwrap(); |
| 2452 | + node |
2431 | 2453 | }
|
2432 | 2454 |
|
2433 | 2455 | fn get_gossip_port(
|
|
0 commit comments