-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: fixup do not reuse HTTPParser
Fix some issues introduced/not fixed via #25094: * Init hook is not emitted for a reused HTTPParser * HTTPParser was still used as resource in init hook * type used in init hook was always HTTPINCOMINGMESSAGE even for client requests * some tests have not been adapted to new resource names With this change the async hooks init event is emitted during a call to Initialize() as the type and resource object is available at this time. As a result Initialize() must be called now which could be seen as breaking change even HTTPParser is not part of documented API. It was needed to put the ClientRequest instance into a wrapper object instead passing it directly as async resource otherwise test-domain-multi fails. I think this is because adding an EventEmitter to a Domain adds a property 'domain' and the presence of this changes the context propagation in domains. Besides that tests still refering to resource HTTPParser have been updated/improved. Fixes: #27467 Fixes: #26961 Refs: #25094 PR-URL: #27477 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
Showing
19 changed files
with
139 additions
and
59 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
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
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
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 |
---|---|---|
@@ -1,39 +1,76 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
const { createHook } = require('async_hooks'); | ||
const http = require('http'); | ||
|
||
// Verify that resource emitted for an HTTPParser is not reused. | ||
// Verify that correct create/destroy events are emitted. | ||
|
||
const reused = Symbol('reused'); | ||
|
||
let reusedHTTPParser = false; | ||
const asyncHook = createHook({ | ||
const reusedParser = []; | ||
const incomingMessageParser = []; | ||
const clientRequestParser = []; | ||
const dupDestroys = []; | ||
const destroyed = []; | ||
|
||
createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
switch (type) { | ||
case 'HTTPINCOMINGMESSAGE': | ||
incomingMessageParser.push(asyncId); | ||
break; | ||
case 'HTTPCLIENTREQUEST': | ||
clientRequestParser.push(asyncId); | ||
break; | ||
} | ||
|
||
if (resource[reused]) { | ||
reusedHTTPParser = true; | ||
reusedParser.push( | ||
`resource reused: ${asyncId}, ${triggerAsyncId}, ${type}` | ||
); | ||
} | ||
resource[reused] = true; | ||
}, | ||
destroy(asyncId) { | ||
if (destroyed.includes(asyncId)) { | ||
dupDestroys.push(asyncId); | ||
} else { | ||
destroyed.push(asyncId); | ||
} | ||
} | ||
}); | ||
asyncHook.enable(); | ||
}).enable(); | ||
|
||
const server = http.createServer(function(req, res) { | ||
const server = http.createServer((req, res) => { | ||
res.end(); | ||
}); | ||
|
||
const PORT = 3000; | ||
const url = 'http://127.0.0.1:' + PORT; | ||
const url = `http://127.0.0.1:${PORT}`; | ||
|
||
server.listen(PORT, common.mustCall(() => { | ||
http.get(url, common.mustCall(() => { | ||
server.close(common.mustCall(() => { | ||
server.listen(PORT, common.mustCall(() => { | ||
http.get(url, common.mustCall(() => { | ||
server.close(common.mustCall(() => { | ||
assert.strictEqual(reusedHTTPParser, false); | ||
setTimeout(common.mustCall(verify), 200); | ||
})); | ||
})); | ||
})); | ||
})); | ||
})); | ||
})); | ||
|
||
function verify() { | ||
assert.strictEqual(reusedParser.length, 0); | ||
|
||
assert.strictEqual(incomingMessageParser.length, 2); | ||
assert.strictEqual(clientRequestParser.length, 2); | ||
|
||
assert.strictEqual(dupDestroys.length, 0); | ||
incomingMessageParser.forEach((id) => assert.ok(destroyed.includes(id))); | ||
clientRequestParser.forEach((id) => assert.ok(destroyed.includes(id))); | ||
} |
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
Oops, something went wrong.