From 1cd902cd1a0f9c4aaa325b45aaba961567e35448 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 4 Jun 2020 12:24:16 +0000 Subject: [PATCH] tests/rust: Extract a with_webserver_in helper wrapper It's much cleaner if the Tokio stuff stays in `test.rs`, and easier to write tests if the function is synchronous. Prep for further tests. --- tests/inst/src/repobin.rs | 19 +++++-------------- tests/inst/src/test.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/tests/inst/src/repobin.rs b/tests/inst/src/repobin.rs index f45f913b..41fd1390 100644 --- a/tests/inst/src/repobin.rs +++ b/tests/inst/src/repobin.rs @@ -6,7 +6,6 @@ use std::path::Path; use crate::test::*; use anyhow::{Context, Result}; use commandspec::{sh_command, sh_execute}; -use tokio::runtime::Runtime; use with_procspawn_tempdir::with_procspawn_tempdir; #[itest] @@ -61,15 +60,16 @@ fn test_extensions() -> Result<()> { Ok(()) } -async fn impl_test_pull_basicauth() -> Result<()> { +#[itest] +#[with_procspawn_tempdir] +fn test_pull_basicauth() -> Result<()> { let opts = TestHttpServerOpts { basicauth: true, ..Default::default() }; let serverrepo = Path::new("server/repo"); std::fs::create_dir_all(&serverrepo)?; - let addr = http_server(&serverrepo, opts).await?; - tokio::task::spawn_blocking(move || -> Result<()> { + with_webserver_in(&serverrepo, &opts, move |addr| { let baseuri = http::Uri::from_maybe_shared(format!("http://{}/", addr).into_bytes())?; let unauthuri = http::Uri::from_maybe_shared(format!("http://unknown:badpw@{}/", addr).into_bytes())?; @@ -107,15 +107,6 @@ async fn impl_test_pull_basicauth() -> Result<()> { } sh_execute!(r#"ostree --repo=client/repo pull origin-goodauth os >/dev/null"#,)?; Ok(()) - }) - .await??; - Ok(()) -} - -#[itest] -#[with_procspawn_tempdir] -fn test_pull_basicauth() -> Result<()> { - let mut rt = Runtime::new()?; - rt.block_on(async move { impl_test_pull_basicauth().await })?; + })?; Ok(()) } diff --git a/tests/inst/src/test.rs b/tests/inst/src/test.rs index 9e7d4b41..7178d7bb 100644 --- a/tests/inst/src/test.rs +++ b/tests/inst/src/test.rs @@ -15,6 +15,7 @@ use futures_util::future; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Request, Response}; use hyper_staticfile::Static; +use tokio::runtime::Runtime; pub(crate) type TestFn = fn() -> Result<()>; @@ -145,6 +146,23 @@ pub(crate) async fn http_server>( Ok(addr) } +pub(crate) fn with_webserver_in, F>( + path: P, + opts: &TestHttpServerOpts, + f: F) -> Result<()> +where + F: FnOnce(&std::net::SocketAddr) -> Result<()>, + F: Send + 'static, +{ + let path = path.as_ref(); + let mut rt = Runtime::new()?; + rt.block_on(async move { + let addr = http_server(path, opts.clone()).await?; + tokio::task::spawn_blocking(move || f(&addr)).await? + })?; + Ok(()) +} + // I put tests in your tests so you can test while you test #[cfg(test)] mod tests {