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