Skip to content

Commit 9b78bb0

Browse files
committed
Add test for reproducing panics on resize
See #380
1 parent 24673ad commit 9b78bb0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/managed_resize.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)