Skip to content

Commit

Permalink
Add reusable connection manager for broken connections
Browse files Browse the repository at this point in the history
  • Loading branch information
tneely committed Aug 27, 2024
1 parent a8d19cf commit 551915e
Showing 1 changed file with 36 additions and 45 deletions.
81 changes: 36 additions & 45 deletions bb8/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ where
}
}

struct BrokenConnectionManager<C> {
_c: PhantomData<C>,
}

impl<C> BrokenConnectionManager<C> {
fn new() -> Self {
BrokenConnectionManager { _c: PhantomData }
}
}

#[async_trait]
impl<C> ManageConnection for BrokenConnectionManager<C>
where
C: Default + Send + Sync + 'static,
{
type Connection = C;
type Error = Error;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
Ok(Default::default())
}

async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
Ok(())
}

fn has_broken(&self, _: &mut Self::Connection) -> bool {
true
}
}

#[tokio::test]
async fn test_max_size_ok() {
let manager = NthConnectionFailManager::<FakeConnection>::new(5);
Expand Down Expand Up @@ -220,27 +251,10 @@ async fn test_drop_on_broken() {
}
}

struct Handler;

#[async_trait]
impl ManageConnection for Handler {
type Connection = Connection;
type Error = Error;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
Ok(Default::default())
}

async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
Ok(())
}

fn has_broken(&self, _: &mut Self::Connection) -> bool {
true
}
}

let pool = Pool::builder().build(Handler).await.unwrap();
let pool = Pool::builder()
.build(BrokenConnectionManager::<Connection>::new())
.await
.unwrap();
{
let _ = pool.get().await.unwrap();
}
Expand Down Expand Up @@ -1049,33 +1063,10 @@ async fn test_add_ok_until_max_size() {

#[tokio::test]
async fn test_add_checks_broken_connections() {
#[derive(Default)]
struct Connection;

struct Handler;

#[async_trait]
impl ManageConnection for Handler {
type Connection = Connection;
type Error = Error;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
Ok(Default::default())
}

async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
Ok(())
}

fn has_broken(&self, _: &mut Self::Connection) -> bool {
true
}
}

let pool = Pool::builder()
.min_idle(1)
.max_size(3)
.build(Handler)
.build(BrokenConnectionManager::<FakeConnection>::new())
.await
.unwrap();

Expand Down

0 comments on commit 551915e

Please sign in to comment.