From 581668c30655595e1ea523631171f823f546a28d Mon Sep 17 00:00:00 2001 From: naskya Date: Mon, 10 Jun 2024 18:09:05 +0900 Subject: [PATCH] chore (backend-rs): more connection checks --- .../backend-rs/src/database/postgresql.rs | 33 +++++++++++++++++-- packages/backend-rs/src/database/redis.rs | 33 +++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/packages/backend-rs/src/database/postgresql.rs b/packages/backend-rs/src/database/postgresql.rs index 2e9a76126e..85b79d0d74 100644 --- a/packages/backend-rs/src/database/postgresql.rs +++ b/packages/backend-rs/src/database/postgresql.rs @@ -41,8 +41,35 @@ mod unit_test { use super::get_conn; #[tokio::test] - async fn connect() { - assert!(get_conn().await.is_ok()); - assert!(get_conn().await.is_ok()); + async fn connect_sequential() { + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + } + + #[tokio::test] + async fn connect_concurrent() { + let [c1, c2, c3, c4, c5] = [ + get_conn(), + get_conn(), + get_conn(), + get_conn(), + get_conn(), + ]; + let _ = tokio::try_join!(c1, c2, c3, c4, c5).unwrap(); + } + + #[tokio::test] + async fn connect_spawn() { + let mut tasks = Vec::new(); + + for _ in 0..5 { + tasks.push(tokio::spawn(get_conn())); + } + for task in tasks { + task.await.unwrap().unwrap(); + } } } diff --git a/packages/backend-rs/src/database/redis.rs b/packages/backend-rs/src/database/redis.rs index 266e563c8d..7f077e27a6 100644 --- a/packages/backend-rs/src/database/redis.rs +++ b/packages/backend-rs/src/database/redis.rs @@ -119,9 +119,36 @@ mod unit_test { use redis::AsyncCommands; #[tokio::test] - async fn connect() { - assert!(get_conn().await.is_ok()); - assert!(get_conn().await.is_ok()); + async fn connect_sequential() { + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + get_conn().await.unwrap(); + } + + #[tokio::test] + async fn connect_concurrent() { + let [c1, c2, c3, c4, c5] = [ + get_conn(), + get_conn(), + get_conn(), + get_conn(), + get_conn(), + ]; + let _ = tokio::try_join!(c1, c2, c3, c4, c5).unwrap(); + } + + #[tokio::test] + async fn connect_spawn() { + let mut tasks = Vec::new(); + + for _ in 0..5 { + tasks.push(tokio::spawn(get_conn())); + } + for task in tasks { + task.await.unwrap().unwrap(); + } } #[tokio::test]