Skip to content

Commit ff4281a

Browse files
committed
Add test for RoutingConnectionProvider resource creation setup (pool)
1 parent c7fdb8f commit ff4281a

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import AuthenticationProvider from './authentication-provider'
2525
const { SERVICE_UNAVAILABLE } = error
2626
export default class PooledConnectionProvider extends ConnectionProvider {
2727
constructor (
28-
{ id, config, log, userAgent, authTokenProvider },
28+
{ id, config, log, userAgent, authTokenProvider, newPool = (...args) => new Pool(...args) },
2929
createChannelConnectionHook = null
3030
) {
3131
super()
@@ -44,7 +44,7 @@ export default class PooledConnectionProvider extends ConnectionProvider {
4444
this._log
4545
)
4646
})
47-
this._connectionPool = new Pool({
47+
this._connectionPool = newPool({
4848
create: this._createConnection.bind(this),
4949
destroy: this._destroyConnection.bind(this),
5050
validateOnAcquire: this._validateConnectionOnAcquire.bind(this),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
6767
log,
6868
userAgent,
6969
authTokenProvider,
70-
routingTablePurgeDelay
70+
routingTablePurgeDelay,
71+
newPool
7172
}) {
72-
super({ id, config, log, userAgent, authTokenProvider }, address => {
73+
super({ id, config, log, userAgent, authTokenProvider, newPool }, address => {
7374
return createChannelConnection(
7475
address,
7576
this._config,

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

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Pool } from '../../src/pool'
3131
import SimpleHostNameResolver from '../../src/channel/browser/browser-host-name-resolver'
3232
import RoutingConnectionProvider from '../../src/connection-provider/connection-provider-routing'
3333
import { DelegateConnection, Connection } from '../../src/connection'
34+
import AuthenticationProvider from '../../src/connection-provider/authentication-provider'
3435

3536
const {
3637
serverAddress: { ServerAddress },
@@ -3047,6 +3048,101 @@ describe.each([
30473048
})
30483049
})
30493050

3051+
describe('constructor', () => {
3052+
describe('newPool', () => {
3053+
describe('param.create', () => {
3054+
it('should create connection', async () => {
3055+
const { create, createChannelConnectionHook, provider } = setup()
3056+
3057+
const connection = await create({}, server0, undefined)
3058+
3059+
expect(createChannelConnectionHook).toHaveBeenCalledWith(server0)
3060+
expect(provider._openConnections[connection.id]).toBe(connection)
3061+
await expect(createChannelConnectionHook.mock.results[0].value).resolves.toBe(connection)
3062+
})
3063+
3064+
it('should register the release function into the connection', async () => {
3065+
const { create } = setup()
3066+
const releaseResult = { property: 'some property' }
3067+
const release = jest.fn(() => releaseResult)
3068+
3069+
const connection = await create({}, server0, release)
3070+
3071+
const released = connection._release()
3072+
3073+
expect(released).toBe(releaseResult)
3074+
expect(release).toHaveBeenCalledWith(server0, connection)
3075+
})
3076+
3077+
it.each([
3078+
null,
3079+
undefined,
3080+
{ scheme: 'bearer', credentials: 'token01' }
3081+
])('should authenticate connection (auth = %o)', async (auth) => {
3082+
const { create, authenticationProviderHook } = setup()
3083+
3084+
const connection = await create({ auth }, server0)
3085+
3086+
expect(authenticationProviderHook.authenticate).toHaveBeenCalledWith({
3087+
connection,
3088+
auth
3089+
})
3090+
})
3091+
3092+
it('should handle create connection failures', async () => {
3093+
const error = newError('some error')
3094+
const createConnection = jest.fn(() => Promise.reject(error))
3095+
const { create, authenticationProviderHook, provider } = setup({ createConnection })
3096+
const openConnections = { ...provider._openConnections }
3097+
3098+
await expect(create({}, server0)).rejects.toThrow(error)
3099+
3100+
expect(authenticationProviderHook.authenticate).not.toHaveBeenCalled()
3101+
expect(provider._openConnections).toEqual(openConnections)
3102+
})
3103+
3104+
it.each([
3105+
null,
3106+
undefined,
3107+
{ scheme: 'bearer', credentials: 'token01' }
3108+
])('should handle authentication failures (auth = %o)', async (auth) => {
3109+
const error = newError('some error')
3110+
const authenticationProvider = jest.fn(() => Promise.reject(error))
3111+
const { create, authenticationProviderHook, createChannelConnectionHook, provider } = setup({ authenticationProvider })
3112+
const openConnections = { ...provider._openConnections }
3113+
3114+
await expect(create({ auth }, server0)).rejects.toThrow(error)
3115+
3116+
const connection = await createChannelConnectionHook.mock.results[0].value
3117+
expect(authenticationProviderHook.authenticate).toHaveBeenCalledWith({ auth, connection })
3118+
expect(provider._openConnections).toEqual(openConnections)
3119+
expect(connection._closed).toBe(true)
3120+
})
3121+
})
3122+
3123+
function setup ({ createConnection, authenticationProvider } = {}) {
3124+
const newPool = jest.fn((...args) => new Pool(...args))
3125+
const createChannelConnectionHook = createConnection || jest.fn(async (address) => new FakeConnection(address))
3126+
const authenticationProviderHook = new AuthenticationProvider({ })
3127+
jest.spyOn(authenticationProviderHook, 'authenticate')
3128+
.mockImplementation(authenticationProvider || jest.fn(({ connection }) => Promise.resolve(connection)))
3129+
const provider = new RoutingConnectionProvider({
3130+
newPool,
3131+
config: {},
3132+
address: server01
3133+
})
3134+
provider._createChannelConnection = createChannelConnectionHook
3135+
provider._authenticationProvider = authenticationProviderHook
3136+
return {
3137+
provider,
3138+
...newPool.mock.calls[0][0],
3139+
createChannelConnectionHook,
3140+
authenticationProviderHook
3141+
}
3142+
}
3143+
})
3144+
})
3145+
30503146
function newPool ({ create, config } = {}) {
30513147
const _create = (address, release) => {
30523148
if (create) {
@@ -3220,6 +3316,12 @@ class FakeConnection extends Connection {
32203316
this.resetAndFlush = jest.fn(() => Promise.resolve())
32213317
this._server = server
32223318
this._authToken = authToken
3319+
this._id = 1
3320+
this._closed = false
3321+
}
3322+
3323+
get id () {
3324+
return this._id
32233325
}
32243326

32253327
get authToken () {
@@ -3243,7 +3345,7 @@ class FakeConnection extends Connection {
32433345
}
32443346

32453347
async close () {
3246-
3348+
this._closed = true
32473349
}
32483350

32493351
protocol () {

0 commit comments

Comments
 (0)