Repository navigation
Expand file tree
/
Copy pathkeep-alive-reuse.js
More file actions
114 lines (91 loc) · 3.26 KB
/
Copy pathkeep-alive-reuse.js
File metadata and controls
114 lines (91 loc) · 3.26 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { Agent, Pool, fetch } = require('../..')
// Regression for #5600 / #5606:
// Reusing an idle keep-alive socket must not stall behind the poll phase.
// Progress is asserted via logical I/O events (server 'connection' / 'request'),
// not wall-clock thresholds. If idle-socket validation is deferred with an
// unref'd setImmediate, the poll phase blocks on the re-ref'd socket and this
// test hits the timeout instead of completing.
//
// The buggy setImmediate path stalls ~TICK_MS (~499ms) per reuse when the
// event loop is idle (woken only by undici's fast-timer tick). Five reuses
// therefore take well over 1s when regressed, and a few dozen ms when fixed.
const REUSES = 5
test('reusing an idle keep-alive socket must not stall', { timeout: 1000 }, async (t) => {
let connections = 0
const server = createServer((req, res) => {
res.writeHead(200, { 'content-length': 2 })
res.end('ok')
})
server.on('connection', () => {
connections++
})
server.listen(0)
await once(server, 'listening')
const pool = new Pool(`http://127.0.0.1:${server.address().port}`, {
connections: 1
})
t.after(async () => {
await pool.close()
server.close()
})
// Establish the keep-alive connection.
{
const res = await pool.request({ path: '/0', method: 'GET' })
assert.strictEqual(await res.body.text(), 'ok')
}
assert.strictEqual(connections, 1)
// Each reuse is gated on the server observing the request. Between
// iterations the client socket is idle/unref'd, which is the state that
// triggers idle-socket validation on the next dispatch.
for (let i = 1; i <= REUSES; i++) {
const requested = once(server, 'request')
const resPromise = pool.request({ path: `/${i}`, method: 'GET' })
// Suppress unhandled rejection if the test times out mid-request.
resPromise.catch(() => {})
await requested
const res = await resPromise
assert.strictEqual(await res.body.text(), 'ok')
assert.strictEqual(connections, 1, 'keep-alive socket must be reused')
}
})
test('fetch reusing an idle keep-alive socket must not stall', { timeout: 1000 }, async (t) => {
let connections = 0
const server = createServer((req, res) => {
res.writeHead(200, { 'content-length': 2 })
res.end('ok')
})
server.on('connection', () => {
connections++
})
server.listen(0)
await once(server, 'listening')
const url = `http://127.0.0.1:${server.address().port}`
const agent = new Agent({
connections: 1,
pipelining: 1,
keepAliveTimeout: 60_000
})
t.after(async () => {
await agent.close()
server.close()
})
{
const res = await fetch(`${url}/0`, { dispatcher: agent })
assert.strictEqual(await res.text(), 'ok')
}
assert.strictEqual(connections, 1)
for (let i = 1; i <= REUSES; i++) {
const requested = once(server, 'request')
const resPromise = fetch(`${url}/${i}`, { dispatcher: agent })
resPromise.catch(() => {})
await requested
const res = await resPromise
assert.strictEqual(await res.text(), 'ok')
assert.strictEqual(connections, 1, 'keep-alive socket must be reused')
}
})