Skip to content

Commit b13ca42

Browse files
bigmontz2hdddg
authored andcommitted
Implementing commands to allows the retry tests
The `SessionReadTransaction` command is the responsilble for encapsulate the read function pass to the transaction and the other ones use the transaction object got by the SessionReadTransaction.
1 parent 47f30ff commit b13ca42

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

testkit-backend/main.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class Backend {
155155
this._sessions = {}
156156
this._resultObservers = {}
157157
this._errors = {}
158+
this._txs = {}
158159
}
159160

160161
// Called whenever a new line is received.
@@ -190,7 +191,7 @@ class Backend {
190191
_handleRequest (request) {
191192
request = JSON.parse(request)
192193
const { name, data } = request
193-
console.log('Got request ' + name)
194+
console.log('> Got request ' + name, data)
194195
switch (name) {
195196
case 'NewDriver':
196197
{
@@ -294,7 +295,69 @@ class Backend {
294295
})
295296
}
296297
break
297-
298+
case 'SessionReadTransaction':
299+
{
300+
const { sessionId } = data
301+
const session = this._sessions[sessionId]
302+
session
303+
.readTransaction(
304+
tx =>
305+
new Promise((resolve, reject) => {
306+
const txId = this._id++
307+
this._txs[txId] = {
308+
sessionId,
309+
tx,
310+
resolve,
311+
reject,
312+
txId
313+
}
314+
this._writeResponse('RetryableTry', { id: txId })
315+
})
316+
)
317+
.then(_ => this._writeResponse('RetryableDone', null))
318+
.catch(error => this._writeError(error))
319+
}
320+
break
321+
case 'TransactionRun':
322+
{
323+
const { txId, cypher, params } = data
324+
const tx = this._txs[txId]
325+
if (params) {
326+
for (const [key, value] of Object.entries(params)) {
327+
params[key] = cypherToNative(value)
328+
}
329+
}
330+
const result = tx.tx.run(cypher, params)
331+
this._id++
332+
const resultObserver = new ResultObserver()
333+
result.subscribe(resultObserver)
334+
this._resultObservers[this._id] = resultObserver
335+
this._writeResponse('Result', {
336+
id: this._id
337+
})
338+
}
339+
break
340+
case 'RetryablePositive':
341+
{
342+
const { sessionId } = data
343+
const tx = Object.values(this._txs).filter(
344+
({ sessionId: id }) => sessionId === id
345+
)[0]
346+
delete this._txs[tx.txId]
347+
tx.resolve()
348+
}
349+
break
350+
case 'RetryableNegative':
351+
{
352+
const { sessionId, errorId } = data
353+
const tx = Object.values(this._txs).filter(
354+
({ sessionId: id }) => sessionId === id
355+
)[0]
356+
const error = this._errors[errorId] || new Error('Client error')
357+
delete this._txs[tx.txId]
358+
tx.reject(error)
359+
}
360+
break
298361
default:
299362
this._writeBackendError('Unknown request: ' + name)
300363
console.log('Unknown request: ' + name)
@@ -303,6 +366,7 @@ class Backend {
303366
}
304367

305368
_writeResponse (name, data) {
369+
console.log('> writing response', name, data)
306370
let response = {
307371
name: name,
308372
data: data

0 commit comments

Comments
 (0)