Skip to content

Commit f00ca69

Browse files
committed
Clear receive timeout when close browser and deno channels
The receive timeout was not being clearer when channel get closed. With the timeout running, receive timeouts events can still be notified to the existing observers. This was causing some tests failing and possibile issues in production code.
1 parent ee83b20 commit f00ca69

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export default class WebSocketChannel {
168168
return new Promise((resolve, reject) => {
169169
if (this._ws && this._ws.readyState !== WS_CLOSED) {
170170
this._open = false
171+
this.stopReceiveTimeout()
171172
this._clearConnectionTimeout()
172173
this._ws.onclose = () => resolve()
173174
this._ws.close()
@@ -206,7 +207,7 @@ export default class WebSocketChannel {
206207
* Start the receive timeout for the channel.
207208
*/
208209
startReceiveTimeout () {
209-
if (this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
210+
if (this._open && this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
210211
this._receiveTimeoutStarted = true
211212
this._resetTimeout()
212213
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export default class DenoChannel {
150150
async close () {
151151
if (this._open) {
152152
this._open = false
153+
this.stopReceiveTimeout()
153154
if (this._conn != null) {
154155
await this._conn.close()
155156
}
@@ -185,7 +186,7 @@ export default class DenoChannel {
185186
* Start the receive timeout for the channel.
186187
*/
187188
startReceiveTimeout () {
188-
if (this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
189+
if (this._open && this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
189190
this._receiveTimeoutStarted = true
190191
this._resetTimeout()
191192
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,31 @@ describe('WebSocketChannel', () => {
412412
expect(fakeSetTimeout.clearedTimeouts).toEqual([])
413413
})
414414

415+
it('should be cleared when connection closes', async () => {
416+
webSocketChannel.startReceiveTimeout()
417+
418+
expect(fakeSetTimeout._timeoutIdCounter).toEqual(1)
419+
expect(fakeSetTimeout.calls.length).toEqual(1)
420+
expect(fakeSetTimeout.calls[0][1]).toEqual(receiveTimeout)
421+
expect(fakeSetTimeout.clearedTimeouts).toEqual([])
422+
423+
await webSocketChannel.close()
424+
425+
expect(fakeSetTimeout._timeoutIdCounter).toEqual(1)
426+
expect(fakeSetTimeout.calls.length).toEqual(1)
427+
expect(fakeSetTimeout.clearedTimeouts).toEqual([0])
428+
})
429+
430+
it('should call not setTimeout(receiveTimeout) when connection is closed', async () => {
431+
await webSocketChannel.close()
432+
433+
webSocketChannel.startReceiveTimeout()
434+
435+
expect(fakeSetTimeout._timeoutIdCounter).toEqual(0)
436+
expect(fakeSetTimeout.calls.length).toEqual(0)
437+
expect(fakeSetTimeout.clearedTimeouts).toEqual([])
438+
})
439+
415440
it('should call setTimeout(receiveTimeout) after stopped', () => {
416441
webSocketChannel.startReceiveTimeout()
417442

packages/neo4j-driver-deno/lib/bolt-connection/channel/browser/browser-channel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export default class WebSocketChannel {
168168
return new Promise((resolve, reject) => {
169169
if (this._ws && this._ws.readyState !== WS_CLOSED) {
170170
this._open = false
171+
this.stopReceiveTimeout()
171172
this._clearConnectionTimeout()
172173
this._ws.onclose = () => resolve()
173174
this._ws.close()
@@ -206,7 +207,7 @@ export default class WebSocketChannel {
206207
* Start the receive timeout for the channel.
207208
*/
208209
startReceiveTimeout () {
209-
if (this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
210+
if (this._open && this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
210211
this._receiveTimeoutStarted = true
211212
this._resetTimeout()
212213
}

packages/neo4j-driver-deno/lib/bolt-connection/channel/deno/deno-channel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export default class DenoChannel {
150150
async close () {
151151
if (this._open) {
152152
this._open = false
153+
this.stopReceiveTimeout()
153154
if (this._conn != null) {
154155
await this._conn.close()
155156
}
@@ -185,7 +186,7 @@ export default class DenoChannel {
185186
* Start the receive timeout for the channel.
186187
*/
187188
startReceiveTimeout () {
188-
if (this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
189+
if (this._open && this._receiveTimeout !== null && !this._receiveTimeoutStarted) {
189190
this._receiveTimeoutStarted = true
190191
this._resetTimeout()
191192
}

0 commit comments

Comments
 (0)