Skip to content

Commit

Permalink
Clean up test databases (#3358)
Browse files Browse the repository at this point in the history
  • Loading branch information
divergentdave authored Aug 8, 2024
1 parent f2ae5c1 commit 99626c4
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion aggregator_core/src/datastore/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
use testcontainers::{runners::AsyncRunner, ContainerRequest, ImageExt};
use tokio::{
io::{AsyncBufRead, AsyncBufReadExt},
join,
join, spawn,
sync::{
oneshot::{self, Sender},
Mutex,
Expand Down Expand Up @@ -165,6 +165,7 @@ pub struct EphemeralDatastore {
pool: Pool,
datastore_key_bytes: Vec<u8>,
migrator: Migrator,
db_name: String,
}

pub const TEST_DATASTORE_MAX_TRANSACTION_RETRIES: u64 = 1000;
Expand Down Expand Up @@ -254,6 +255,21 @@ impl EphemeralDatastore {
}
}

impl Drop for EphemeralDatastore {
fn drop(&mut self) {
// Make a best-effort attempt to delete the database created for this datastore. This may
// fail if the container hosting the database server is deleted faster, which would make
// this moot.
let pool = self.pool.clone();
let db_name = self.db_name.clone();
spawn(async move {
if let Ok(conn) = pool.get().await {
let _ = conn.execute("DROP DATABASE $1", &[&db_name]).await;
}
});
}
}

/// Builder to configure a new [`EphemeralDatastore`].
pub struct EphemeralDatastoreBuilder {
schema_version: i64,
Expand Down Expand Up @@ -383,6 +399,7 @@ impl EphemeralDatastoreBuilder {
pool,
datastore_key_bytes: generate_aead_key_bytes(),
migrator,
db_name,
}
}
}
Expand Down

0 comments on commit 99626c4

Please sign in to comment.