diff --git a/sdk/core/azure_core_test/Cargo.toml b/sdk/core/azure_core_test/Cargo.toml index bd073f9e58..017849e82e 100644 --- a/sdk/core/azure_core_test/Cargo.toml +++ b/sdk/core/azure_core_test/Cargo.toml @@ -21,7 +21,7 @@ serde.workspace = true tracing.workspace = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -tokio = { workspace = true, features = ["io-util", "process", "sync"] } +tokio = { workspace = true, features = ["io-util", "process", "sync", "time"] } [dev-dependencies] clap.workspace = true diff --git a/sdk/core/azure_core_test/examples/test_proxy.rs b/sdk/core/azure_core_test/examples/test_proxy.rs index fdc3c9e05c..c9379d7941 100644 --- a/sdk/core/azure_core_test/examples/test_proxy.rs +++ b/sdk/core/azure_core_test/examples/test_proxy.rs @@ -14,12 +14,10 @@ async fn main() -> Result<(), Box> { let args = Args::parse(); - tracing_subscriber::fmt() - // Default trace level based on command line arguments. - .with_max_level(args.trace_level()) - // RUST_LOG environment variable can override trace level. - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) - .init(); + let filter = tracing_subscriber::EnvFilter::builder() + .with_default_directive(args.trace_level().into()) + .from_env_lossy(); + tracing_subscriber::fmt().with_env_filter(filter).init(); let mut proxy = proxy::start(env!("CARGO_MANIFEST_DIR"), Some(args.into())).await?; @@ -57,8 +55,8 @@ struct Args { verbose: bool, } -#[cfg(not(target_arch = "wasm32"))] impl Args { + #[cfg(not(target_arch = "wasm32"))] fn trace_level(&self) -> tracing::level_filters::LevelFilter { if self.verbose { return tracing::level_filters::LevelFilter::DEBUG; diff --git a/sdk/core/azure_core_test/src/proxy.rs b/sdk/core/azure_core_test/src/proxy.rs index f80ee134cb..12f9fdd4bb 100644 --- a/sdk/core/azure_core_test/src/proxy.rs +++ b/sdk/core/azure_core_test/src/proxy.rs @@ -9,6 +9,7 @@ use std::{ path::Path, process::{ExitStatus, Stdio}, sync::Arc, + time::Duration, }; use tokio::{ io::{AsyncBufReadExt, BufReader}, @@ -16,11 +17,12 @@ use tokio::{ }; use tracing::{Level, Span}; -// cspell:ignore aspnetcore devcert testproxy +// cspell:ignore aspnetcore devcert teamprojectid testproxy const KESTREL_CERT_PATH_ENV: &str = "ASPNETCORE_Kestrel__Certificates__Default__Path"; const KESTREL_CERT_PASSWORD_ENV: &str = "ASPNETCORE_Kestrel__Certificates__Default__Password"; const KESTREL_CERT_PASSWORD: &str = "password"; const PROXY_MANUAL_START: &str = "PROXY_MANUAL_START"; +const SYSTEM_TEAMPROJECTID: &str = "SYSTEM_TEAMPROJECTID"; pub async fn start( test_data_dir: impl AsRef, @@ -63,7 +65,14 @@ pub async fn start( // Wait until the service is listening on a port. let mut stdout = command.stdout.take(); - let url = wait_till_listening(&mut stdout).await?; + let max_seconds = Duration::from_secs(env::var(SYSTEM_TEAMPROJECTID).map_or(5, |_| 20)); + let url = tokio::select! { + v = wait_till_listening(&mut stdout) => { v? }, + _ = tokio::time::sleep(max_seconds) => { + command.kill().await?; + return Err(azure_core::Error::message(ErrorKind::Other, "timeout waiting for test-proxy to start")); + }, + }; Ok(Proxy { command: Some(command), @@ -79,9 +88,6 @@ async fn wait_till_listening(stdout: &mut Option) -> Result { )); }; - // cspell:ignore teamprojectid - let _max_seconds = env::var("SYSTEM_TEAMPROJECTID").map_or(5, |_| 20); - // Wait for the process to respond to requests and check output until pattern: "Now listening on: http://0.0.0.0:60583" (random port). let mut reader = BufReader::new(stdout).lines(); while let Some(line) = reader.next_line().await? { @@ -90,7 +96,7 @@ async fn wait_till_listening(stdout: &mut Option) -> Result { if let Some(idx) = line.find(PATTERN) { let idx = idx + PATTERN.len(); let url = line[idx..].parse()?; - tracing::event!(target: crate::SPAN_TARGET, Level::INFO, "listening on {}", url); + tracing::event!(target: crate::SPAN_TARGET, Level::INFO, "listening on {url}"); return Ok(url); }