Skip to content

Fix RxSession in Testkit Backend #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions packages/core/src/internal/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import { stringify } from '../json'

const ENCRYPTION_ON: EncryptionLevel = 'ENCRYPTION_ON'
const ENCRYPTION_OFF: EncryptionLevel = 'ENCRYPTION_OFF'

// eslint-disable-next-line @typescript-eslint/naming-convention
const __isBrokenObject__ = '__isBrokenObject__'
// eslint-disable-next-line @typescript-eslint/naming-convention
const __reason__ = '__reason__'
/**
* Verifies if the object is null or empty
* @param obj The subject object
Expand Down Expand Up @@ -237,7 +240,16 @@ function createBrokenObject<T extends object> (error: Error, object: any = {}):
}

return new Proxy(object, {
get: fail,
get: (_: T, p: string | Symbol): any => {
if (p === __isBrokenObject__) {
return true
} else if (p === __reason__) {
return error
} else if (p === 'toJSON') {
return undefined
}
fail()
},
set: fail,
apply: fail,
construct: fail,
Expand All @@ -253,6 +265,27 @@ function createBrokenObject<T extends object> (error: Error, object: any = {}):
})
}

/**
* Verifies if it is a Broken Object
* @param {any} object The object
* @returns {boolean} If it was created with createBrokenObject
*/
function isBrokenObject (object: any): boolean {
return object !== null && typeof object === 'object' && object[__isBrokenObject__] === true
}

/**
* Returns if the reason the object is broken.
*
* This method should only be called with instances create with {@link createBrokenObject}
*
* @param {any} object The object
* @returns {Error} The reason the object is broken
*/
function getBrokenObjectReason (object: any): Error {
return object[__reason__]
}

export {
isEmptyObjectOrNull,
isObject,
Expand All @@ -265,5 +298,7 @@ export {
validateQueryAndParameters,
ENCRYPTION_ON,
ENCRYPTION_OFF,
createBrokenObject
createBrokenObject,
isBrokenObject,
getBrokenObjectReason
}
17 changes: 14 additions & 3 deletions packages/core/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@
* limitations under the License.
*/

import { isBrokenObject, getBrokenObjectReason } from './internal/util'

/**
* Custom version on JSON.stringify that can handle values that normally don't support serialization, such as BigInt.
* @private
* @param val A JavaScript value, usually an object or array, to be converted.
* @returns A JSON string representing the given value.
*/
export function stringify (val: any): string {
return JSON.stringify(val, (_, value) =>
typeof value === 'bigint' ? `${value}n` : value
)
return JSON.stringify(val, (_, value) => {
if (isBrokenObject(value)) {
return {
__isBrokenObject__: true,
__reason__: getBrokenObjectReason(value)
}
}
if (typeof value === 'bigint') {
return `${value}n`
}
return value
})
}
7 changes: 7 additions & 0 deletions packages/core/test/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`json .stringify should handle objects created with createBrokenObject 1`] = `"{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\",\\"retriable\\":false}}"`;

exports[`json .stringify should handle objects created with createBrokenObject in list 1`] = `"[{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\",\\"retriable\\":false}}]"`;

exports[`json .stringify should handle objects created with createBrokenObject inside other object 1`] = `"{\\"number\\":1,\\"broken\\":{\\"__isBrokenObject__\\":true,\\"__reason__\\":{\\"code\\":\\"N/A\\",\\"name\\":\\"Neo4jError\\",\\"retriable\\":false}}}"`;
49 changes: 48 additions & 1 deletion packages/core/test/internal/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { newError } from '../../src'
import Integer, { int } from '../../src/integer'
import {
isEmptyObjectOrNull,
Expand All @@ -28,7 +29,10 @@ import {
assertValidDate,
validateQueryAndParameters,
ENCRYPTION_ON,
ENCRYPTION_OFF
ENCRYPTION_OFF,
createBrokenObject,
isBrokenObject,
getBrokenObjectReason
} from '../../src/internal/util'

/* eslint-disable no-new-wrappers */
Expand Down Expand Up @@ -250,4 +254,47 @@ describe('Util', () => {
expect(ENCRYPTION_ON).toBe('ENCRYPTION_ON'))
test('should ENCRYPTION_OFF toBe "ENCRYPTION_OFF"', () =>
expect(ENCRYPTION_OFF).toBe('ENCRYPTION_OFF'))

describe('isBrokenObject', () => {
it('should return true when object created with createBrokenObject', () => {
const object = createBrokenObject(newError('error'), {})

expect(isBrokenObject(object)).toBe(true)
})

it('should return false for regular objects', () => {
const object = {}

expect(isBrokenObject(object)).toBe(false)
})

it('should return false for non-objects', () => {
expect(isBrokenObject(null)).toBe(false)
expect(isBrokenObject(undefined)).toBe(false)
expect(isBrokenObject(1)).toBe(false)
expect(isBrokenObject(() => {})).toBe(false)
expect(isBrokenObject('string')).toBe(false)
})
})

describe('getBrokenObjectReason', () => {
it('should return the reason the object is broken', () => {
const reason = newError('error')
const object = createBrokenObject(reason, {})

expect(getBrokenObjectReason(object)).toBe(reason)
})
})

describe('createBrokenObject', () => {
describe('toJSON', () => {
it('should return undefined', () => {
const reason = newError('error')
const object = createBrokenObject(reason, {})

// @ts-expect-error
expect(object.toJSON).toBeUndefined()
})
})
})
})
49 changes: 49 additions & 0 deletions packages/core/test/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { json, newError } from '../src'
import { createBrokenObject } from '../src/internal/util'

describe('json', () => {
describe('.stringify', () => {
it('should handle objects created with createBrokenObject', () => {
const reason = newError('some error')
const broken = createBrokenObject(reason, { })

expect(json.stringify(broken)).toMatchSnapshot()
})

it('should handle objects created with createBrokenObject in list', () => {
const reason = newError('some error')
const broken = createBrokenObject(reason, { })

expect(json.stringify([broken])).toMatchSnapshot()
})

it('should handle objects created with createBrokenObject inside other object', () => {
const reason = newError('some error')
const broken = createBrokenObject(reason, { })

expect(json.stringify({
number: 1,
broken
})).toMatchSnapshot()
})
})
})
30 changes: 28 additions & 2 deletions packages/neo4j-driver/src/result-rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export default class RxResult {
* @constructor
* @protected
* @param {Observable<Result>} result - An observable of single Result instance to relay requests.
* @param {number} state - The streaming state
*/
constructor (result) {
constructor (result, state) {
const replayedResult = result.pipe(publishReplay(1), refCount())

this._result = replayedResult
Expand All @@ -48,7 +49,7 @@ export default class RxResult {
this._records = undefined
this._controls = new StreamControl()
this._summary = new ReplaySubject()
this._state = States.READY
this._state = state || States.READY
}

/**
Expand Down Expand Up @@ -184,6 +185,31 @@ export default class RxResult {
}
}

/**
* Create a {@link Observable} for the current {@link RxResult}
*
*
* @package
* @experimental
* @since 5.0
* @return {Observable<RxResult>}
*/
_toObservable () {
function wrap (result) {
return new Observable(observer => {
observer.next(result)
observer.complete()
})
}
return new Observable(observer => {
this._result.subscribe({
complete: () => observer.complete(),
next: result => observer.next(new RxResult(wrap(result)), this._state),
error: e => observer.error(e)
})
})
}

_setupRecordsStream (result) {
if (this._records) {
return this._records
Expand Down
14 changes: 8 additions & 6 deletions packages/neo4j-driver/src/session-rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ export default class RxSession {

return new Observable(observer => {
try {
observer.next(
new RxTransaction(
this._session._beginTransaction(accessMode, txConfig)
)
)
observer.complete()
this._session._beginTransaction(accessMode, txConfig)
.then(tx => {
observer.next(
new RxTransaction(tx)
)
observer.complete()
})
.catch(err => observer.error(err))
} catch (err) {
observer.error(err)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/neo4j-driver/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ describe('#unit index', () => {

function subject () {
const driver = neo4j.driver('bolt://localhost')
driver._createSession = () => ({
_mode: 'READ',
_beginTransaction: async () => new Transaction({})
})
return driver.rxSession().beginTransaction().toPromise()
}
})
Expand Down Expand Up @@ -202,6 +206,10 @@ describe('#unit index', () => {

async function subject () {
const driver = neo4j.driver('bolt://localhost')
driver._createSession = () => ({
_mode: 'READ',
_beginTransaction: async () => new Transaction({})
})
const tx = await driver.rxSession().beginTransaction().toPromise()
return InternalRxManagedTransaction.fromTransaction(tx)
}
Expand Down
1 change: 0 additions & 1 deletion packages/testkit-backend/src/feature/async.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const features = [
'Feature:API:Result.List',
'Feature:API:Result.Peek',
'Optimization:EagerTransactionBegin',
'Optimization:PullPipelining'
]

Expand Down
2 changes: 1 addition & 1 deletion packages/testkit-backend/src/feature/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const features = [
'Feature:API:ConnectionAcquisitionTimeout',
'Feature:API:Driver:GetServerInfo',
'Feature:API:Driver.VerifyConnectivity',
'Feature:API:Result.Peek',
'Optimization:EagerTransactionBegin',
'Optimization:ImplicitDefaultArguments',
'Optimization:MinimalResets',
...SUPPORTED_TLS
Expand Down
Loading