Skip to content

Commit

Permalink
improve test for issue #3410 by spawning a custom server
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Aug 23, 2024
1 parent efc5e83 commit d87f441
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
37 changes: 34 additions & 3 deletions test/issue-3410.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { fork } = require('node:child_process')
const { resolve: pathResolve } = require('node:path')
const { test } = require('node:test')
const { eventLoopBlocker } = require('./utils/event-loop-blocker')
const { Agent, fetch, setGlobalDispatcher } = require('..')
const { eventLoopBlocker } = require('./utils/event-loop-blocker')

test('https://github.com/nodejs/undici/issues/3356', async (t) => {
t = tspl(t, { plan: 1 })

// Spawn a server in a new process to avoid effects from the blocking event loop
const {
serverProcess,
address
} = await new Promise((resolve, reject) => {
const childProcess = fork(
pathResolve(__dirname, './utils/hello-world-server.js'),
[],
{ windowsHide: true }
)

childProcess.on('message', (address) => {
resolve({
serverProcess: childProcess,
address
})
})
childProcess.on('error', err => {
reject(err)
})
})

const connectTimeout = 2000
setGlobalDispatcher(new Agent({ connectTimeout }))

const fetchPromise = fetch('http://www.example.org/')
const fetchPromise = fetch(address)

eventLoopBlocker(3000)

await fetchPromise
const response = await fetchPromise

t.equal(await response.text(), 'Hello World')

serverProcess.kill('SIGKILL')
})
30 changes: 30 additions & 0 deletions test/utils/hello-world-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { createServer } = require('node:http')
const hostname = '127.0.0.1'

const server = createServer(async (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')

await sendInDelayedChunks(res, 'Hello World', 125)
res.end()
})

async function sendInDelayedChunks (res, payload, delay) {
const chunks = payload.split('')

for (const chunk of chunks) {
await new Promise(resolve => setTimeout(resolve, delay))

res.write(chunk)
}
}

server.listen(0, hostname, () => {
if (process.send) {
process.send(`http://${hostname}:${server.address().port}/`)
} else {
console.log(`http://${hostname}:${server.address().port}/`)
}
})

0 comments on commit d87f441

Please sign in to comment.