mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-07 20:39:04 +03:00
Try another hacky approach for tearing down DBs after all tests
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
b0ed58add3
commit
c245356b42
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -5850,6 +5850,7 @@ dependencies = [
|
|||||||
"tide-compress",
|
"tide-compress",
|
||||||
"time 0.2.25",
|
"time 0.2.25",
|
||||||
"toml",
|
"toml",
|
||||||
|
"util",
|
||||||
"zed",
|
"zed",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -61,6 +61,7 @@ gpui = { path = "../gpui" }
|
|||||||
zed = { path = "../zed", features = ["test-support"] }
|
zed = { path = "../zed", features = ["test-support"] }
|
||||||
ctor = "0.1"
|
ctor = "0.1"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
|
util = { path = "../util" }
|
||||||
|
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
serde_json = { version = "1.0.64", features = ["preserve_order"] }
|
serde_json = { version = "1.0.64", features = ["preserve_order"] }
|
||||||
|
@ -534,6 +534,7 @@ pub mod tests {
|
|||||||
Postgres,
|
Postgres,
|
||||||
};
|
};
|
||||||
use std::{mem, path::Path};
|
use std::{mem, path::Path};
|
||||||
|
use util::ResultExt as _;
|
||||||
|
|
||||||
pub struct TestDb {
|
pub struct TestDb {
|
||||||
pub db: Option<Db>,
|
pub db: Option<Db>,
|
||||||
@ -545,13 +546,31 @@ pub mod tests {
|
|||||||
static ref POOL: Mutex<Vec<TestDb>> = Default::default();
|
static ref POOL: Mutex<Vec<TestDb>> = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ctor::dtor]
|
use std::os::raw::c_int;
|
||||||
fn clear_pool() {
|
|
||||||
for db in POOL.lock().drain(..) {
|
extern "C" {
|
||||||
db.teardown();
|
fn atexit(callback: extern "C" fn()) -> c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[ctor::ctor]
|
||||||
|
fn init() {
|
||||||
|
unsafe {
|
||||||
|
atexit(teardown_db_pool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" fn teardown_db_pool() {
|
||||||
|
std::thread::spawn(|| {
|
||||||
|
block_on(async move {
|
||||||
|
for db in POOL.lock().drain(..) {
|
||||||
|
db.teardown().await.log_err();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.log_err();
|
||||||
|
}
|
||||||
|
|
||||||
impl TestDb {
|
impl TestDb {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut pool = POOL.lock();
|
let mut pool = POOL.lock();
|
||||||
@ -606,22 +625,20 @@ pub mod tests {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn teardown(mut self) {
|
async fn teardown(mut self) -> Result<()> {
|
||||||
let db = self.db.take().unwrap();
|
let db = self.db.take().unwrap();
|
||||||
block_on(async {
|
let query = "
|
||||||
let query = "
|
|
||||||
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
||||||
FROM pg_stat_activity
|
FROM pg_stat_activity
|
||||||
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
|
WHERE pg_stat_activity.datname = '{}' AND pid <> pg_backend_pid();
|
||||||
";
|
";
|
||||||
sqlx::query(query)
|
sqlx::query(query)
|
||||||
.bind(&self.name)
|
.bind(&self.name)
|
||||||
.execute(&db.pool)
|
.execute(&db.pool)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
db.pool.close().await;
|
||||||
db.pool.close().await;
|
Postgres::drop_database(&self.url).await?;
|
||||||
Postgres::drop_database(&self.url).await.unwrap();
|
Ok(())
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user