From ace2231236c137027693473fd63d4a9c06be7c6f Mon Sep 17 00:00:00 2001 From: xxchan Date: Wed, 17 Apr 2024 19:33:15 +0800 Subject: [PATCH] feat(bin): add random suffix to the temporary databases (#207) --- CHANGELOG.md | 10 ++++ Cargo.lock | 100 +++++++++++++++++++++++++---------- Cargo.toml | 2 +- sqllogictest-bin/Cargo.toml | 3 +- sqllogictest-bin/src/main.rs | 25 ++++++--- sqllogictest/src/runner.rs | 25 +++++++-- 6 files changed, 125 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac20304..c959cae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## [0.20.1] - 2024-04-17 + +* bin: When using `-j ` to run tests in parallel, add a random suffix to the temporary databases. This is useful if the test is manually canceled, but you want to rerun it freshly. Note that if the test failed, the database will be dropped. This is existing behavior and unchanged. +* bin: replace `env_logger` with `tracing-subscriber`. You will be able to see the record being executed with `RUST_LOG=debug sqllogictest ...`. +* runner: fix the behavior of background `system` commands (end with `&`). In `0.20.0`, it will block until the process exits. Now we return immediately. + ``` + system ok + sleep 5 & + ``` + ## [0.20.0] - 2024-04-08 * Show stdout, stderr when `system` command fails. diff --git a/Cargo.lock b/Cargo.lock index cfa41c5..1d45fab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,19 +446,6 @@ dependencies = [ "syn 2.0.37", ] -[[package]] -name = "env_logger" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -728,17 +715,6 @@ dependencies = [ "hashbrown 0.14.0", ] -[[package]] -name = "is-terminal" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" -dependencies = [ - "hermit-abi", - "rustix", - "windows-sys 0.48.0", -] - [[package]] name = "itertools" version = "0.11.0" @@ -849,6 +825,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d906846a98739ed9d73d66e62c2641eef8321f1734b7a1156ab045a0248fb2b3" +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-bigint" version = "0.4.4" @@ -904,6 +890,12 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "owo-colors" version = "3.5.0" @@ -1349,6 +1341,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1403,7 +1404,7 @@ dependencies = [ [[package]] name = "sqllogictest" -version = "0.20.0" +version = "0.20.1" dependencies = [ "async-trait", "educe", @@ -1426,14 +1427,13 @@ dependencies = [ [[package]] name = "sqllogictest-bin" -version = "0.20.0" +version = "0.20.1" dependencies = [ "anyhow", "async-trait", "chrono", "clap", "console", - "env_logger", "fs-err", "futures", "glob", @@ -1443,11 +1443,13 @@ dependencies = [ "sqllogictest", "sqllogictest-engines", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] name = "sqllogictest-engines" -version = "0.20.0" +version = "0.20.1" dependencies = [ "async-trait", "bytes", @@ -1578,6 +1580,16 @@ dependencies = [ "syn 2.0.37", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "threadpool" version = "1.8.1" @@ -1710,6 +1722,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -1757,6 +1795,12 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index f7ed8f0..72008d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = ["sqllogictest", "sqllogictest-bin", "sqllogictest-engines", "tests"] [workspace.package] -version = "0.20.0" +version = "0.20.1" edition = "2021" homepage = "https://github.com/risinglightdb/sqllogictest-rs" keywords = ["sql", "database", "parser", "cli"] diff --git a/sqllogictest-bin/Cargo.toml b/sqllogictest-bin/Cargo.toml index fbca6eb..20d1e7a 100644 --- a/sqllogictest-bin/Cargo.toml +++ b/sqllogictest-bin/Cargo.toml @@ -18,7 +18,6 @@ async-trait = "0.1" chrono = { version = "0.4" } clap = { version = "4", features = ["derive", "env"] } console = { version = "0.15" } -env_logger = { version = "0.10" } futures = { version = "0.3", default-features = false } glob = "0.3" itertools = "0.11" @@ -35,3 +34,5 @@ tokio = { version = "1", features = [ "process", ] } fs-err = "2.9.0" +tracing-subscriber= "0.3" +tracing = "0.1" \ No newline at end of file diff --git a/sqllogictest-bin/src/main.rs b/sqllogictest-bin/src/main.rs index 710a3e2..90c9ce7 100644 --- a/sqllogictest-bin/src/main.rs +++ b/sqllogictest-bin/src/main.rs @@ -1,6 +1,6 @@ mod engines; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::io::{stdout, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; @@ -14,6 +14,7 @@ use fs_err::{File, OpenOptions}; use futures::StreamExt; use itertools::Itertools; use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestSuite}; +use rand::distributions::DistString; use rand::seq::SliceRandom; use sqllogictest::{ default_validator, strict_column_validator, update_record_with_output, AsyncDB, Injected, @@ -130,7 +131,7 @@ impl DBConfig { #[tokio::main] pub async fn main() -> Result<()> { - env_logger::init(); + tracing_subscriber::fmt::init(); let cli = Opt::command().disable_help_flag(true).arg( Arg::new("help") @@ -265,15 +266,25 @@ async fn run_parallel( junit: Option, ) -> Result<()> { let mut create_databases = BTreeMap::new(); + let mut filenames = BTreeSet::new(); for file in files { - let db_name = file + let filename = file .to_str() .ok_or_else(|| anyhow!("not a UTF-8 filename"))?; - let db_name = db_name.replace([' ', '.', '-', '/'], "_"); - eprintln!("+ Discovered Test: {db_name}"); - if create_databases.insert(db_name.to_string(), file).is_some() { - return Err(anyhow!("duplicated file name found: {}", db_name)); + let normalized_filename = filename.replace([' ', '.', '-', '/'], "_"); + eprintln!("+ Discovered Test: {normalized_filename}"); + if !filenames.insert(normalized_filename.clone()) { + return Err(anyhow!( + "duplicated file name found: {}", + normalized_filename + )); } + let random_id: String = rand::distributions::Alphanumeric + .sample_string(&mut rand::thread_rng(), 8) + .to_lowercase(); + let db_name = format!("{normalized_filename}_{random_id}"); + + create_databases.insert(db_name, file); } let mut db = engines::connect(engine, &config).await?; diff --git a/sqllogictest/src/runner.rs b/sqllogictest/src/runner.rs index 6b02818..ca1d965 100644 --- a/sqllogictest/src/runner.rs +++ b/sqllogictest/src/runner.rs @@ -619,7 +619,11 @@ impl> Runner { loc: _, stdout: expected_stdout, } => { - let command = match self.may_substitute(command) { + if should_skip(&self.labels, "", &conditions) { + return RecordOutput::Nothing; + } + + let mut command = match self.may_substitute(command) { Ok(command) => command, Err(error) => { return RecordOutput::System { @@ -629,8 +633,9 @@ impl> Runner { } }; - if should_skip(&self.labels, "", &conditions) { - return RecordOutput::Nothing; + let is_background = command.trim().ends_with('&'); + if is_background { + command = command.trim_end_matches('&').trim().to_string(); } let mut cmd = if cfg!(target_os = "windows") { @@ -642,8 +647,22 @@ impl> Runner { cmd.arg("-c").arg(command); cmd }; + + if is_background { + // Spawn a new process, but don't wait for stdout, otherwise it will block until the process exits. + let error: Option = match cmd.spawn() { + Ok(_) => None, + Err(e) => Some(Arc::new(e)), + }; + return RecordOutput::System { + error, + stdout: None, + }; + } + cmd.stdout(std::process::Stdio::piped()); cmd.stderr(std::process::Stdio::piped()); + let result = D::run_command(cmd).await; #[derive(thiserror::Error, Debug)] #[error(