diff options
author | Antoine A <> | 2023-01-24 14:57:19 +0100 |
---|---|---|
committer | Antoine A <> | 2023-01-24 14:57:19 +0100 |
commit | 4f2696c5fc72f2ba07ba5a5b4c9e51e1237d2a92 (patch) | |
tree | 47445a2beb6c6d876348af512b0e1a371e8f2f8c | |
parent | 065a0d5ed7f17d512bafc4ba2b41321cd4905587 (diff) | |
download | depolymerization-4f2696c5fc72f2ba07ba5a5b4c9e51e1237d2a92.tar.gz depolymerization-4f2696c5fc72f2ba07ba5a5b4c9e51e1237d2a92.tar.bz2 depolymerization-4f2696c5fc72f2ba07ba5a5b4c9e51e1237d2a92.zip |
Improve postgres setup for tests
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | instrumentation/Cargo.toml | 2 | ||||
-rw-r--r-- | instrumentation/src/utils.rs | 94 |
3 files changed, 53 insertions, 50 deletions
@@ -1083,6 +1083,7 @@ dependencies = [ "hex", "libdeflater", "owo-colors", + "signal-child", "tempfile", "ureq", ] @@ -1896,6 +1897,12 @@ dependencies = [ ] [[package]] +name = "signal-child" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a4eed4c5ae38438470ab8e0108bb751012f786f44ff585cfd837c9a5fe426f" + +[[package]] name = "siphasher" version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/instrumentation/Cargo.toml b/instrumentation/Cargo.toml index 707c5f1..4103410 100644 --- a/instrumentation/Cargo.toml +++ b/instrumentation/Cargo.toml @@ -28,6 +28,8 @@ fastrand = "1.8.0" owo-colors = "3.5.0" # Better backtrace color-backtrace = "0.5.1" +# Send signal to child processes +signal-child = "1.0.5" [build-dependencies] diff --git a/instrumentation/src/utils.rs b/instrumentation/src/utils.rs index f847b4d..f35eefc 100644 --- a/instrumentation/src/utils.rs +++ b/instrumentation/src/utils.rs @@ -32,6 +32,7 @@ use common::{ rand_slice, url::Url, }; +use signal_child::{signal::Signal, Signalable}; use tempfile::TempDir; pub fn print_now(disp: impl Display) { @@ -227,6 +228,7 @@ pub struct CommonCtx { gateway: ChildGuard, pub gateway_url: String, pub taler_conf: TalerConfig, + db: ChildGuard, wire: ChildGuard, _wire2: Option<ChildGuard>, } @@ -271,11 +273,11 @@ impl CommonCtx { pub fn setup(dirs: Dirs, name: &str, stressed: bool, init_wallet: impl FnOnce(&Dirs)) -> Self { let taler_conf = TalerConfig::load(Some(&dirs.conf)); // Setup database - { + let db = { // Init databases files cmd_redirect_ok( - "pg_ctl", - &["init", "-D", dirs.db_dir.to_string_lossy().as_ref()], + "initdb", + &[dirs.db_dir.to_string_lossy().as_ref()], "log/postgres.log", "init_db", ); @@ -288,25 +290,36 @@ impl CommonCtx { ), ) .unwrap(); - Self::_start_db(&dirs.db_dir); - - let mut psql = ChildGuard( - Command::new("psql") - .args(["-h", "localhost", "-p", "5454", "postgres"]) - .stderr(Stdio::null()) - .stdout(Stdio::null()) - .stdin(Stdio::piped()) - .spawn() - .unwrap(), - ); - psql.0 - .stdin - .as_mut() - .unwrap() - .write_all("CREATE ROLE postgres LOGIN SUPERUSER PASSWORD 'password'".as_bytes()) - .unwrap(); - cmd_ok(psql, "psql setup"); - } + let db = Self::_start_db(&dirs.db_dir); + retry(|| { + let mut psql = ChildGuard( + Command::new("psql") + .args(["-h", "localhost", "-p", "5454", "postgres"]) + .stderr(Stdio::null()) + .stdout( + std::fs::File::options() + .append(true) + .create(true) + .open("log/postgres.log") + .unwrap(), + ) + .stdin(Stdio::piped()) + .spawn() + .unwrap(), + ); + psql.0 + .stdin + .as_mut() + .unwrap() + .write_all( + "CREATE ROLE postgres LOGIN SUPERUSER PASSWORD 'password'".as_bytes(), + ) + .unwrap(); + psql.0.wait().unwrap().success() + }); + + db + }; // Wire let wire_path = format!("target/release/{}", name); @@ -366,6 +379,7 @@ impl CommonCtx { gateway_url, taler_conf, wire, + db, _wire2: wire2, } } @@ -384,32 +398,20 @@ impl CommonCtx { /* ----- Database ----- */ - fn _start_db(path: &Path) { - cmd_redirect_ok( - "pg_ctl", - &["start", "-D", path.to_string_lossy().as_ref()], + fn _start_db(path: &Path) -> ChildGuard { + cmd_redirect( + "postgres", + &["-D", path.to_string_lossy().as_ref()], "log/postgres.log", - "start db", ) } - fn _stop_db(path: &Path, log: &Path) -> std::io::Result<ChildGuard> { - try_cmd_redirect( - "pg_ctl", - &["stop", "-D", path.to_string_lossy().as_ref()], - log.to_string_lossy().as_ref(), - ) + pub fn resume_db(&mut self) { + self.db = Self::_start_db(&self.dirs.db_dir); } - pub fn resume_db(&self) { - Self::_start_db(&self.dirs.db_dir) - } - - pub fn stop_db(&self) { - cmd_ok( - Self::_stop_db(&self.dirs.db_dir, "log/postgres.log".as_ref()).unwrap(), - "stop db", - ) + pub fn stop_db(&mut self) { + self.db.0.signal(Signal::SIGQUIT).unwrap(); } /* ----- Wire Gateway -----*/ @@ -434,11 +436,3 @@ impl CommonCtx { retry(|| check_gateway_down(&self.gateway_url)); } } - -impl Drop for CommonCtx { - fn drop(&mut self) { - Self::_stop_db(&self.dirs.db_dir, "/dev/null".as_ref()) - .and_then(|mut it| it.0.wait()) - .ok(); - } -} |