-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve test for issue #3410 by spawning a custom server
- Loading branch information
Showing
2 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/`) | ||
} | ||
}) |