Skip to content

Commit bc442da

Browse files
committed
Add unit tests
1 parent 23375a7 commit bc442da

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const DATABASE_NOT_FOUND_CODE = 'Neo.ClientError.Database.DatabaseNotFound'
4747
const INVALID_BOOKMARK_CODE = 'Neo.ClientError.Transaction.InvalidBookmark'
4848
const INVALID_BOOKMARK_MIXTURE_CODE =
4949
'Neo.ClientError.Transaction.InvalidBookmarkMixture'
50-
const FORBIDEN_CODE = 'Neo.ClientError.Security.Forbidden'
5150
const AUTHORIZATION_EXPIRED_CODE =
5251
'Neo.ClientError.Security.AuthorizationExpired'
5352

@@ -460,7 +459,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
460459
impersonatedUser
461460
)
462461
} catch (error) {
463-
return this._handleRediscoveryError(error)
462+
return this._handleRediscoveryError(error, currentRouter)
464463
} finally {
465464
session.close()
466465
}
@@ -503,11 +502,11 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
503502
impersonatedUser
504503
})
505504
} catch (error) {
506-
return this._handleRediscoveryError(error)
505+
return this._handleRediscoveryError(error, routerAddress)
507506
}
508507
}
509508

510-
_handleRediscoveryError(error) {
509+
_handleRediscoveryError(error, routerAddress) {
511510
if (_isFailFastError(error) || _isFailFastSecurityError(error)) {
512511
throw error
513512
} else if (error.code === PROCEDURE_NOT_FOUND_CODE) {
@@ -661,7 +660,6 @@ class RoutingTableRegistry {
661660
function _isFailFastError (error) {
662661
return [
663662
DATABASE_NOT_FOUND_CODE,
664-
FORBIDEN_CODE,
665663
INVALID_BOOKMARK_CODE,
666664
INVALID_BOOKMARK_MIXTURE_CODE,
667665
].includes(error.code)

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

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,86 @@ describe('#unit RoutingConnectionProvider', () => {
16571657
})
16581658
}, 10000)
16591659

1660+
describe('when rediscovery.lookupRoutingTableOnRouter fails', () => {
1661+
describe.each([
1662+
'Neo.ClientError.Database.DatabaseNotFound',
1663+
'Neo.ClientError.Transaction.InvalidBookmark',
1664+
'Neo.ClientError.Transaction.InvalidBookmarkMixture',
1665+
'Neo.ClientError.Security.Forbidden',
1666+
'Neo.ClientError.Security.IWontTellYou'
1667+
])('with "%s"', errorCode => {
1668+
it('should fail without change the error ', async () => {
1669+
const error = newError('something wrong', errorCode)
1670+
const connectionProvider = newRoutingConnectionProviderWithFakeRediscovery(
1671+
new FakeRediscovery(null, error),
1672+
newPool()
1673+
)
1674+
1675+
let completed = false
1676+
try {
1677+
await connectionProvider.acquireConnection({ accessMode: READ })
1678+
completed = true
1679+
} catch (capturedError) {
1680+
expect(capturedError).toBe(error)
1681+
}
1682+
1683+
expect(completed).toBe(false)
1684+
})
1685+
})
1686+
1687+
describe('with "Neo.ClientError.Procedure.ProcedureNotFound"', () => {
1688+
it('should fail with SERVICE_UNAVAILABLE and a message about do not connect to cause cluster', async () => {
1689+
const error = newError('something wrong', 'Neo.ClientError.Procedure.ProcedureNotFound')
1690+
const connectionProvider = newRoutingConnectionProviderWithFakeRediscovery(
1691+
new FakeRediscovery(null, error),
1692+
newPool()
1693+
)
1694+
1695+
let completed = false
1696+
try {
1697+
await connectionProvider.acquireConnection({ accessMode: READ })
1698+
completed = true
1699+
} catch (capturedError) {
1700+
expect(capturedError.code).toBe(SERVICE_UNAVAILABLE)
1701+
expect(capturedError.message).toBe(
1702+
'Server at server-non-existing-seed-router:7687 can\'t '
1703+
+ 'perform routing. Make sure you are connecting to a causal cluster'
1704+
)
1705+
}
1706+
1707+
expect(completed).toBe(false)
1708+
})
1709+
})
1710+
1711+
describe.each([
1712+
'Neo.ClientError.Security.AuthorizationExpired',
1713+
'Neo.ClientError.MadeUp.Error'
1714+
])('with "%s"', errorCode => {
1715+
it('should fail with SERVICE_UNAVAILABLE and message about no routing servers available ', async () => {
1716+
const error = newError('something wrong', errorCode)
1717+
const connectionProvider = newRoutingConnectionProviderWithFakeRediscovery(
1718+
new FakeRediscovery(null, error),
1719+
newPool()
1720+
)
1721+
1722+
let completed = false
1723+
try {
1724+
await connectionProvider.acquireConnection({ accessMode: READ })
1725+
completed = true
1726+
} catch (capturedError) {
1727+
expect(capturedError.code).toBe(SERVICE_UNAVAILABLE)
1728+
expect(capturedError.message).toEqual(expect.stringContaining(
1729+
'Could not perform discovery. ' +
1730+
'No routing servers available. Known routing table: '
1731+
))
1732+
}
1733+
1734+
expect(completed).toBe(false)
1735+
})
1736+
1737+
})
1738+
})
1739+
16601740
describe('multi-database', () => {
16611741
it.each(usersDataSet)('should acquire read connection from correct routing table [user=%s]', async (user) => {
16621742
const pool = newPool()
@@ -2491,13 +2571,31 @@ function newRoutingConnectionProvider (
24912571
)
24922572
}
24932573

2574+
function newRoutingConnectionProviderWithFakeRediscovery (
2575+
fakeRediscovery,
2576+
pool = null,
2577+
routerToRoutingTable = { null: {} }
2578+
) {
2579+
const seedRouter = ServerAddress.fromUrl('server-non-existing-seed-router')
2580+
return newRoutingConnectionProviderWithSeedRouter(
2581+
seedRouter,
2582+
[seedRouter],
2583+
[],
2584+
routerToRoutingTable,
2585+
pool,
2586+
null,
2587+
fakeRediscovery
2588+
)
2589+
}
2590+
24942591
function newRoutingConnectionProviderWithSeedRouter (
24952592
seedRouter,
24962593
seedRouterResolved,
24972594
routingTables,
24982595
routerToRoutingTable = { null: {} },
24992596
connectionPool = null,
2500-
routingTablePurgeDelay = null
2597+
routingTablePurgeDelay = null,
2598+
fakeRediscovery = null
25012599
) {
25022600
const pool = connectionPool || newPool()
25032601
const connectionProvider = new RoutingConnectionProvider({
@@ -2513,7 +2611,8 @@ function newRoutingConnectionProviderWithSeedRouter (
25132611
routingTables.forEach(r => {
25142612
connectionProvider._routingTableRegistry.register(r)
25152613
})
2516-
connectionProvider._rediscovery = new FakeRediscovery(routerToRoutingTable)
2614+
connectionProvider._rediscovery =
2615+
fakeRediscovery || new FakeRediscovery(routerToRoutingTable)
25172616
connectionProvider._hostNameResolver = new FakeDnsResolver(seedRouterResolved)
25182617
connectionProvider._useSeedRouter = routingTables.every(
25192618
r => r.expirationTime !== Integer.ZERO
@@ -2630,11 +2729,15 @@ class FakeConnection extends Connection {
26302729
}
26312730

26322731
class FakeRediscovery {
2633-
constructor (routerToRoutingTable) {
2732+
constructor (routerToRoutingTable, error) {
26342733
this._routerToRoutingTable = routerToRoutingTable
2734+
this._error = error
26352735
}
26362736

26372737
lookupRoutingTableOnRouter (ignored, database, router, user) {
2738+
if (this._error) {
2739+
return Promise.reject(this._error)
2740+
}
26382741
const table = this._routerToRoutingTable[database || null]
26392742
if (table) {
26402743
let routingTables = table[router.asKey()]

0 commit comments

Comments
 (0)