Skip to content

Commit 3110086

Browse files
ericktdjc
authored andcommitted
Run tests on a random port
Before this patch, the `server` and `custom_ca_store` tests would run on either port 1337 or 1338, but some other process could be using those ports, which would confuse the results. This changes the tests to instead use a random port in order to be more consistent.
1 parent 40bcb63 commit 3110086

2 files changed

Lines changed: 39 additions & 32 deletions

File tree

examples/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ async fn run_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
5656
let key = PrivateKeyDer::from_pem_file("examples/sample.rsa")
5757
.map_err(|e| error(format!("could not read private key file: {e}")))?;
5858

59-
println!("Starting to serve on https://{addr}");
60-
6159
// Create a TCP listener via tokio.
6260
let incoming = TcpListener::bind(&addr).await?;
61+
let addr = incoming.local_addr()?;
62+
63+
println!("Starting to serve on https://{addr}");
6364

6465
// Build TLS configuration.
6566
let mut server_config = ServerConfig::builder()

tests/tests.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::env;
2-
use std::net::TcpStream;
2+
use std::io::{BufRead, BufReader};
3+
use std::net::SocketAddr;
34
use std::path::PathBuf;
4-
use std::process::Command;
5-
use std::thread;
6-
use std::time;
5+
use std::process::{Child, Command, Stdio};
76

87
fn examples_dir() -> PathBuf {
98
let target_dir: PathBuf = env::var("CARGO_TARGET_DIR")
@@ -18,18 +17,37 @@ fn server_command() -> Command {
1817
Command::new(examples_dir().join("server"))
1918
}
2019

21-
fn client_command() -> Command {
22-
Command::new(examples_dir().join("client"))
20+
fn start_server() -> (Child, SocketAddr) {
21+
let mut srv = server_command()
22+
.arg("0")
23+
.stdout(Stdio::piped())
24+
.stderr(Stdio::inherit())
25+
.spawn()
26+
.expect("cannot run server example");
27+
28+
let stdout = srv
29+
.stdout
30+
.take()
31+
.expect("failed to get stdout");
32+
33+
let mut reader = BufReader::new(stdout);
34+
let mut line = String::new();
35+
reader
36+
.read_line(&mut line)
37+
.expect("failed to read line");
38+
39+
let addr = line
40+
.trim()
41+
.strip_prefix("Starting to serve on https://")
42+
.expect("unexpected output")
43+
.parse()
44+
.expect("failed to parse socket address");
45+
46+
(srv, addr)
2347
}
2448

25-
fn wait_for_server(addr: &str) {
26-
for i in 0..10 {
27-
if TcpStream::connect(addr).is_ok() {
28-
return;
29-
}
30-
thread::sleep(time::Duration::from_millis(i * 100));
31-
}
32-
panic!("failed to connect to {addr:?} after 10 tries");
49+
fn client_command() -> Command {
50+
Command::new(examples_dir().join("client"))
3351
}
3452

3553
#[test]
@@ -44,18 +62,12 @@ fn client() {
4462

4563
#[test]
4664
fn server() {
47-
let mut srv = server_command()
48-
.arg("1337")
49-
.spawn()
50-
.expect("cannot run server example");
51-
52-
let addr = "localhost:1337";
53-
wait_for_server(addr);
65+
let (mut srv, addr) = start_server();
5466

5567
let output = Command::new("curl")
5668
.arg("--insecure")
5769
.arg("--http1.0")
58-
.arg(format!("https://{addr}"))
70+
.arg(format!("https://localhost:{}", addr.port()))
5971
.output()
6072
.expect("cannot run curl");
6173

@@ -78,16 +90,10 @@ fn server() {
7890

7991
#[test]
8092
fn custom_ca_store() {
81-
let mut srv = server_command()
82-
.arg("1338")
83-
.spawn()
84-
.expect("cannot run server example");
85-
86-
let addr = "localhost:1338";
87-
wait_for_server(addr);
93+
let (mut srv, addr) = start_server();
8894

8995
let rc = client_command()
90-
.arg(format!("https://{addr}"))
96+
.arg(format!("https://localhost:{}", addr.port()))
9197
.arg("examples/sample.pem")
9298
.output()
9399
.expect("cannot run client example");

0 commit comments

Comments
 (0)