Use a pool of databases to speed up integration tests

Also, use env_logger consistently in the tests for each crate.
Only initiallize the logger at all if some RUST_LOG env var is set.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-02-07 15:00:00 -08:00
parent 8a2613d49c
commit e3f055d950
7 changed files with 99 additions and 47 deletions

View File

@ -1,6 +1,7 @@
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }

View File

@ -18,9 +18,9 @@ use crate::{
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
env_logger::builder() if std::env::var("RUST_LOG").is_ok() {
.filter_level(log::LevelFilter::Info) env_logger::init();
.init(); }
} }
pub fn run_test( pub fn run_test(

View File

@ -17,8 +17,9 @@ use util::test::Network;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[test] #[test]

View File

@ -526,54 +526,88 @@ pub struct ChannelMessage {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use rand::prelude::*; use rand::prelude::*;
use sqlx::{ use sqlx::{
migrate::{MigrateDatabase, Migrator}, migrate::{MigrateDatabase, Migrator},
Postgres, Postgres,
}; };
use std::path::Path; use std::{mem, path::Path};
pub struct TestDb { pub struct TestDb {
pub db: Db, pub db: Option<Db>,
pub name: String, pub name: String,
pub url: String, pub url: String,
} }
impl TestDb { lazy_static! {
pub fn new() -> Self { static ref POOL: Mutex<Vec<TestDb>> = Default::default();
// Enable tests to run in parallel by serializing the creation of each test database. }
lazy_static::lazy_static! {
static ref DB_CREATION: std::sync::Mutex<()> = std::sync::Mutex::new(());
}
let mut rng = StdRng::from_entropy(); #[ctor::dtor]
let name = format!("zed-test-{}", rng.gen::<u128>()); fn clear_pool() {
let url = format!("postgres://postgres@localhost/{}", name); for db in POOL.lock().drain(..) {
let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations")); db.teardown();
let db = block_on(async {
{
let _lock = DB_CREATION.lock();
Postgres::create_database(&url)
.await
.expect("failed to create test db");
}
let mut db = Db::new(&url, 5).await.unwrap();
db.test_mode = true;
let migrator = Migrator::new(migrations_path).await.unwrap();
migrator.run(&db.pool).await.unwrap();
db
});
Self { db, name, url }
}
pub fn db(&self) -> &Db {
&self.db
} }
} }
impl Drop for TestDb { impl TestDb {
fn drop(&mut self) { pub fn new() -> Self {
let mut pool = POOL.lock();
if let Some(db) = pool.pop() {
db.truncate();
db
} else {
let mut rng = StdRng::from_entropy();
let name = format!("zed-test-{}", rng.gen::<u128>());
let url = format!("postgres://postgres@localhost/{}", name);
let migrations_path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/migrations"));
let db = block_on(async {
Postgres::create_database(&url)
.await
.expect("failed to create test db");
let mut db = Db::new(&url, 5).await.unwrap();
db.test_mode = true;
let migrator = Migrator::new(migrations_path).await.unwrap();
migrator.run(&db.pool).await.unwrap();
db
});
Self {
db: Some(db),
name,
url,
}
}
}
pub fn db(&self) -> &Db {
self.db.as_ref().unwrap()
}
fn truncate(&self) {
block_on(async {
let query = "
SELECT tablename FROM pg_tables
WHERE schemaname = 'public';
";
let table_names = sqlx::query_scalar::<_, String>(query)
.fetch_all(&self.db().pool)
.await
.unwrap();
sqlx::query(&format!(
"TRUNCATE TABLE {} RESTART IDENTITY",
table_names.join(", ")
))
.execute(&self.db().pool)
.await
.unwrap();
})
}
fn teardown(mut self) {
let db = self.db.take().unwrap();
block_on(async { block_on(async {
let query = " let query = "
SELECT pg_terminate_backend(pg_stat_activity.pid) SELECT pg_terminate_backend(pg_stat_activity.pid)
@ -582,15 +616,27 @@ pub mod tests {
"; ";
sqlx::query(query) sqlx::query(query)
.bind(&self.name) .bind(&self.name)
.execute(&self.db.pool) .execute(&db.pool)
.await .await
.unwrap(); .unwrap();
self.db.pool.close().await; db.pool.close().await;
Postgres::drop_database(&self.url).await.unwrap(); Postgres::drop_database(&self.url).await.unwrap();
}); });
} }
} }
impl Drop for TestDb {
fn drop(&mut self) {
if let Some(db) = self.db.take() {
POOL.lock().push(TestDb {
db: Some(db),
name: mem::take(&mut self.name),
url: mem::take(&mut self.url),
});
}
}
}
#[gpui::test] #[gpui::test]
async fn test_get_users_by_ids() { async fn test_get_users_by_ids() {
let test_db = TestDb::new(); let test_db = TestDb::new();

View File

@ -1196,8 +1196,9 @@ mod tests {
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[gpui::test] #[gpui::test]

View File

@ -12,8 +12,9 @@ use util::test::Network;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
// std::env::set_var("RUST_LOG", "info"); if std::env::var("RUST_LOG").is_ok() {
env_logger::init(); env_logger::init();
}
} }
#[test] #[test]

View File

@ -12,7 +12,9 @@ use workspace::Settings;
#[cfg(test)] #[cfg(test)]
#[ctor::ctor] #[ctor::ctor]
fn init_logger() { fn init_logger() {
env_logger::init(); if std::env::var("RUST_LOG").is_ok() {
env_logger::init();
}
} }
pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> { pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {