From 26ae04bc6585482709171d44590c4902317a6024 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Fri, 26 Jan 2024 12:50:41 +0100 Subject: [PATCH] Add tests for deadlock --- bb8/tests/test.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/bb8/tests/test.rs b/bb8/tests/test.rs index 69853ed..43e233d 100644 --- a/bb8/tests/test.rs +++ b/bb8/tests/test.rs @@ -317,6 +317,55 @@ async fn test_get_timeout() { ready(r).await.unwrap(); } +#[tokio::test] +async fn test_lots_of_waiters() { + let pool = Pool::builder() + .max_size(3) + .connection_timeout(Duration::from_millis(5_000)) + .build(OkManager::::new()) + .await + .unwrap(); + + let mut waiters: Vec> = Vec::new(); + + for _ in 0..25000 { + let pool = pool.clone(); + let (tx, rx) = oneshot::channel(); + waiters.push(rx); + tokio::spawn(async move { + let _conn = pool.get().await.unwrap(); + tx.send(()).unwrap(); + }); + } + + let results = futures_util::future::join_all(&mut waiters).await; + + for result in results { + assert!(result.is_ok()); + } +} + +#[tokio::test] +async fn test_timeout_caller() { + let pool = Pool::builder() + .max_size(1) + .connection_timeout(Duration::from_millis(5_000)) + .build(OkManager::::new()) + .await + .unwrap(); + + let one = pool.get().await; + assert!(one.is_ok()); + + let res = tokio::time::timeout(Duration::from_millis(100), pool.get()).await; + assert!(res.is_err()); + + drop(one); + + let two = pool.get().await; + assert!(two.is_ok()); +} + #[tokio::test] async fn test_now_invalid() { static INVALID: AtomicBool = AtomicBool::new(false);