-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http2: refinement and test for socketError
Fixes: nodejs/http2#184 Refines the `'socketError'` event a bit and adds a test for the emission of the `'socketError'` event on the server. Client side is tested separately Backport-PR-URL: #14813 Backport-Reviewed-By: Anna Henningsen <[email protected]> Backport-Reviewed-By: Timothy Gu <[email protected]> PR-URL: #14239 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
Showing
3 changed files
with
70 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Flags: --expose-http2 | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http2 = require('http2'); | ||
|
||
const server = http2.createServer(); | ||
server.on('stream', common.mustCall((stream) => { | ||
stream.respond(); | ||
stream.end('ok'); | ||
})); | ||
server.on('session', common.mustCall((session) => { | ||
// First, test that the socketError event is forwarded to the session object | ||
// and not the server object. | ||
const handler = common.mustCall((error, socket) => { | ||
common.expectsError({ | ||
type: Error, | ||
message: 'test' | ||
})(error); | ||
assert.strictEqual(socket, session.socket); | ||
}); | ||
const isNotCalled = common.mustNotCall(); | ||
session.on('socketError', handler); | ||
server.on('socketError', isNotCalled); | ||
session.socket.emit('error', new Error('test')); | ||
session.removeListener('socketError', handler); | ||
server.removeListener('socketError', isNotCalled); | ||
|
||
// Second, test that the socketError is forwarded to the server object when | ||
// no socketError listener is registered for the session | ||
server.on('socketError', common.mustCall((error, socket, session) => { | ||
common.expectsError({ | ||
type: Error, | ||
message: 'test' | ||
})(error); | ||
assert.strictEqual(socket, session.socket); | ||
assert.strictEqual(session, session); | ||
})); | ||
session.socket.emit('error', new Error('test')); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const client = http2.connect(`http://localhost:${server.address().port}`); | ||
const req = client.request(); | ||
req.resume(); | ||
req.on('end', common.mustCall()); | ||
req.on('streamClosed', common.mustCall(() => { | ||
client.destroy(); | ||
server.close(); | ||
})); | ||
})); |