|
| 1 | +#![cfg(feature = "managed")] |
| 2 | + |
| 3 | +use std::convert::Infallible; |
| 4 | + |
| 5 | +use deadpool::managed::{self, Metrics, Object, RecycleResult}; |
| 6 | + |
| 7 | +type Pool = managed::Pool<Manager, Object<Manager>>; |
| 8 | + |
| 9 | +struct Manager {} |
| 10 | + |
| 11 | +impl managed::Manager for Manager { |
| 12 | + type Type = (); |
| 13 | + type Error = Infallible; |
| 14 | + |
| 15 | + async fn create(&self) -> Result<(), Infallible> { |
| 16 | + Ok(()) |
| 17 | + } |
| 18 | + |
| 19 | + async fn recycle(&self, _conn: &mut (), _: &Metrics) -> RecycleResult<Infallible> { |
| 20 | + Ok(()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[tokio::test] |
| 25 | +async fn test_grow_reuse_existing() { |
| 26 | + // Shrink doesn't discard objects currently borrowed from the pool but |
| 27 | + // keeps track of them so that repeatedly growing and shrinking will |
| 28 | + // not cause excessive object creation. This logic used to contain a bug |
| 29 | + // causing an overflow. |
| 30 | + let mgr = Manager {}; |
| 31 | + let pool = Pool::builder(mgr).max_size(2).build().unwrap(); |
| 32 | + let obj1 = pool.get().await.unwrap(); |
| 33 | + let obj2 = pool.get().await.unwrap(); |
| 34 | + assert!(pool.status().size == 2); |
| 35 | + assert!(pool.status().max_size == 2); |
| 36 | + pool.resize(0); |
| 37 | + // At this point the two objects are still tracked |
| 38 | + assert!(pool.status().size == 2); |
| 39 | + assert!(pool.status().max_size == 0); |
| 40 | + pool.resize(1); |
| 41 | + // Only one of the objects should be returned to the pool |
| 42 | + assert!(pool.status().size == 2); |
| 43 | + assert!(pool.status().max_size == 1); |
| 44 | + drop(obj1); |
| 45 | + // The first drop brings the size to 1. |
| 46 | + assert!(pool.status().size == 1); |
| 47 | + assert!(pool.status().max_size == 1); |
| 48 | + drop(obj2); |
| 49 | + assert!(pool.status().size == 1); |
| 50 | + assert!(pool.status().max_size == 1); |
| 51 | +} |
0 commit comments