Skip to content

Commit

Permalink
Add a test for the previous patch
Browse files Browse the repository at this point in the history
  • Loading branch information
espindola committed Dec 26, 2023
1 parent ef20c7e commit fce9553
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ version = "0.25.0"
optional = true

[dev-dependencies]
waker-fn = "1"
tempfile = "3.1.0"
socket2 = { version = "0.5.2", features = ["all"] }
tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread"] }
Expand Down
46 changes: 45 additions & 1 deletion src/conn/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,14 @@ mod test {
poll, try_join, FutureExt,
};
use tokio::time::{sleep, timeout};
use waker_fn::waker_fn;

use std::{
cmp::Reverse,
task::{Poll, RawWaker, RawWakerVTable, Waker},
future::Future,
pin::pin,
sync::{Arc, OnceLock},
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
time::Duration,
};

Expand Down Expand Up @@ -1047,6 +1051,46 @@ mod test {
Ok(())
}

#[tokio::test]
async fn save_last_waker() {
// Test that if passed multiple wakers, we call the last one.

let pool = pool_with_one_connection();

// Get a connection, so we know the next future will be
// queued.
let conn = pool.get_conn().await.unwrap();
let mut pending_fut = pin!(pool.get_conn());

let build_waker = || {
let called = Arc::new(OnceLock::new());
let called2 = called.clone();
let waker = waker_fn(move || called2.set(()).unwrap());
(called, waker)
};

let mut assert_pending = |waker| {
let mut context = Context::from_waker(&waker);
let p = pending_fut.as_mut().poll(&mut context);
assert!(matches!(p, Poll::Pending));
};

let (first_called, waker) = build_waker();
assert_pending(waker);

let (second_called, waker) = build_waker();
assert_pending(waker);

drop(conn);

while second_called.get().is_none() {
assert!(first_called.get().is_none());
tokio::time::sleep(Duration::from_millis(100)).await;
}

assert!(first_called.get().is_none());
}

#[tokio::test]
async fn check_priorities() -> super::Result<()> {
let pool = pool_with_one_connection();
Expand Down

0 comments on commit fce9553

Please sign in to comment.