1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
'use strict';
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const http = require('http');
// Regression test for https://github.com/nodejs/node/issues/19859.
// Checks that matching destroys are emitted when creating new/reusing old http
// parser instances.
const N = 50;
const KEEP_ALIVE = 100;
const createdIdsIncomingMessage = [];
const createdIdsClientRequest = [];
const destroyedIdsIncomingMessage = [];
const destroyedIdsClientRequest = [];
async_hooks.createHook({
init: (asyncId, type) => {
if (type === 'HTTPINCOMINGMESSAGE') {
createdIdsIncomingMessage.push(asyncId);
}
if (type === 'HTTPCLIENTREQUEST') {
createdIdsClientRequest.push(asyncId);
}
},
destroy: (asyncId) => {
if (createdIdsIncomingMessage.includes(asyncId)) {
destroyedIdsIncomingMessage.push(asyncId);
}
if (createdIdsClientRequest.includes(asyncId)) {
destroyedIdsClientRequest.push(asyncId);
}
if (destroyedIdsClientRequest.length === N && keepAliveAgent) {
keepAliveAgent.destroy();
keepAliveAgent = undefined;
}
if (destroyedIdsIncomingMessage.length === N && server.listening) {
server.close();
}
}
}).enable();
const server = http.createServer((req, res) => {
req.on('close', common.mustCall(() => {
req.on('readable', common.mustNotCall());
}));
res.end('Hello');
});
let keepAliveAgent = new http.Agent({
keepAlive: true,
keepAliveMsecs: KEEP_ALIVE,
});
server.listen(0, () => {
for (let i = 0; i < N; ++i) {
(function makeRequest() {
http.get({
port: server.address().port,
agent: keepAliveAgent
}, (res) => {
res.resume();
});
})();
}
});
function checkOnExit() {
assert.strictEqual(createdIdsIncomingMessage.length, N);
assert.strictEqual(createdIdsClientRequest.length, N);
assert.strictEqual(destroyedIdsIncomingMessage.length, N);
assert.strictEqual(destroyedIdsClientRequest.length, N);
assert.deepStrictEqual(destroyedIdsIncomingMessage.sort(),
createdIdsIncomingMessage.sort());
assert.deepStrictEqual(destroyedIdsClientRequest.sort(),
createdIdsClientRequest.sort());
}
process.on('SIGTERM', () => {
// Catching SIGTERM and calling `process.exit(1)` so that the `exit` event
// is triggered and the assertions are checked. This can be useful for
// troubleshooting this test if it times out.
process.exit(1);
});
// Ordinary exit.
process.on('exit', checkOnExit);
|