File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -300,16 +300,18 @@ class Pool {
300300 async _purgeKey ( key ) {
301301 const pool = this . _pools [ key ] || [ ]
302302 const poolState = this . _poolState [ key ] || new PoolState ( )
303+ const destructionList = [ ]
303304 while ( pool . length ) {
304305 const resource = pool . pop ( )
305306 if ( this . _removeIdleObserver ) {
306307 this . _removeIdleObserver ( resource )
307308 }
308- await this . _destroy ( resource )
309+ destructionList . push ( this . _destroy ( resource ) )
309310 }
310311 poolState . close ( )
311312 delete this . _pools [ key ]
312313 delete this . _poolState [ key ]
314+ await Promise . all ( destructionList )
313315 }
314316
315317 _processPendingAcquireRequests ( address ) {
Original file line number Diff line number Diff line change @@ -923,6 +923,43 @@ describe('#unit Pool', () => {
923923 expect ( resource1 . observer ) . toBeFalsy ( )
924924 expect ( resource2 . observer ) . toBeFalsy ( )
925925 } )
926+
927+ it ( 'should purge resources in parallel' , async ( ) => {
928+ const address = ServerAddress . fromUrl ( 'bolt://localhost:7687' )
929+ let resourceCount = 0
930+ const resourcesReleased = [ ]
931+ let resolveRelease
932+ const releasePromise = new Promise ( ( resolve ) => {
933+ resolveRelease = resolve
934+ } )
935+
936+ const pool = new Pool ( {
937+ create : ( server , release ) =>
938+ Promise . resolve ( new Resource ( server , resourceCount ++ , release ) ) ,
939+ destroy : res => {
940+ resourcesReleased . push ( res )
941+ resourceCount --
942+ // Only destroy when the last resource
943+ // get destroyed
944+ if ( resourceCount === 0 ) {
945+ resolveRelease ( )
946+ }
947+ return releasePromise
948+ } ,
949+ validate : res => true ,
950+ } )
951+
952+ const resource1 = await pool . acquire ( address )
953+ const resource2 = await pool . acquire ( address )
954+ await resource1 . close ( )
955+ await resource2 . close ( )
956+
957+ await pool . purge ( address )
958+
959+ expect ( resourcesReleased ) . toEqual ( [
960+ resource2 , resource1
961+ ] )
962+ } )
926963} )
927964
928965function expectNoPendingAcquisitionRequests ( pool ) {
You can’t perform that action at this time.
0 commit comments