From ef2f004b0599b8c6dcddf4e641f54a966a510902 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 1 Dec 2023 15:16:07 +0100 Subject: [PATCH] speed up tests by starting postgres in parallel --- tests/common.rs | 39 ++++++++++++++++++++++++----------- tests/scripts/start_dev_db.sh | 3 ++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index b5d7f75..23d6a06 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -35,10 +35,19 @@ impl TestData { .init(); }); + // initialize postgres databases in parallel because its slow + let (alpha_db_path, alpha_db_thread) = start_temporary_database("alpha"); + let (beta_db_path, beta_db_thread) = start_temporary_database("beta"); + let (gamma_db_path, gamma_db_thread) = start_temporary_database("gamma"); + + alpha_db_thread.join().unwrap(); + beta_db_thread.join().unwrap(); + gamma_db_thread.join().unwrap(); + Self { - alpha: Instance::start("alpha", 8131), - beta: Instance::start("beta", 8132), - gamma: Instance::start("gamma", 8133), + alpha: Instance::start(alpha_db_path, 8131), + beta: Instance::start(beta_db_path, 8132), + gamma: Instance::start(gamma_db_path, 8133), } } @@ -50,6 +59,20 @@ impl TestData { } } +fn start_temporary_database(name: &'static str) -> (String, std::thread::JoinHandle<()>) { + let db_path = format!("{}/target/test_db/{name}", current_dir().unwrap().display()); + let db_path_ = db_path.clone(); + let db_thread = std::thread::spawn(move || { + Command::new("./tests/scripts/start_dev_db.sh") + .arg(&db_path_) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .output() + .unwrap(); + }); + (db_path, db_thread) +} + pub struct Instance { db_path: String, pub hostname: String, @@ -57,15 +80,7 @@ pub struct Instance { } impl Instance { - fn start(name: &'static str, port: i32) -> Self { - let db_path = format!("{}/target/test_db/{name}", current_dir().unwrap().display()); - // TODO: would be faster to use async Command from tokio and run in parallel - Command::new("./tests/scripts/start_dev_db.sh") - .arg(&db_path) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .output() - .unwrap(); + fn start(db_path: String, port: i32) -> Self { let db_url = format!("postgresql://lemmy:password@/lemmy?host={db_path}"); let hostname = format!("localhost:{port}"); let hostname_ = hostname.clone(); diff --git a/tests/scripts/start_dev_db.sh b/tests/scripts/start_dev_db.sh index 86980a6..aa6a258 100755 --- a/tests/scripts/start_dev_db.sh +++ b/tests/scripts/start_dev_db.sh @@ -16,7 +16,8 @@ fi # Create cluster initdb --username=postgres --auth=trust --no-instructions -#touch "$PGHOST/.s.PGSQL.5432" +touch "$PGHOST/.s.PGSQL.5432" +echo "$PGHOST/.s.PGSQL.5432" # Start server that only listens to socket in current directory pg_ctl start --options="-c listen_addresses= -c unix_socket_directories=$PGHOST"