@@ -3120,24 +3120,156 @@ describe.each([
3120
3120
} )
3121
3121
} )
3122
3122
3123
- function setup ( { createConnection, authenticationProvider } = { } ) {
3123
+ describe ( 'param.destroy' , ( ) => {
3124
+ it ( 'should close connection and unregister it' , async ( ) => {
3125
+ const { create, destroy, provider } = setup ( )
3126
+ const openConnections = { ...provider . _openConnections }
3127
+ const connection = await create ( { } , server0 , undefined )
3128
+
3129
+ await destroy ( connection )
3130
+
3131
+ expect ( connection . _closed ) . toBe ( true )
3132
+ expect ( provider . _openConnections ) . toEqual ( openConnections )
3133
+ } )
3134
+ } )
3135
+
3136
+ describe ( 'param.validateOnAcquire' , ( ) => {
3137
+ it . each ( [
3138
+ null ,
3139
+ undefined ,
3140
+ { scheme : 'bearer' , credentials : 'token01' }
3141
+ ] ) ( 'should return true when connection is open and within the lifetime and authentication succeed (auth=%o)' , async ( auth ) => {
3142
+ const connection = new FakeConnection ( server0 )
3143
+ connection . creationTimestamp = Date . now ( )
3144
+
3145
+ const { validateOnAcquire, authenticationProviderHook } = setup ( )
3146
+
3147
+ await expect ( validateOnAcquire ( { auth } , connection ) ) . resolves . toBe ( true )
3148
+
3149
+ expect ( authenticationProviderHook . authenticate ) . toHaveBeenCalledWith ( {
3150
+ connection, auth
3151
+ } )
3152
+ } )
3153
+
3154
+ it . each ( [
3155
+ null ,
3156
+ undefined ,
3157
+ { scheme : 'bearer' , credentials : 'token01' }
3158
+ ] ) ( 'should return true when connection is open and within the lifetime and authentication fails (auth=%o)' , async ( auth ) => {
3159
+ const connection = new FakeConnection ( server0 )
3160
+ const error = newError ( 'failed' )
3161
+ const authenticationProvider = jest . fn ( ( ) => Promise . reject ( error ) )
3162
+ connection . creationTimestamp = Date . now ( )
3163
+
3164
+ const { validateOnAcquire, authenticationProviderHook, log } = setup ( { authenticationProvider } )
3165
+
3166
+ await expect ( validateOnAcquire ( { auth } , connection ) ) . resolves . toBe ( false )
3167
+
3168
+ expect ( authenticationProviderHook . authenticate ) . toHaveBeenCalledWith ( {
3169
+ connection, auth
3170
+ } )
3171
+
3172
+ expect ( log . debug ) . toHaveBeenCalledWith (
3173
+ `The connection ${ connection . id } is not valid because of an error ${ error . code } '${ error . message } '`
3174
+ )
3175
+ } )
3176
+ it ( 'should return false when connection is closed and within the lifetime' , async ( ) => {
3177
+ const connection = new FakeConnection ( server0 )
3178
+ connection . creationTimestamp = Date . now ( )
3179
+ await connection . close ( )
3180
+
3181
+ const { validateOnAcquire, authenticationProviderHook } = setup ( )
3182
+
3183
+ await expect ( validateOnAcquire ( { } , connection ) ) . resolves . toBe ( false )
3184
+ expect ( authenticationProviderHook . authenticate ) . not . toHaveBeenCalled ( )
3185
+ } )
3186
+
3187
+ it ( 'should return false when connection is open and out of the lifetime' , async ( ) => {
3188
+ const connection = new FakeConnection ( server0 )
3189
+ connection . creationTimestamp = Date . now ( ) - 4000
3190
+
3191
+ const { validateOnAcquire, authenticationProviderHook } = setup ( { maxConnectionLifetime : 3000 } )
3192
+
3193
+ await expect ( validateOnAcquire ( { } , connection ) ) . resolves . toBe ( false )
3194
+ expect ( authenticationProviderHook . authenticate ) . not . toHaveBeenCalled ( )
3195
+ } )
3196
+
3197
+ it ( 'should return false when connection is closed and out of the lifetime' , async ( ) => {
3198
+ const connection = new FakeConnection ( server0 )
3199
+ await connection . close ( )
3200
+ connection . creationTimestamp = Date . now ( ) - 4000
3201
+
3202
+ const { validateOnAcquire, authenticationProviderHook } = setup ( { maxConnectionLifetime : 3000 } )
3203
+
3204
+ await expect ( validateOnAcquire ( { } , connection ) ) . resolves . toBe ( false )
3205
+ expect ( authenticationProviderHook . authenticate ) . not . toHaveBeenCalled ( )
3206
+ } )
3207
+ } )
3208
+
3209
+ describe ( 'param.validateOnRelease' , ( ) => {
3210
+ it ( 'should return true when connection is open and within the lifetime' , ( ) => {
3211
+ const connection = new FakeConnection ( server0 )
3212
+ connection . creationTimestamp = Date . now ( )
3213
+
3214
+ const { validateOnRelease } = setup ( )
3215
+
3216
+ expect ( validateOnRelease ( connection ) ) . toBe ( true )
3217
+ } )
3218
+
3219
+ it ( 'should return false when connection is closed and within the lifetime' , async ( ) => {
3220
+ const connection = new FakeConnection ( server0 )
3221
+ connection . creationTimestamp = Date . now ( )
3222
+ await connection . close ( )
3223
+
3224
+ const { validateOnRelease } = setup ( )
3225
+
3226
+ expect ( validateOnRelease ( connection ) ) . toBe ( false )
3227
+ } )
3228
+
3229
+ it ( 'should return false when connection is open and out of the lifetime' , ( ) => {
3230
+ const connection = new FakeConnection ( server0 )
3231
+ connection . creationTimestamp = Date . now ( ) - 4000
3232
+
3233
+ const { validateOnRelease } = setup ( { maxConnectionLifetime : 3000 } )
3234
+
3235
+ expect ( validateOnRelease ( connection ) ) . toBe ( false )
3236
+ } )
3237
+
3238
+ it ( 'should return false when connection is closed and out of the lifetime' , async ( ) => {
3239
+ const connection = new FakeConnection ( server0 )
3240
+ await connection . close ( )
3241
+ connection . creationTimestamp = Date . now ( ) - 4000
3242
+
3243
+ const { validateOnRelease } = setup ( { maxConnectionLifetime : 3000 } )
3244
+
3245
+ expect ( validateOnRelease ( connection ) ) . toBe ( false )
3246
+ } )
3247
+ } )
3248
+
3249
+ function setup ( { createConnection, authenticationProvider, maxConnectionLifetime } = { } ) {
3124
3250
const newPool = jest . fn ( ( ...args ) => new Pool ( ...args ) )
3251
+ const log = new Logger ( 'debug' , ( ) => undefined )
3252
+ jest . spyOn ( log , 'debug' )
3125
3253
const createChannelConnectionHook = createConnection || jest . fn ( async ( address ) => new FakeConnection ( address ) )
3126
3254
const authenticationProviderHook = new AuthenticationProvider ( { } )
3127
3255
jest . spyOn ( authenticationProviderHook , 'authenticate' )
3128
3256
. mockImplementation ( authenticationProvider || jest . fn ( ( { connection } ) => Promise . resolve ( connection ) ) )
3129
3257
const provider = new RoutingConnectionProvider ( {
3130
3258
newPool,
3131
- config : { } ,
3132
- address : server01
3259
+ config : {
3260
+ maxConnectionLifetime : maxConnectionLifetime || 1000
3261
+ } ,
3262
+ address : server01 ,
3263
+ log
3133
3264
} )
3134
3265
provider . _createChannelConnection = createChannelConnectionHook
3135
3266
provider . _authenticationProvider = authenticationProviderHook
3136
3267
return {
3137
3268
provider,
3138
3269
...newPool . mock . calls [ 0 ] [ 0 ] ,
3139
3270
createChannelConnectionHook,
3140
- authenticationProviderHook
3271
+ authenticationProviderHook,
3272
+ log
3141
3273
}
3142
3274
}
3143
3275
} )
@@ -3348,6 +3480,10 @@ class FakeConnection extends Connection {
3348
3480
this . _closed = true
3349
3481
}
3350
3482
3483
+ isOpen ( ) {
3484
+ return ! this . _closed
3485
+ }
3486
+
3351
3487
protocol ( ) {
3352
3488
return {
3353
3489
version : this . _protocolVersion ,
0 commit comments