Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout waiting for test-proxy to start #1961

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/core/azure_core_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions sdk/core/azure_core_test/examples/test_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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?;

Expand Down Expand Up @@ -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;
Expand Down
18 changes: 12 additions & 6 deletions sdk/core/azure_core_test/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ use std::{
path::Path,
process::{ExitStatus, Stdio},
sync::Arc,
time::Duration,
};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::{Child, ChildStdout, Command},
};
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<Path>,
Expand Down Expand Up @@ -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),
Expand All @@ -79,9 +88,6 @@ async fn wait_till_listening(stdout: &mut Option<ChildStdout>) -> Result<Url> {
));
};

// 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? {
Expand All @@ -90,7 +96,7 @@ async fn wait_till_listening(stdout: &mut Option<ChildStdout>) -> Result<Url> {
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);
}
Expand Down