Skip to content

Commit

Permalink
feat(bin): add random suffix to the temporary databases (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan authored Apr 17, 2024
1 parent 38030ac commit ace2231
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 40 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <jobs>` 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.
Expand Down
100 changes: 72 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion sqllogictest-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,3 +34,5 @@ tokio = { version = "1", features = [
"process",
] }
fs-err = "2.9.0"
tracing-subscriber= "0.3"
tracing = "0.1"
25 changes: 18 additions & 7 deletions sqllogictest-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -265,15 +266,25 @@ async fn run_parallel(
junit: Option<String>,
) -> 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?;
Expand Down
25 changes: 22 additions & 3 deletions sqllogictest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,11 @@ impl<D: AsyncDB, M: MakeConnection<Conn = D>> Runner<D, M> {
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 {
Expand All @@ -629,8 +633,9 @@ impl<D: AsyncDB, M: MakeConnection<Conn = D>> Runner<D, M> {
}
};

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") {
Expand All @@ -642,8 +647,22 @@ impl<D: AsyncDB, M: MakeConnection<Conn = D>> Runner<D, M> {
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<AnyError> = 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(
Expand Down

0 comments on commit ace2231

Please sign in to comment.