Skip to content

Commit

Permalink
chore(rust): used random ports to avoid port clash in ockam examples
Browse files Browse the repository at this point in the history
  • Loading branch information
PsychoPunkSage authored and adrianbenavides committed Aug 1, 2024
1 parent f2156b7 commit d6ad83e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 8 additions & 10 deletions examples/rust/tcp_inlet_and_outlet/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use example_test_helper::{CmdBuilder, Error};
use ockam::compat::rand;
use ockam::compat::rand::Rng;
use example_test_helper::{find_available_port, CmdBuilder, Error};

#[test]
fn run_01_inlet_outlet_one_process() -> Result<(), Error> {
let port = rand::thread_rng().gen_range(10000..65535);
let port = find_available_port();
// Spawn example, wait for it to start up
let runner = CmdBuilder::new(&format!(
"cargo run --locked --example 01-inlet-outlet 127.0.0.1:{port} ockam.io:80"
Expand All @@ -24,8 +22,8 @@ fn run_01_inlet_outlet_one_process() -> Result<(), Error> {

#[test]
fn run_02_inlet_outlet_separate_processes() -> Result<(), Error> {
let routing_port = rand::thread_rng().gen_range(10000..65535);
let inlet_port = rand::thread_rng().gen_range(10000..65535);
let routing_port = find_available_port();
let inlet_port = find_available_port();
// Spawn outlet, wait for it to start up
let outlet = CmdBuilder::new(&format!(
"cargo run --locked --example 02-outlet ockam.io:80 {routing_port}"
Expand Down Expand Up @@ -55,8 +53,8 @@ fn run_02_inlet_outlet_separate_processes() -> Result<(), Error> {
#[test]
#[ignore]
fn run_03_inlet_outlet_separate_processes_secure_channel() -> Result<(), Error> {
let routing_port = rand::thread_rng().gen_range(10000..65535);
let inlet_port = rand::thread_rng().gen_range(10000..65535);
let routing_port = find_available_port();
let inlet_port = find_available_port();
// Spawn outlet, wait for it to start up
let outlet = CmdBuilder::new(&format!(
"cargo run --locked --example 03-outlet ockam.io:80 {routing_port}"
Expand Down Expand Up @@ -85,8 +83,8 @@ fn run_03_inlet_outlet_separate_processes_secure_channel() -> Result<(), Error>

#[test]
#[ignore]
fn run_04_inlet_outlet_seperate_processes_secure_channel_via_ockam_orchestrator() -> Result<(), Error> {
let port = rand::thread_rng().gen_range(10000..65535);
fn run_04_inlet_outlet_separate_processes_secure_channel_via_ockam_orchestrator() -> Result<(), Error> {
let port = find_available_port();
// Spawn outlet, wait for it to start up, grab dynamic forwarding address
let outlet = CmdBuilder::new("cargo run --locked --example 04-outlet ockam.io:80").spawn()?;
outlet.match_stdout(r"(?i)RemoteRelay was created on the node")?;
Expand Down
7 changes: 7 additions & 0 deletions tools/docs/example_test_helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use cfg_if::cfg_if;
use duct::unix::HandleExt;
use duct::{cmd, Handle};
use regex::Regex;
use std::net::TcpListener;
use std::time::SystemTime;
use std::{fs, io};
use std::{fs::File, io::Read, thread::sleep, time::Duration};
Expand Down Expand Up @@ -326,6 +327,12 @@ impl CmdRunner {
}
}

pub fn find_available_port() -> u16 {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to address");
let address = listener.local_addr().expect("Failed to get local address");
address.port()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d6ad83e

Please sign in to comment.