@@ -10,6 +10,9 @@ const DEFAULT_SCHEMA: &str = "public";
10
10
11
11
#[ derive( Debug , Clone ) ]
12
12
pub struct Pool {
13
+ #[ cfg( test) ]
14
+ pool : Arc < std:: sync:: Mutex < Option < r2d2:: Pool < PostgresConnectionManager < NoTls > > > > > ,
15
+ #[ cfg( not( test) ) ]
13
16
pool : r2d2:: Pool < PostgresConnectionManager < NoTls > > ,
14
17
metrics : Arc < Metrics > ,
15
18
max_size : u32 ,
@@ -43,14 +46,31 @@ impl Pool {
43
46
. map_err ( PoolError :: PoolCreationFailed ) ?;
44
47
45
48
Ok ( Pool {
49
+ #[ cfg( test) ]
50
+ pool : Arc :: new ( std:: sync:: Mutex :: new ( Some ( pool) ) ) ,
51
+ #[ cfg( not( test) ) ]
46
52
pool,
47
53
metrics,
48
54
max_size : config. max_pool_size ,
49
55
} )
50
56
}
51
57
58
+ fn with_pool < R > (
59
+ & self ,
60
+ f : impl FnOnce ( & r2d2:: Pool < PostgresConnectionManager < NoTls > > ) -> R ,
61
+ ) -> R {
62
+ #[ cfg( test) ]
63
+ {
64
+ f ( & self . pool . lock ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) )
65
+ }
66
+ #[ cfg( not( test) ) ]
67
+ {
68
+ f ( & self . pool )
69
+ }
70
+ }
71
+
52
72
pub fn get ( & self ) -> Result < PoolClient , PoolError > {
53
- match self . pool . get ( ) {
73
+ match self . with_pool ( |p| p . get ( ) ) {
54
74
Ok ( conn) => Ok ( conn) ,
55
75
Err ( err) => {
56
76
self . metrics . failed_db_connections . inc ( ) ;
@@ -60,16 +80,21 @@ impl Pool {
60
80
}
61
81
62
82
pub ( crate ) fn used_connections ( & self ) -> u32 {
63
- self . pool . state ( ) . connections - self . pool . state ( ) . idle_connections
83
+ self . with_pool ( |p| p . state ( ) . connections - p . state ( ) . idle_connections )
64
84
}
65
85
66
86
pub ( crate ) fn idle_connections ( & self ) -> u32 {
67
- self . pool . state ( ) . idle_connections
87
+ self . with_pool ( |p| p . state ( ) . idle_connections )
68
88
}
69
89
70
90
pub ( crate ) fn max_size ( & self ) -> u32 {
71
91
self . max_size
72
92
}
93
+
94
+ #[ cfg( test) ]
95
+ pub ( crate ) fn shutdown ( & self ) {
96
+ self . pool . lock ( ) . unwrap ( ) . take ( ) ;
97
+ }
73
98
}
74
99
75
100
#[ derive( Debug ) ]
0 commit comments