Skip to content

Commit 718b844

Browse files
committed
Validate connection liveness check timeout
1 parent 54d5d9c commit 718b844

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

packages/core/src/driver.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ interface DriverConfig {
107107
fetchSize?: number
108108
logging?: LoggingConfig
109109
notificationFilter?: NotificationFilter
110+
connectionLivenessCheckTimeout?: number
110111
}
111112

112113
/**
@@ -907,6 +908,9 @@ function sanitizeConfig (config: any): void {
907908
DEFAULT_FETCH_SIZE
908909
)
909910
config.connectionTimeout = extractConnectionTimeout(config)
911+
912+
config.connectionLivenessCheckTimeout =
913+
validateConnectionLivenessCheckTimeoutSizeValue(config.connectionLivenessCheckTimeout)
910914
}
911915

912916
/**
@@ -962,6 +966,24 @@ function extractConnectionTimeout (config: any): number | null {
962966
}
963967
}
964968

969+
/**
970+
* @private
971+
*/
972+
function validateConnectionLivenessCheckTimeoutSizeValue (
973+
rawValue: any
974+
): number | undefined {
975+
if (rawValue == null) {
976+
return undefined
977+
}
978+
const connectionLivenessCheckTimeout = parseInt(rawValue, 10)
979+
if (connectionLivenessCheckTimeout < 0 || Number.isNaN(connectionLivenessCheckTimeout)) {
980+
throw new Error(
981+
`The connectionLivenessCheckTimeout can only be a positive value or 0 for always. However connectionLivenessCheckTimeout = ${connectionLivenessCheckTimeout}`
982+
)
983+
}
984+
return connectionLivenessCheckTimeout
985+
}
986+
965987
/**
966988
* @private
967989
* @returns {ConfiguredCustomResolver} new custom resolver that wraps the passed-in resolver function.

packages/core/test/driver.test.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
/* eslint-disable @typescript-eslint/promise-function-async */
18-
import { bookmarkManager, ConnectionProvider, EagerResult, newError, NotificationFilter, Result, ResultSummary, ServerInfo, Session, TransactionConfig } from '../src'
18+
import { bookmarkManager, ConnectionProvider, EagerResult, int, newError, NotificationFilter, Result, ResultSummary, ServerInfo, Session, TransactionConfig } from '../src'
1919
import Driver, { QueryConfig, READ, routing } from '../src/driver'
2020
import { Bookmarks } from '../src/internal/bookmarks'
2121
import { Logger } from '../src/internal/logger'
@@ -603,6 +603,64 @@ describe('Driver', () => {
603603
await driver.close()
604604
})
605605
})
606+
607+
describe('config', () => {
608+
describe('connectionLivenessCheckTimeout', () => {
609+
it.each([
610+
[undefined, undefined],
611+
[null, undefined],
612+
[0, 0],
613+
[BigInt(0), 0],
614+
[int(0), 0],
615+
['0', 0],
616+
[3000, 3000],
617+
[BigInt(3000), 3000],
618+
[int(3000), 3000],
619+
['3000', 3000]
620+
])('should sanitize value % to %s', async (configured, expected) => {
621+
const createConnectionProviderMock = jest.fn(mockCreateConnectonProvider(connectionProvider))
622+
const driver = new Driver(
623+
META_INFO,
624+
// @ts-expect-error
625+
{ connectionLivenessCheckTimeout: configured },
626+
createConnectionProviderMock,
627+
createSession
628+
)
629+
630+
driver._getOrCreateConnectionProvider()
631+
632+
expect(createConnectionProviderMock).toHaveBeenCalledWith(
633+
expect.any(Number),
634+
expect.objectContaining({ connectionLivenessCheckTimeout: expected }),
635+
expect.any(Object),
636+
expect.any(Object)
637+
)
638+
639+
await driver.close()
640+
})
641+
642+
it.each([
643+
[-1, -1],
644+
[int(-1), -1],
645+
[BigInt(-1), -1],
646+
['-1', -1],
647+
[{}, NaN],
648+
['a', NaN]
649+
])('should fail in invalid values', async (configured, expected) => {
650+
const createConnectionProviderMock = jest.fn(mockCreateConnectonProvider(connectionProvider))
651+
652+
expect(() => new Driver(
653+
META_INFO,
654+
// @ts-expect-error
655+
{ connectionLivenessCheckTimeout: configured },
656+
createConnectionProviderMock,
657+
createSession
658+
)).toThrow(new Error(`The connectionLivenessCheckTimeout can only be a positive value or 0 for always. However connectionLivenessCheckTimeout = ${expected}`))
659+
660+
expect(createConnectionProviderMock).not.toHaveBeenCalled()
661+
})
662+
})
663+
})
606664
})
607665

608666
function mockCreateConnectonProvider (connectionProvider: ConnectionProvider) {

packages/neo4j-driver-deno/lib/core/driver.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ interface DriverConfig {
106106
trust?: TrustStrategy
107107
fetchSize?: number
108108
logging?: LoggingConfig
109-
notificationFilter?: NotificationFilter
109+
notificationFilter?: NotificationFilter,
110+
connectionLivenessCheckTimeout?: number
110111
}
111112

112113
/**
@@ -907,6 +908,9 @@ function sanitizeConfig (config: any): void {
907908
DEFAULT_FETCH_SIZE
908909
)
909910
config.connectionTimeout = extractConnectionTimeout(config)
911+
912+
config.connectionLivenessCheckTimeout =
913+
validateConnectionLivenessCheckTimeoutSizeValue(config.connectionLivenessCheckTimeout)
910914
}
911915

912916
/**
@@ -962,6 +966,24 @@ function extractConnectionTimeout (config: any): number | null {
962966
}
963967
}
964968

969+
/**
970+
* @private
971+
*/
972+
function validateConnectionLivenessCheckTimeoutSizeValue (
973+
rawValue: any
974+
): number | undefined {
975+
if (rawValue == null) {
976+
return undefined
977+
}
978+
const connectionLivenessCheckTimeout = parseInt(rawValue, 10)
979+
if (connectionLivenessCheckTimeout < 0 || Number.isNaN(connectionLivenessCheckTimeout)) {
980+
throw new Error(
981+
`The connectionLivenessCheckTimeout can only be a positive value or 0 for always. However connectionLivenessCheckTimeout = ${connectionLivenessCheckTimeout}`
982+
)
983+
}
984+
return connectionLivenessCheckTimeout
985+
}
986+
965987
/**
966988
* @private
967989
* @returns {ConfiguredCustomResolver} new custom resolver that wraps the passed-in resolver function.

0 commit comments

Comments
 (0)