Skip to content

Commit 712d758

Browse files
committed
Fix tests to build examples and run on a random port
This makes two changes to the tests to make them a little more reliable: * The tests fail to compile when run with `cargo test --no-default-features`. This changes the tests to shell out to `cargo run ...` to run the examples with the reguired features. * It changes the test to use a random port rather than a fixed one, which might be used by the current system.
1 parent 40bcb63 commit 712d758

2 files changed

Lines changed: 69 additions & 43 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: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,66 @@
11
use std::env;
2-
use std::net::TcpStream;
3-
use std::path::PathBuf;
4-
use std::process::Command;
5-
use std::thread;
6-
use std::time;
7-
8-
fn examples_dir() -> PathBuf {
9-
let target_dir: PathBuf = env::var("CARGO_TARGET_DIR")
10-
.unwrap_or_else(|_| "target".to_string())
11-
.into();
12-
target_dir
13-
.join("debug")
14-
.join("examples")
2+
use std::io::{BufRead, BufReader};
3+
use std::net::SocketAddr;
4+
use std::process::{Child, Command, Stdio};
5+
6+
fn cargo_command() -> Command {
7+
let cargo_bin = env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
8+
9+
let mut cmd = Command::new(cargo_bin);
10+
cmd.arg("--quiet");
11+
12+
cmd
1513
}
1614

1715
fn server_command() -> Command {
18-
Command::new(examples_dir().join("server"))
16+
let mut server = cargo_command();
17+
server
18+
.arg("run")
19+
.arg("--features=aws-lc-rs")
20+
.arg("--example")
21+
.arg("server")
22+
.arg("0");
23+
24+
server
1925
}
2026

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

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");
55+
fn client_command() -> Command {
56+
let mut client = cargo_command();
57+
client
58+
.arg("run")
59+
.arg("--features=native-tokio,http1")
60+
.arg("--example")
61+
.arg("client");
62+
63+
client
3364
}
3465

3566
#[test]
@@ -39,23 +70,21 @@ fn client() {
3970
.output()
4071
.expect("cannot run client example");
4172

42-
assert!(rc.status.success());
73+
if !rc.status.success() {
74+
assert_eq!(String::from_utf8_lossy(&rc.stdout), "");
75+
assert_eq!(String::from_utf8_lossy(&rc.stderr), "");
76+
panic!("test failed");
77+
}
4378
}
4479

4580
#[test]
4681
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);
82+
let (mut srv, addr) = start_server();
5483

5584
let output = Command::new("curl")
5685
.arg("--insecure")
5786
.arg("--http1.0")
58-
.arg(format!("https://{addr}"))
87+
.arg(format!("https://localhost:{}", addr.port()))
5988
.output()
6089
.expect("cannot run curl");
6190

@@ -78,16 +107,10 @@ fn server() {
78107

79108
#[test]
80109
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);
110+
let (mut srv, addr) = start_server();
88111

89112
let rc = client_command()
90-
.arg(format!("https://{addr}"))
113+
.arg(format!("https://localhost:{}", addr.port()))
91114
.arg("examples/sample.pem")
92115
.output()
93116
.expect("cannot run client example");
@@ -98,5 +121,7 @@ fn custom_ca_store() {
98121

99122
if !rc.status.success() {
100123
assert_eq!(String::from_utf8_lossy(&rc.stdout), "");
124+
assert_eq!(String::from_utf8_lossy(&rc.stderr), "");
125+
panic!("test failed");
101126
}
102127
}

0 commit comments

Comments
 (0)