From 046269ca7a701054c0134a9d451ba6821656b273 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Thu, 23 Apr 2020 00:51:09 +0530 Subject: [PATCH] http2: add test for `sessionError` and `aborted`, and docs --- doc/api/http2.md | 6 ++-- lib/internal/http2/core.js | 2 +- test/parallel/test-http2-client-destroy.js | 22 +++++++-------- .../test-http2-emit-on-session-error.js | 28 +++++++++++++++++++ 4 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-http2-emit-on-session-error.js diff --git a/doc/api/http2.md b/doc/api/http2.md index 4969082fce0b23..aebb19c2917b53 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -1715,7 +1715,7 @@ The `'session'` event is emitted when a new `Http2Session` is created by the added: v8.4.0 --> -> Stability: 1 - Experimental. Use [`'aborted'`][]. +> Stability: 0 - Deprecated. Use [`'aborted'`][]. The `'sessionError'` event is emitted when an `'error'` event is emitted by an `Http2Session` object associated with the `Http2Server`. @@ -1878,7 +1878,7 @@ The `'session'` event is emitted when a new `Http2Session` is created by the added: v8.4.0 --> -> Stability: 1 - Experimental. Use [`'aborted'`][]. +> Stability: 0 - Deprecated. Use [`'aborted'`][]. The `'sessionError'` event is emitted when an `'error'` event is emitted by an `Http2Session` object associated with the `Http2SecureServer`. @@ -3583,9 +3583,9 @@ following additional properties: [RFC 8336]: https://tools.ietf.org/html/rfc8336 [RFC 8441]: https://tools.ietf.org/html/rfc8441 [`'checkContinue'`]: #http2_event_checkcontinue +[`'aborted'`]: #http2_event_aborted [`'connect'`]: #http2_event_connect [`'request'`]: #http2_event_request -[`'aborted'`]: #http2_event_aborted [`'unknownProtocol'`]: #http2_event_unknownprotocol [`ClientHttp2Stream`]: #http2_class_clienthttp2stream [`Duplex`]: stream.html#stream_class_stream_duplex diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 37265324fb75c6..89f0bdd0abd585 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2794,8 +2794,8 @@ function sessionOnPriority(stream, parent, weight, exclusive) { function sessionOnError(error) { if (this[kServer] !== undefined) { - this[kServer].emit('sessionError', error, this); this[kServer].emit('aborted', error, this); + this[kServer].emit('sessionError', error, this); } } diff --git a/test/parallel/test-http2-client-destroy.js b/test/parallel/test-http2-client-destroy.js index 730f776306ba3e..12da903c6535a0 100644 --- a/test/parallel/test-http2-client-destroy.js +++ b/test/parallel/test-http2-client-destroy.js @@ -70,27 +70,27 @@ const Countdown = require('../common/countdown'); req.on('response', common.mustNotCall()); - const aborted = { + const sessionError = { name: 'Error', code: 'ERR_HTTP2_INVALID_SESSION', message: 'The session has been destroyed' }; - assert.throws(() => client.setNextStreamID(), aborted); - assert.throws(() => client.ping(), aborted); - assert.throws(() => client.settings({}), aborted); - assert.throws(() => client.goaway(), aborted); - assert.throws(() => client.request(), aborted); + assert.throws(() => client.setNextStreamID(), sessionError); + assert.throws(() => client.ping(), sessionError); + assert.throws(() => client.settings({}), sessionError); + assert.throws(() => client.goaway(), sessionError); + assert.throws(() => client.request(), sessionError); client.close(); // Should be a non-op at this point // Wait for setImmediate call from destroy() to complete // so that state.destroyed is set to true setImmediate(() => { - assert.throws(() => client.setNextStreamID(), aborted); - assert.throws(() => client.ping(), aborted); - assert.throws(() => client.settings({}), aborted); - assert.throws(() => client.goaway(), aborted); - assert.throws(() => client.request(), aborted); + assert.throws(() => client.setNextStreamID(), sessionError); + assert.throws(() => client.ping(), sessionError); + assert.throws(() => client.settings({}), sessionError); + assert.throws(() => client.goaway(), sessionError); + assert.throws(() => client.request(), sessionError); client.close(); // Should be a non-op at this point }); diff --git a/test/parallel/test-http2-emit-on-session-error.js b/test/parallel/test-http2-emit-on-session-error.js new file mode 100644 index 00000000000000..3daa66d0aee975 --- /dev/null +++ b/test/parallel/test-http2-emit-on-session-error.js @@ -0,0 +1,28 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) { common.skip('missing crypto'); } +const { strictEqual } = require('assert'); +const h2 = require('http2'); + +const server = h2.createServer(); +const _error = new Error('some error'); + +// Check if `sessionError` and `aborted` events +// are emitted if `session` emits an `error` event +server.on('aborted', common.mustCall((error, _ins) => { + strictEqual(error, _error); +})); + +server.on('sessionError', common.mustCall((error, _ins) => { + strictEqual(error, _error); +})); + +server.on('session', common.mustCall((session) => { + session.destroy(_error); + server.close(); +})); + +server.listen(0, common.mustCall(() => { + const session = h2.connect(`http://localhost:${server.address().port}`); + session.close(); +}));