Skip to content

Commit

Permalink
force delete database
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Feb 22, 2025
1 parent 268456e commit e857490
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
28 changes: 14 additions & 14 deletions crates/kitsune-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ where
.unwrap()
}

pub async fn database_test<F, O>(func: F) -> O
pub async fn database_test<F, Fut>(func: F) -> Fut::Output
where
F: AsyncFnOnce(PgPool) -> O,
F: FnOnce(PgPool) -> Fut,
Fut: Future,
{
let db_url = env::var("DATABASE_URL").unwrap();
let mut url = Url::parse(&db_url).unwrap();
Expand Down Expand Up @@ -63,13 +64,10 @@ where
.expect("Failed to connect to database");

provide_resource(pool, func, async |_pool| {
// Drop the newly created database. We don't need it anymore.
if let Err(error) = admin_conn
.batch_execute(&format!("DROP DATABASE {db_name}"))
admin_conn
.batch_execute(&format!("DROP DATABASE {db_name} WITH (FORCE)"))
.await
{
eprintln!("failed to drop database. sorry. error: {error:?}");
}
.unwrap();
})
.await
}
Expand All @@ -82,9 +80,10 @@ pub fn language_detection_config() -> language_detection::Configuration {
}
}

pub async fn minio_test<F, O>(func: F) -> O
pub async fn minio_test<F, Fut>(func: F) -> Fut::Output
where
F: AsyncFnOnce(Arc<kitsune_s3::Client>) -> O,
F: FnOnce(Arc<kitsune_s3::Client>) -> Fut,
Fut: Future,
{
let endpoint = env::var("MINIO_URL").unwrap();
let endpoint = endpoint.parse().unwrap();
Expand All @@ -107,15 +106,16 @@ where

client.create_bucket().await.unwrap();

provide_resource(client, func, async |client| {
provide_resource(client, func, |client| async move {
client.delete_bucket().await.unwrap();
})
.await
}

pub async fn redis_test<F, O>(func: F) -> O
pub async fn redis_test<F, Fut>(func: F) -> Fut::Output
where
F: AsyncFnOnce(RedisPool) -> O,
F: FnOnce(RedisPool) -> Fut,
Fut: Future,
{
let redis_url = env::var("REDIS_URL").unwrap();
let mut config = RedisConfig::from_url(&redis_url).unwrap();
Expand All @@ -126,7 +126,7 @@ where
let pool = RedisPool::new(config, None, None, None, 5).unwrap();
pool.init().await.unwrap();

provide_resource(pool, func, async |pool| {
provide_resource(pool, func, |pool| async move {
pool.custom::<(), ()>(fred::cmd!("FLUSHDB"), vec![])
.await
.unwrap();
Expand Down
10 changes: 6 additions & 4 deletions crates/kitsune-test/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use std::panic;

/// Provide a resource to the `run` closure, catch any panics that may occur while polling the future returned by `run`,
/// then run the `cleanup` closure, and resume any panic unwinds that were caught
pub async fn provide_resource<Resource, Run, Cleanup, RunOutput>(
pub async fn provide_resource<Resource, Run, Cleanup, RunFut, CleanupFut>(
resource: Resource,
run: Run,
cleanup: Cleanup,
) -> RunOutput
) -> RunFut::Output
where
Resource: Clone,
Run: AsyncFnOnce(Resource) -> RunOutput,
Cleanup: AsyncFnOnce(Resource),
Run: FnOnce(Resource) -> RunFut,
RunFut: Future,
Cleanup: FnOnce(Resource) -> CleanupFut,
CleanupFut: Future,
{
let out = CatchPanic::new(run(resource.clone())).await;
cleanup(resource).await;
Expand Down

0 comments on commit e857490

Please sign in to comment.