Skip to content

Commit 0e0b975

Browse files
authored
Fix lack of ProtocolVersion in the Result.summary() (#937)
When there are more than one observer subscribed to the ResultStreamObserver, the first notified observed got the summary with the protocol info because the connection holder stills holding the connection. The next observers notified won't have the protocol version since the connection holder will not have the connection in hands at the moment. This situation is occurring during some tests where result iterator is not fully consumed and the summary is called in the meantime. Checking for existance of the summary in the result and notifying the observer with the already created summary should solve issue since the first summary created will have the protocol version set.
1 parent ba439b3 commit 0e0b975

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

packages/core/src/result.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ class Result implements Promise<QueryResult> {
436436
const onKeysOriginal = observer.onKeys ?? DEFAULT_ON_KEYS
437437

438438
const onCompletedWrapper = (metadata: any): void => {
439-
this._createSummary(metadata).then(summary => {
439+
this._releaseConnectionAndGetSummary(metadata).then(summary => {
440+
if (this._summary !== null) {
441+
return onCompletedOriginal.call(observer, this._summary)
442+
}
440443
this._summary = summary
441444
return onCompletedOriginal.call(observer, summary)
442445
}).catch(onErrorOriginal)
@@ -484,7 +487,7 @@ class Result implements Promise<QueryResult> {
484487
* @param metadata
485488
* @returns
486489
*/
487-
private _createSummary (metadata: any): Promise<ResultSummary> {
490+
private _releaseConnectionAndGetSummary (metadata: any): Promise<ResultSummary> {
488491
const {
489492
validatedQuery: query,
490493
params: parameters

packages/core/test/result.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from '../src'
2727

2828
import Result from '../src/result'
29+
import FakeConnection from './utils/connection.fake'
2930

3031
describe('Result', () => {
3132
const expectedError = newError('some error')
@@ -146,11 +147,14 @@ describe('Result', () => {
146147
{ query: 'query', parameters: { b: 2 } }
147148
]
148149
])('when query=%s and parameters=%s', (query, params, expected) => {
150+
let connectionHolderMock: connectionHolder.ConnectionHolder
149151
beforeEach(() => {
152+
connectionHolderMock = new connectionHolder.ConnectionHolder({})
150153
result = new Result(
151154
Promise.resolve(streamObserverMock),
152155
query,
153-
params
156+
params,
157+
connectionHolderMock
154158
)
155159
})
156160

@@ -174,6 +178,44 @@ describe('Result', () => {
174178
expect(summary).toEqual(expectedSummary)
175179
})
176180

181+
it('should resolve pre-existing summary', async () => {
182+
const metadata = {
183+
resultConsumedAfter: 20,
184+
resultAvailableAfter: 124,
185+
extraInfo: 'extra'
186+
}
187+
const protocolVersion = 5.0
188+
189+
const expectedSummary = new ResultSummary(
190+
expected.query,
191+
expected.parameters,
192+
metadata,
193+
protocolVersion
194+
)
195+
196+
jest.spyOn(connectionHolderMock, 'getConnection')
197+
.mockImplementationOnce(async () => {
198+
const conn = new FakeConnection()
199+
conn.protocolVersion = protocolVersion
200+
return conn
201+
})
202+
203+
jest.spyOn(connectionHolderMock, 'releaseConnection')
204+
.mockImplementationOnce(async () => null)
205+
206+
const iterator = result[Symbol.asyncIterator]()
207+
const next = iterator.next()
208+
const summaryPromise = result.summary()
209+
210+
streamObserverMock.onCompleted(metadata)
211+
212+
const { value } = await next
213+
const summary = await summaryPromise
214+
215+
expect(value).toEqual(expectedSummary)
216+
expect(summary).toEqual(expectedSummary)
217+
})
218+
177219
it('should reject a pre-existing error', async () => {
178220
const expectedError = newError('the expected error')
179221
streamObserverMock.onError(expectedError)

0 commit comments

Comments
 (0)