@@ -24,6 +24,10 @@ class TestablePgPoolStrategy extends PgPoolStrategy {
2424 setCurrentPoolForTest ( pool : Pool ) : void {
2525 this . pool = pool
2626 }
27+
28+ getCachedExecutorPoolForTest ( ) : Pool | undefined {
29+ return Reflect . get ( this , 'executorPool' ) as Pool | undefined
30+ }
2731}
2832
2933function createPoolStrategySettings (
@@ -2141,3 +2145,39 @@ describe('PgTenantConnection payload serialization', () => {
21412145 }
21422146 } )
21432147} )
2148+
2149+ describe ( 'PgPoolStrategy executor reuse' , ( ) => {
2150+ it ( 'reuses one executor per physical pool and rebuilds it when the pool is replaced' , ( ) => {
2151+ const strategy = new TestablePgPoolStrategy ( createPoolStrategySettings ( ) )
2152+ const poolA = { options : { max : 8 } } as unknown as Pool
2153+ strategy . setCurrentPoolForTest ( poolA )
2154+
2155+ const first = strategy . acquire ( )
2156+ expect ( strategy . acquire ( ) ) . toBe ( first )
2157+
2158+ strategy . rebalance ( { maxConnections : 4 } )
2159+ expect ( strategy . acquire ( ) ) . toBe ( first )
2160+
2161+ const poolB = { options : { max : 8 } } as unknown as Pool
2162+ strategy . setCurrentPoolForTest ( poolB )
2163+ const replacement = strategy . acquire ( )
2164+ expect ( replacement ) . not . toBe ( first )
2165+ expect ( strategy . acquire ( ) ) . toBe ( replacement )
2166+ } )
2167+
2168+ it ( 'releases the cached executor pool when the strategy is destroyed' , async ( ) => {
2169+ const strategy = new TestablePgPoolStrategy ( createPoolStrategySettings ( ) )
2170+ const pool = {
2171+ options : { max : 8 } ,
2172+ end : vi . fn ( ) . mockResolvedValue ( undefined ) ,
2173+ } as unknown as Pool
2174+ strategy . setCurrentPoolForTest ( pool )
2175+
2176+ strategy . acquire ( )
2177+ expect ( strategy . getCachedExecutorPoolForTest ( ) ) . toBe ( pool )
2178+
2179+ await strategy . destroy ( )
2180+
2181+ expect ( strategy . getCachedExecutorPoolForTest ( ) ) . toBeUndefined ( )
2182+ } )
2183+ } )
0 commit comments