@@ -969,10 +969,7 @@ describe('#unit Pool', () => {
969
969
await conn2 . release ( address , conn2 )
970
970
} )
971
971
972
- it . each ( [
973
- [ 'is not valid' , ( promise : any ) => promise . resolve ( false ) ] ,
974
- [ 'validation fails' , ( promise : any ) => promise . reject ( new Error ( 'failed' ) ) ]
975
- ] ) ( 'should create new connection if the current one when %s' , async ( _ , resolver ) => {
972
+ it ( 'should create new connection if the current one breaks due to being invalid' , async ( ) => {
976
973
const conns : any [ ] = [ ]
977
974
const pool = new Pool < any > ( {
978
975
// Hook into connection creation to track when and what connections that are
@@ -1020,7 +1017,7 @@ describe('#unit Pool', () => {
1020
1017
expect ( conns . length ) . toEqual ( 1 )
1021
1018
1022
1019
// should resolve the promise with the configured value
1023
- resolver ( conns [ 0 ] . promises [ 0 ] )
1020
+ conns [ 0 ] . promises [ 0 ] . resolve ( false )
1024
1021
1025
1022
// getting the connection 1
1026
1023
const conn1 = await req0
@@ -1038,6 +1035,63 @@ describe('#unit Pool', () => {
1038
1035
expect ( conns . length ) . toEqual ( 2 )
1039
1036
} )
1040
1037
1038
+ it ( 'should create new connection if the current one breaks from error during validation' , async ( ) => {
1039
+ const conns : any [ ] = [ ]
1040
+ const pool = new Pool < any > ( {
1041
+ // Hook into connection creation to track when and what connections that are
1042
+ // created.
1043
+ create : async ( _ , server , release ) => {
1044
+ // Create a fake connection that makes it possible control when it's connected
1045
+ // and released from the outer scope.
1046
+ const conn : any = {
1047
+ server,
1048
+ release
1049
+ }
1050
+ conns . push ( conn )
1051
+ return conn
1052
+ } ,
1053
+ validateOnAcquire : async ( context , resource : any ) => {
1054
+ const promise = new Promise < boolean > ( ( resolve , reject ) => {
1055
+ if ( resource . promises == null ) {
1056
+ resource . promises = [ ]
1057
+ }
1058
+ resource . promises . push ( {
1059
+ resolve,
1060
+ reject
1061
+ } )
1062
+ } )
1063
+
1064
+ return await promise
1065
+ } ,
1066
+ // Setup pool to only allow one connection
1067
+ config : new PoolConfig ( 1 , 100000 )
1068
+ } )
1069
+ // Make the first request for a connection, this will return a connection instantaneously
1070
+ const conn0 = await pool . acquire ( { } , address )
1071
+ expect ( conns . length ) . toEqual ( 1 )
1072
+
1073
+ // Releasing connection back to the pool, so it can be re-acquired.
1074
+ await conn0 . release ( address , conn0 )
1075
+
1076
+ // Request the same connection again, it will wait until resolve get called.
1077
+ const req0 = pool . acquire ( { } , address )
1078
+ expect ( conns . length ) . toEqual ( 1 )
1079
+
1080
+ // should resolve the promise with the configured value
1081
+ conns [ 0 ] . promises [ 0 ] . reject ( new Error ( 'Failed' ) )
1082
+ await expect ( async ( ) => await req0 ) . rejects . toThrow ( )
1083
+
1084
+ // Request other connection, this should also resolve the same connection.
1085
+ const conn2 = await pool . acquire ( { } , address )
1086
+ expect ( conns . length ) . toEqual ( 2 )
1087
+
1088
+ await conn2 . release ( address , conn2 )
1089
+ expect ( conns . length ) . toEqual ( 2 )
1090
+ expect ( conn0 ) . not . toBe ( conn2 )
1091
+ expect ( idleResources ( pool , address ) ) . toBe ( 1 )
1092
+ expect ( resourceInUse ( pool , address ) ) . toBe ( 0 )
1093
+ } )
1094
+
1041
1095
it ( 'should not time out if max pool size is not set' , async ( ) => {
1042
1096
let counter = 0
1043
1097
0 commit comments