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 2 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
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 streamming 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
35 changes: 23 additions & 12 deletions packages/testkit-backend/src/request-handlers-rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,27 @@ export function SessionRun (context, data, wire) {
}
}

let result
let rxResult
try {
result = session.run(cypher, params, { metadata, timeout })
rxResult = session.run(cypher, params, { metadata, timeout })
} catch (e) {
console.log('got some err: ' + JSON.stringify(e))
wire.writeError(e)
return
}

const it = toAsyncIterator(result)
result[Symbol.asyncIterator] = () => it
rxResult
._toObservable()
.subscribe({
error: e => wire.writeError(e),
next: result => {
result[Symbol.asyncIterator] = () => toAsyncIterator(result)

const id = context.addResult(result)
const id = context.addResult(result)

wire.writeResponse(responses.Result({ id }))
wire.writeResponse(responses.Result({ id }))
}
})
}

export function ResultConsume (context, data, wire) {
Expand Down Expand Up @@ -123,14 +129,19 @@ export function TransactionRun (context, data, wire) {
params[key] = cypherToNative(value)
}
}
const result = tx.tx.run(cypher, params)

const it = toAsyncIterator(result)
result[Symbol.asyncIterator] = () => it
tx.tx.run(cypher, params)
._toObservable()
.subscribe({
error: e => wire.writeError(e),
next: result => {
result[Symbol.asyncIterator] = () => toAsyncIterator(result)

const id = context.addResult(result)
const id = context.addResult(result)

wire.writeResponse(responses.Result({ id }))
wire.writeResponse(responses.Result({ id }))
}
})
}

export function TransactionRollback (context, data, wire) {
Expand Down Expand Up @@ -305,7 +316,7 @@ function toAsyncIterator (result) {
return: async (value) => {
state.finished = true
state.summary = value
return { done: true, value: value }
return { done: true, value }
}
}
}
9 changes: 9 additions & 0 deletions packages/testkit-backend/src/skipped-tests/rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ const skippedTests = [
skip(
'Throws after run insted of the first next because of the backend implementation',
ifEquals('stub.disconnects.test_disconnects.TestDisconnects.test_disconnect_on_tx_begin')
),
skip(
'Backend could not support this test',
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV4x4.test_unknown_zoned_date_time'),
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV4x4.test_unknown_zoned_date_time_patched'),
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV4x4.test_unknown_then_known_zoned_date_time'),
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV4x4.test_unknown_then_known_zoned_date_time_patched'),
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV5x0.test_unknown_then_known_zoned_date_time'),
ifEquals('stub.types.test_temporal_types.TestTemporalTypesV5x0.test_unknown_zoned_date_time')
)
]

Expand Down
4 changes: 4 additions & 0 deletions testkit/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
print("Testkit should test browser")
os.environ["TEST_ENVIRONMENT"] = "REMOTE"

session_type = os.environ.get("TEST_SESSION_TYPE", None)
if session_type is not None:
os.environ["SESSION_TYPE"] = session_type

print("npm run start-testkit-backend")
with open_proccess_in_driver_repo([
"npm", "run", "start-testkit-backend"
Expand Down