|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('node:test') |
| 4 | +const assert = require('node:assert') |
| 5 | +const { createServer } = require('node:http') |
| 6 | +const { once } = require('node:events') |
| 7 | +const { Agent, Pool, fetch } = require('../..') |
| 8 | + |
| 9 | +// Regression for #5600 / #5606: |
| 10 | +// Reusing an idle keep-alive socket must not stall behind the poll phase. |
| 11 | +// Progress is asserted via logical I/O events (server 'connection' / 'request'), |
| 12 | +// not wall-clock thresholds. If idle-socket validation is deferred with an |
| 13 | +// unref'd setImmediate, the poll phase blocks on the re-ref'd socket and this |
| 14 | +// test hits the timeout instead of completing. |
| 15 | +// |
| 16 | +// The buggy setImmediate path stalls ~TICK_MS (~499ms) per reuse when the |
| 17 | +// event loop is idle (woken only by undici's fast-timer tick). Five reuses |
| 18 | +// therefore take well over 1s when regressed, and a few dozen ms when fixed. |
| 19 | + |
| 20 | +const REUSES = 5 |
| 21 | + |
| 22 | +test('reusing an idle keep-alive socket must not stall', { timeout: 1000 }, async (t) => { |
| 23 | + let connections = 0 |
| 24 | + |
| 25 | + const server = createServer((req, res) => { |
| 26 | + res.writeHead(200, { 'content-length': 2 }) |
| 27 | + res.end('ok') |
| 28 | + }) |
| 29 | + |
| 30 | + server.on('connection', () => { |
| 31 | + connections++ |
| 32 | + }) |
| 33 | + |
| 34 | + server.listen(0) |
| 35 | + await once(server, 'listening') |
| 36 | + |
| 37 | + const pool = new Pool(`http://127.0.0.1:${server.address().port}`, { |
| 38 | + connections: 1 |
| 39 | + }) |
| 40 | + |
| 41 | + t.after(async () => { |
| 42 | + await pool.close() |
| 43 | + server.close() |
| 44 | + }) |
| 45 | + |
| 46 | + // Establish the keep-alive connection. |
| 47 | + { |
| 48 | + const res = await pool.request({ path: '/0', method: 'GET' }) |
| 49 | + assert.strictEqual(await res.body.text(), 'ok') |
| 50 | + } |
| 51 | + assert.strictEqual(connections, 1) |
| 52 | + |
| 53 | + // Each reuse is gated on the server observing the request. Between |
| 54 | + // iterations the client socket is idle/unref'd, which is the state that |
| 55 | + // triggers idle-socket validation on the next dispatch. |
| 56 | + for (let i = 1; i <= REUSES; i++) { |
| 57 | + const requested = once(server, 'request') |
| 58 | + const resPromise = pool.request({ path: `/${i}`, method: 'GET' }) |
| 59 | + // Suppress unhandled rejection if the test times out mid-request. |
| 60 | + resPromise.catch(() => {}) |
| 61 | + |
| 62 | + await requested |
| 63 | + |
| 64 | + const res = await resPromise |
| 65 | + assert.strictEqual(await res.body.text(), 'ok') |
| 66 | + assert.strictEqual(connections, 1, 'keep-alive socket must be reused') |
| 67 | + } |
| 68 | +}) |
| 69 | + |
| 70 | +test('fetch reusing an idle keep-alive socket must not stall', { timeout: 1000 }, async (t) => { |
| 71 | + let connections = 0 |
| 72 | + |
| 73 | + const server = createServer((req, res) => { |
| 74 | + res.writeHead(200, { 'content-length': 2 }) |
| 75 | + res.end('ok') |
| 76 | + }) |
| 77 | + |
| 78 | + server.on('connection', () => { |
| 79 | + connections++ |
| 80 | + }) |
| 81 | + |
| 82 | + server.listen(0) |
| 83 | + await once(server, 'listening') |
| 84 | + |
| 85 | + const url = `http://127.0.0.1:${server.address().port}` |
| 86 | + const agent = new Agent({ |
| 87 | + connections: 1, |
| 88 | + pipelining: 1, |
| 89 | + keepAliveTimeout: 60_000 |
| 90 | + }) |
| 91 | + |
| 92 | + t.after(async () => { |
| 93 | + await agent.close() |
| 94 | + server.close() |
| 95 | + }) |
| 96 | + |
| 97 | + { |
| 98 | + const res = await fetch(`${url}/0`, { dispatcher: agent }) |
| 99 | + assert.strictEqual(await res.text(), 'ok') |
| 100 | + } |
| 101 | + assert.strictEqual(connections, 1) |
| 102 | + |
| 103 | + for (let i = 1; i <= REUSES; i++) { |
| 104 | + const requested = once(server, 'request') |
| 105 | + const resPromise = fetch(`${url}/${i}`, { dispatcher: agent }) |
| 106 | + resPromise.catch(() => {}) |
| 107 | + |
| 108 | + await requested |
| 109 | + |
| 110 | + const res = await resPromise |
| 111 | + assert.strictEqual(await res.text(), 'ok') |
| 112 | + assert.strictEqual(connections, 1, 'keep-alive socket must be reused') |
| 113 | + } |
| 114 | +}) |
0 commit comments