diff --git a/tests/connect/src/main.rs b/tests/connect/src/main.rs index ccd2b68..7766e28 100644 --- a/tests/connect/src/main.rs +++ b/tests/connect/src/main.rs @@ -48,31 +48,6 @@ fn main() { ts = mt; } - assert!( - std::process::Command::new("strace") - .args([ - "-f", - "-e", - "trace=connect", - "-o", - "/tmp/tame-index-connection-trace" - ]) - .arg(latest) - .arg("reuses_connection") - .status() - .unwrap() - .success(), - "failed to strace test" - ); - - let trace = std::fs::read_to_string("/tmp/tame-index-connection-trace") - .expect("failed to read strace output"); - - let connection_counts = trace - .lines() - .filter(|line| line.contains("connect(")) - .count(); - // The connection count should be roughly the same as the processor count let stdout = std::process::Command::new("nproc").output().unwrap().stdout; @@ -81,9 +56,39 @@ fn main() { .trim() .parse() .unwrap(); + let max = proc_count + 5; - assert!( - connection_counts <= max, - "connection syscalls ({connection_counts}) should be lower than {max}" - ); + + for test in ["reuses_connection", "async_reuses_connection"] { + assert!( + std::process::Command::new("strace") + .args([ + "-f", + "-e", + "trace=connect", + "-o", + "/tmp/tame-index-connection-trace" + ]) + .arg(&latest) + .arg("--exact") + .arg(format!("remote::{test}")) + .status() + .unwrap() + .success(), + "failed to strace test" + ); + + let trace = std::fs::read_to_string("/tmp/tame-index-connection-trace") + .expect("failed to read strace output"); + + let connection_counts = trace + .lines() + .filter(|line| line.contains("connect(")) + .count(); + + assert!( + connection_counts <= max, + "connection syscalls ({connection_counts}) should be lower than {max}" + ); + } } diff --git a/tests/sparse.rs b/tests/sparse.rs index f065d0d..4107c62 100644 --- a/tests/sparse.rs +++ b/tests/sparse.rs @@ -219,161 +219,181 @@ mod remote { .expect("failed to find expected version"); } + // cargo metadata --format-version=1 | jq -r '.packages[].name | "\"\(.)\","' + const KRATES: &[&str] = &[ + "addr2line", + "adler", + "async-compression", + "autocfg", + "backtrace", + "base64", + "bitflags", + "bitflags", + "bumpalo", + "bytes", + "camino", + "cargo-platform", + "cargo_metadata", + "cc", + "cfg-if", + "core-foundation", + "core-foundation-sys", + "crc32fast", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-utils", + "either", + "encoding_rs", + "equivalent", + "errno", + "fastrand", + "flate2", + "fnv", + "form_urlencoded", + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", + "getrandom", + "gimli", + "h2", + "hashbrown", + "hermit-abi", + "home", + "http", + "http-body", + "httparse", + "httpdate", + "hyper", + "hyper-rustls", + "idna", + "indexmap", + "ipnet", + "itoa", + "js-sys", + "libc", + "linux-raw-sys", + "log", + "memchr", + "mime", + "miniz_oxide", + "mio", + "num_cpus", + "object", + "once_cell", + "percent-encoding", + "pin-project-lite", + "pin-utils", + "proc-macro2", + "quote", + "rayon", + "rayon-core", + "reqwest", + "ring", + "rustc-demangle", + "rustix", + "rustls", + "rustls-pemfile", + "rustls-webpki", + "ryu", + "sct", + "semver", + "serde", + "serde_derive", + "serde_json", + "serde_spanned", + "serde_urlencoded", + "slab", + "smol_str", + "socket2", + "spin", + "static_assertions", + "syn", + "sync_wrapper", + "system-configuration", + "system-configuration-sys", + "tame-index", + "tempfile", + "thiserror", + "thiserror-impl", + "tiny-bench", + "tinyvec", + "tinyvec_macros", + "tokio", + "tokio-rustls", + "tokio-util", + "toml", + "toml_datetime", + "toml_edit", + "tower-service", + "tracing", + "tracing-core", + "try-lock", + "twox-hash", + "unicode-bidi", + "unicode-ident", + "unicode-normalization", + "untrusted", + "url", + "want", + "wasi", + "wasm-bindgen", + "wasm-bindgen-backend", + "wasm-bindgen-futures", + "wasm-bindgen-macro", + "wasm-bindgen-macro-support", + "wasm-bindgen-shared", + "web-sys", + "webpki-roots", + "windows-sys", + "windows-sys", + "windows-targets", + "windows-targets", + "windows_aarch64_gnullvm", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", + "windows_x86_64_msvc", + "winnow", + "winreg", + ]; + + fn ensure_no_errors( + results: std::collections::BTreeMap< + String, + Result, tame_index::Error>, + >, + ) { + use std::fmt::Write; + let mut errors = String::new(); + + for (name, res) in results { + match res { + Ok(Some(_)) => continue, + Ok(None) => writeln!(&mut errors, "{name}:\tfailed to locate").unwrap(), + Err(err) => writeln!(&mut errors, "{name}:\t{err}").unwrap(), + } + } + + assert!(errors.is_empty(), "{errors}"); + } + /// Reuses connections. This test is intended to be run under strace to /// validate that connections are not being created /// https://github.com/EmbarkStudios/tame-index/issues/46 #[test] fn reuses_connection() { - // cargo metadata --format-version=1 | jq -r '.packages[].name | "\"\(.)\","' - const KRATES: &[&str] = &[ - "addr2line", - "adler", - "async-compression", - "autocfg", - "backtrace", - "base64", - "bitflags", - "bitflags", - "bumpalo", - "bytes", - "camino", - "cargo-platform", - "cargo_metadata", - "cc", - "cfg-if", - "core-foundation", - "core-foundation-sys", - "crc32fast", - "crossbeam-deque", - "crossbeam-epoch", - "crossbeam-utils", - "either", - "encoding_rs", - "equivalent", - "errno", - "fastrand", - "flate2", - "fnv", - "form_urlencoded", - "futures-channel", - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", - "getrandom", - "gimli", - "h2", - "hashbrown", - "hermit-abi", - "home", - "http", - "http-body", - "httparse", - "httpdate", - "hyper", - "hyper-rustls", - "idna", - "indexmap", - "ipnet", - "itoa", - "js-sys", - "libc", - "linux-raw-sys", - "log", - "memchr", - "mime", - "miniz_oxide", - "mio", - "num_cpus", - "object", - "once_cell", - "percent-encoding", - "pin-project-lite", - "pin-utils", - "proc-macro2", - "quote", - "rayon", - "rayon-core", - "reqwest", - "ring", - "rustc-demangle", - "rustix", - "rustls", - "rustls-pemfile", - "rustls-webpki", - "ryu", - "sct", - "semver", - "serde", - "serde_derive", - "serde_json", - "serde_spanned", - "serde_urlencoded", - "slab", - "smol_str", - "socket2", - "spin", - "static_assertions", - "syn", - "sync_wrapper", - "system-configuration", - "system-configuration-sys", - "tame-index", - "tempfile", - "thiserror", - "thiserror-impl", - "tiny-bench", - "tinyvec", - "tinyvec_macros", - "tokio", - "tokio-rustls", - "tokio-util", - "toml", - "toml_datetime", - "toml_edit", - "tower-service", - "tracing", - "tracing-core", - "try-lock", - "twox-hash", - "unicode-bidi", - "unicode-ident", - "unicode-normalization", - "untrusted", - "url", - "want", - "wasi", - "wasm-bindgen", - "wasm-bindgen-backend", - "wasm-bindgen-futures", - "wasm-bindgen-macro", - "wasm-bindgen-macro-support", - "wasm-bindgen-shared", - "web-sys", - "webpki-roots", - "windows-sys", - "windows-sys", - "windows-targets", - "windows-targets", - "windows_aarch64_gnullvm", - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", - "windows_x86_64_msvc", - "winnow", - "winreg", - ]; - let td = utils::tempdir(); let index = crates_io(&td); let lock = &utils::unlocked(); @@ -387,17 +407,31 @@ mod remote { lock, ); - use std::fmt::Write; - let mut errors = String::new(); + ensure_no_errors(results); + } - for (name, res) in results { - match res { - Ok(Some(_)) => continue, - Ok(None) => writeln!(&mut errors, "{name}:\tfailed to locate").unwrap(), - Err(err) => writeln!(&mut errors, "{name}:\t{err}").unwrap(), - } - } + // Ditto, but for async + #[test] + fn async_reuses_connection() { + let rt = tokio::runtime::Runtime::new().unwrap(); + let _guard = rt.enter(); - assert!(errors.is_empty(), "{errors}"); + let td = utils::tempdir(); + let index = crates_io(&td); + let lock = &utils::unlocked(); + + let client = reqwest::Client::builder().build().unwrap(); + let rsi = tame_index::index::AsyncRemoteSparseIndex::new(index, client); + + let results = rsi + .krates_blocking( + KRATES.into_iter().map(|s| s.to_string()).collect(), + false, + None, + lock, + ) + .unwrap(); + + ensure_no_errors(results); } }