allow for parallel tests

By default the libtest test harness runs multiple tests in parallel.
This makes tests fail because the server port is already occupied by another port.
This commit is contained in:
Johann150 2021-02-20 10:22:12 +01:00
parent e1d808a790
commit 3c6684d54c
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1

View file

@ -6,11 +6,17 @@ use url::Url;
static BINARY_PATH: &'static str = env!("CARGO_BIN_EXE_agate");
fn addr() -> SocketAddr {
"[::1]:1965".to_socket_addrs().unwrap().next().unwrap()
fn addr(port: u16) -> SocketAddr {
use std::net::{IpAddr, Ipv4Addr};
(IpAddr::V4(Ipv4Addr::LOCALHOST), port)
.to_socket_addrs()
.unwrap()
.next()
.unwrap()
}
fn get(args: &[&str], url: &str) -> Result<Page, anyhow::Error> {
fn get(args: &[&str], addr: SocketAddr, url: &str) -> Result<Page, anyhow::Error> {
// start the server
let mut server = Command::new(BINARY_PATH)
.stderr(Stdio::piped())
@ -32,7 +38,7 @@ fn get(args: &[&str], url: &str) -> Result<Page, anyhow::Error> {
// actually perform the request
let page = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { Page::fetch_from(&Url::parse(url).unwrap(), addr(), None).await });
.block_on(async { Page::fetch_from(&Url::parse(url).unwrap(), addr, None).await });
// try to stop the server again
match server.try_wait() {
@ -57,7 +63,7 @@ fn get(args: &[&str], url: &str) -> Result<Page, anyhow::Error> {
#[test]
fn index_page() {
let page = get(&[], "gemini://localhost").expect("could not get page");
let page = get(&[], addr(1965), "gemini://localhost").expect("could not get page");
assert_eq!(
page.header,