Skip to content

Commit bfa8dbb

Browse files
committed
Add tests to the RoutingConnectionProvider pool configuration
1 parent ff4281a commit bfa8dbb

File tree

2 files changed

+141
-5
lines changed

2 files changed

+141
-5
lines changed

packages/bolt-connection/src/connection-provider/connection-provider-pooled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export default class PooledConnectionProvider extends ConnectionProvider {
9595
await this._authenticationProvider.authenticate({ connection: conn, auth })
9696
return true
9797
} catch (error) {
98-
this._log.info(
98+
this._log.debug(
9999
`The connection ${conn.id} is not valid because of an error ${error.code} '${error.message}'`
100100
)
101101
return false

packages/bolt-connection/test/connection-provider/connection-provider-routing.test.js

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,24 +3120,156 @@ describe.each([
31203120
})
31213121
})
31223122

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 } = {}) {
31243250
const newPool = jest.fn((...args) => new Pool(...args))
3251+
const log = new Logger('debug', () => undefined)
3252+
jest.spyOn(log, 'debug')
31253253
const createChannelConnectionHook = createConnection || jest.fn(async (address) => new FakeConnection(address))
31263254
const authenticationProviderHook = new AuthenticationProvider({ })
31273255
jest.spyOn(authenticationProviderHook, 'authenticate')
31283256
.mockImplementation(authenticationProvider || jest.fn(({ connection }) => Promise.resolve(connection)))
31293257
const provider = new RoutingConnectionProvider({
31303258
newPool,
3131-
config: {},
3132-
address: server01
3259+
config: {
3260+
maxConnectionLifetime: maxConnectionLifetime || 1000
3261+
},
3262+
address: server01,
3263+
log
31333264
})
31343265
provider._createChannelConnection = createChannelConnectionHook
31353266
provider._authenticationProvider = authenticationProviderHook
31363267
return {
31373268
provider,
31383269
...newPool.mock.calls[0][0],
31393270
createChannelConnectionHook,
3140-
authenticationProviderHook
3271+
authenticationProviderHook,
3272+
log
31413273
}
31423274
}
31433275
})
@@ -3348,6 +3480,10 @@ class FakeConnection extends Connection {
33483480
this._closed = true
33493481
}
33503482

3483+
isOpen () {
3484+
return !this._closed
3485+
}
3486+
33513487
protocol () {
33523488
return {
33533489
version: this._protocolVersion,

0 commit comments

Comments
 (0)