speed up tests by starting postgres in parallel

This commit is contained in:
Felix Ableitner 2023-12-01 15:16:07 +01:00
parent d4772d35c2
commit ef2f004b05
2 changed files with 29 additions and 13 deletions

View File

@ -35,10 +35,19 @@ impl TestData {
.init(); .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 { Self {
alpha: Instance::start("alpha", 8131), alpha: Instance::start(alpha_db_path, 8131),
beta: Instance::start("beta", 8132), beta: Instance::start(beta_db_path, 8132),
gamma: Instance::start("gamma", 8133), 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 { pub struct Instance {
db_path: String, db_path: String,
pub hostname: String, pub hostname: String,
@ -57,15 +80,7 @@ pub struct Instance {
} }
impl Instance { impl Instance {
fn start(name: &'static str, port: i32) -> Self { fn start(db_path: String, 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();
let db_url = format!("postgresql://lemmy:password@/lemmy?host={db_path}"); let db_url = format!("postgresql://lemmy:password@/lemmy?host={db_path}");
let hostname = format!("localhost:{port}"); let hostname = format!("localhost:{port}");
let hostname_ = hostname.clone(); let hostname_ = hostname.clone();

View File

@ -16,7 +16,8 @@ fi
# Create cluster # Create cluster
initdb --username=postgres --auth=trust --no-instructions 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 # Start server that only listens to socket in current directory
pg_ctl start --options="-c listen_addresses= -c unix_socket_directories=$PGHOST" pg_ctl start --options="-c listen_addresses= -c unix_socket_directories=$PGHOST"