Skip to content

Commit

Permalink
Allow tx timeout to be 0 and send it (#841)
Browse files Browse the repository at this point in the history
Altering TestKit back end to treat exceptions on session.run and tx.run
as `DriverError`s
  • Loading branch information
robsdedude authored Jan 18, 2022
1 parent c45a6b9 commit 10f64b8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/bolt-connection/src/bolt/request-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default class RequestMessage {
if ( databaseContext.databaseName ) {
dbContext.db = databaseContext.databaseName
}

if ( databaseContext.impersonatedUser ) {
dbContext.imp_user = databaseContext.impersonatedUser
}
Expand Down Expand Up @@ -286,7 +286,7 @@ function buildTxMetadata (bookmark, txConfig, database, mode, impersonatedUser)
if (!bookmark.isEmpty()) {
metadata.bookmarks = bookmark.values()
}
if (txConfig.timeout) {
if (txConfig.timeout !== null) {
metadata.tx_timeout = txConfig.timeout
}
if (txConfig.metadata) {
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/internal/tx-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ function extractTimeout(config: any): Integer | null {
if (util.isObject(config) && (config.timeout || config.timeout === 0)) {
util.assertNumberOrInteger(config.timeout, 'Transaction timeout')
const timeout = int(config.timeout)
if (timeout.isZero()) {
throw newError('Transaction timeout should not be zero')
}
if (timeout.isNegative()) {
throw newError('Transaction timeout should not be negative')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/neo4j-driver/test/bolt-v3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import sharedNeo4j from './internal/shared-neo4j'
const TX_CONFIG_WITH_METADATA = { metadata: { a: 1, b: 2 } }
const TX_CONFIG_WITH_TIMEOUT = { timeout: 42 }

const INVALID_TIMEOUT_VALUES = [0, -1, -42, '15 seconds', [1, 2, 3]]
const INVALID_TIMEOUT_VALUES = [-1, -42, '15 seconds', [1, 2, 3]]
const INVALID_METADATA_VALUES = [
'metadata',
['1', '2', '3'],
Expand Down
4 changes: 3 additions & 1 deletion packages/neo4j-driver/test/internal/tx-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ describe('#unit TxConfig', () => {
})

it('should fail to construct with invalid timeout', () => {
const invalidTimeoutValues = ['15s', [15], {}, 0, int(0), -42, int(-42)]
const invalidTimeoutValues = ['15s', [15], {}, -42, int(-42)]

invalidTimeoutValues.forEach(invalidValue =>
expect(() => new TxConfig({ timeout: invalidValue })).toThrow()
)
})

it('should construct with valid timeout', () => {
testConfigCreationWithTimeout(0)
testConfigCreationWithTimeout(1)
testConfigCreationWithTimeout(42000)

testConfigCreationWithTimeout(int(0))
testConfigCreationWithTimeout(int(1))
testConfigCreationWithTimeout(int(424242))
})
Expand Down
18 changes: 16 additions & 2 deletions packages/testkit-backend/src/request-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ export function SessionRun (context, data, wire) {
Promise.all(observers.map(obs => obs.completitionPromise()))
.catch(_ => null)
.then(_ => {
const result = session.run(cypher, params, { metadata, timeout })
let result
try {
result = session.run(cypher, params, { metadata, timeout })
} catch (e) {
console.log('got some err: ' + JSON.stringify(e))
wire.writeError(e)
return
}
const resultObserver = new ResultObserver({ sessionId, result })
const id = context.addResultObserver(resultObserver)
wire.writeResponse('Result', { id })
Expand Down Expand Up @@ -261,7 +268,14 @@ export function RetryableNegative (context, data, wire) {
export function SessionBeginTransaction (context, data, wire) {
const { sessionId, txMeta: metadata, timeout } = data
const session = context.getSession(sessionId)
const tx = session.beginTransaction({ metadata, timeout })
let tx
try {
tx = session.beginTransaction({ metadata, timeout })
} catch (e) {
console.log('got some err: ' + JSON.stringify(e))
wire.writeError(e)
return
}
const id = context.addTx(tx, sessionId)
wire.writeResponse('Transaction', { id })
}
Expand Down

0 comments on commit 10f64b8

Please sign in to comment.