Skip to content

Commit a525084

Browse files
committed
Wait for the reset command finishes before notify onComplete
1 parent e8be21b commit a525084

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/bolt-connection/src/connection/connection-channel.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default class ChannelConnection extends Connection {
123123
super(errorHandler)
124124

125125
this._reseting = false
126+
this._resetObservers = []
126127
this._id = idGenerator++
127128
this._address = address
128129
this._server = { address: address.asHostPort() }
@@ -339,18 +340,24 @@ export default class ChannelConnection extends Connection {
339340
}
340341

341342
_reset(observer) {
343+
this._resetObservers.push(observer)
342344
if (this._reseting) {
343-
observer.onComplete()
344345
return
345346
}
346347
this._reseting = true
348+
349+
const notifyFinish = (notify) => {
350+
this._reseting = false
351+
const observers = this._resetObservers
352+
this._resetObservers = []
353+
observers.forEach(notify)
354+
}
355+
347356
this._protocol.reset({
348357
onError: error => {
349-
this._reseting = false
350-
observer.onError(error)
358+
notifyFinish(obs => obs.onError(error))
351359
}, onComplete: () => {
352-
this._reseting = false
353-
observer.onComplete()
360+
notifyFinish(obs => obs.onComplete())
354361
}
355362
})
356363
}

packages/bolt-connection/test/connection/connection-channel.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ describe('ChannelConnection', () => {
282282
connection._resetOnFailure()
283283

284284
expect(protocol.reset).toHaveBeenCalledTimes(1)
285-
expect(protocol.resetFailure).toHaveBeenCalledTimes(1)
285+
expect(protocol.resetFailure).not.toHaveBeenCalled()
286286
})
287287

288288
it('should call protocol.reset() when after a previous reset completed', () => {
@@ -397,20 +397,23 @@ describe('ChannelConnection', () => {
397397
}
398398

399399
const protocol = {
400-
reset: jest.fn(),
400+
reset: jest.fn(observer => {
401+
setTimeout(() => observer.onComplete(), 100)
402+
}),
401403
resetFailure: jest.fn()
402404
}
403405
const protocolSupplier = () => protocol
404406
const connection = spyOnConnectionChannel({ channel, protocolSupplier })
405407

406-
// to not block since the reset will never complete
407-
connection.resetAndFlush()
408+
const completeFirstResetAndFlush = connection.resetAndFlush()
408409

409410
expect(protocol.reset).toHaveBeenCalledTimes(1)
410411

411412
await connection.resetAndFlush()
412413

413414
expect(protocol.reset).toHaveBeenCalledTimes(1)
415+
416+
await completeFirstResetAndFlush
414417
})
415418

416419
it('should call protocol.reset() when after a previous reset completed', async () => {

0 commit comments

Comments
 (0)