|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const async_id_symbol = process.binding('async_wrap').async_id_symbol; |
| 5 | +const async_hooks = require('async_hooks'); |
| 6 | +const http = require('http'); |
| 7 | + |
| 8 | +// Regression test for https://github.com/nodejs/node/issues/19859 |
| 9 | +// Checks that an http.Agent emits a destroy for the old asyncId before calling |
| 10 | +// asyncReset()s when reusing a socket handle. The setup is nearly identical to |
| 11 | +// parallel/test-async-hooks-http-agent (which focuses on the assertion that |
| 12 | +// a fresh asyncId is assigned to the net.Socket instance). |
| 13 | + |
| 14 | +const destroyedIds = new Set(); |
| 15 | +async_hooks.createHook({ |
| 16 | + destroy: common.mustCallAtLeast((asyncId) => { |
| 17 | + destroyedIds.add(asyncId); |
| 18 | + }, 1) |
| 19 | +}).enable(); |
| 20 | + |
| 21 | +// Make sure a single socket is transparently reused for 2 requests. |
| 22 | +const agent = new http.Agent({ |
| 23 | + keepAlive: true, |
| 24 | + keepAliveMsecs: Infinity, |
| 25 | + maxSockets: 1 |
| 26 | +}); |
| 27 | + |
| 28 | +const server = http.createServer(common.mustCall((req, res) => { |
| 29 | + req.once('data', common.mustCallAtLeast(() => { |
| 30 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 31 | + res.write('foo'); |
| 32 | + })); |
| 33 | + req.on('end', common.mustCall(() => { |
| 34 | + res.end('bar'); |
| 35 | + })); |
| 36 | +}, 2)).listen(0, common.mustCall(() => { |
| 37 | + const port = server.address().port; |
| 38 | + const payload = 'hello world'; |
| 39 | + |
| 40 | + // First request. This is useless except for adding a socket to the |
| 41 | + // agent’s pool for reuse. |
| 42 | + const r1 = http.request({ |
| 43 | + agent, port, method: 'POST' |
| 44 | + }, common.mustCall((res) => { |
| 45 | + // Remember which socket we used. |
| 46 | + const socket = res.socket; |
| 47 | + const asyncIdAtFirstRequest = socket[async_id_symbol]; |
| 48 | + assert.ok(asyncIdAtFirstRequest > 0, `${asyncIdAtFirstRequest} > 0`); |
| 49 | + // Check that request and response share their socket. |
| 50 | + assert.strictEqual(r1.socket, socket); |
| 51 | + |
| 52 | + res.on('data', common.mustCallAtLeast(() => {})); |
| 53 | + res.on('end', common.mustCall(() => { |
| 54 | + // setImmediate() to give the agent time to register the freed socket. |
| 55 | + setImmediate(common.mustCall(() => { |
| 56 | + // The socket is free for reuse now. |
| 57 | + assert.strictEqual(socket[async_id_symbol], -1); |
| 58 | + |
| 59 | + // second request: |
| 60 | + const r2 = http.request({ |
| 61 | + agent, port, method: 'POST' |
| 62 | + }, common.mustCall((res) => { |
| 63 | + assert.ok(destroyedIds.has(asyncIdAtFirstRequest)); |
| 64 | + |
| 65 | + // Empty payload, to hit the “right” code path. |
| 66 | + r2.end(''); |
| 67 | + |
| 68 | + res.on('data', common.mustCallAtLeast(() => {})); |
| 69 | + res.on('end', common.mustCall(() => { |
| 70 | + // Clean up to let the event loop stop. |
| 71 | + server.close(); |
| 72 | + agent.destroy(); |
| 73 | + })); |
| 74 | + })); |
| 75 | + |
| 76 | + // Schedule a payload to be written immediately, but do not end the |
| 77 | + // request just yet. |
| 78 | + r2.write(payload); |
| 79 | + })); |
| 80 | + })); |
| 81 | + })); |
| 82 | + r1.end(payload); |
| 83 | +})); |
0 commit comments