From d1d868624d57bcca8fd967fa68ca606c1031a7c2 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Wed, 13 Nov 2024 14:22:22 +0100 Subject: [PATCH] run in separate thread maybe fixes it --- test/fetch/long-lived-abort-controller.js | 39 ++++++++++++++++------- test/issue-3410.js | 4 +-- test/utils/hello-world-server-delayed.js | 30 +++++++++++++++++ test/utils/hello-world-server.js | 14 +------- 4 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 test/utils/hello-world-server-delayed.js diff --git a/test/fetch/long-lived-abort-controller.js b/test/fetch/long-lived-abort-controller.js index 7935135426b..daf6ca7ede5 100644 --- a/test/fetch/long-lived-abort-controller.js +++ b/test/fetch/long-lived-abort-controller.js @@ -1,24 +1,39 @@ 'use strict' -const http = require('node:http') -const { fetch } = require('../../') -const { once } = require('events') +const { fork } = require('node:child_process') +const { resolve: pathResolve } = require('node:path') const { test } = require('node:test') -const { closeServerAsPromise } = require('../utils/node-http') +const { fetch } = require('../../') const { strictEqual, fail } = require('node:assert') const isNode18 = process.version.startsWith('v18') test('long-lived-abort-controller', { skip: isNode18 }, async (t) => { - const server = http.createServer((req, res) => { - res.writeHead(200, { 'Content-Type': 'text/plain' }) - res.write('Hello World!') - res.end() - }).listen(0) + // 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 } + ) - await once(server, 'listening') + childProcess.on('message', (address) => { + resolve({ + serverProcess: childProcess, + address + }) + }) + childProcess.on('error', err => { + reject(err) + }) + }) - t.after(closeServerAsPromise(server)) + t.after(() => { + serverProcess.kill('SIGKILL') + }) let emittedWarning = null function onWarning (value) { @@ -36,7 +51,7 @@ test('long-lived-abort-controller', { skip: isNode18 }, async (t) => { // Unfortunately we are relying on GC and implementation details here. for (let i = 0; i < 2000; i++) { // make request - const res = await fetch(`http://localhost:${server.address().port}`, { + const res = await fetch(address, { signal: controller.signal }) diff --git a/test/issue-3410.js b/test/issue-3410.js index d5bb8093ef2..4e80332d25a 100644 --- a/test/issue-3410.js +++ b/test/issue-3410.js @@ -17,7 +17,7 @@ describe('https://github.com/nodejs/undici/issues/3410', () => { address } = await new Promise((resolve, reject) => { const childProcess = fork( - pathResolve(__dirname, './utils/hello-world-server.js'), + pathResolve(__dirname, './utils/hello-world-server-delayed.js'), [], { windowsHide: true } ) @@ -56,7 +56,7 @@ describe('https://github.com/nodejs/undici/issues/3410', () => { address } = await new Promise((resolve, reject) => { const childProcess = fork( - pathResolve(__dirname, './utils/hello-world-server.js'), + pathResolve(__dirname, './utils/hello-world-server-delayed.js'), [], { windowsHide: true } ) diff --git a/test/utils/hello-world-server-delayed.js b/test/utils/hello-world-server-delayed.js new file mode 100644 index 00000000000..520ed3a734d --- /dev/null +++ b/test/utils/hello-world-server-delayed.js @@ -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}/`) + } +}) diff --git a/test/utils/hello-world-server.js b/test/utils/hello-world-server.js index 520ed3a734d..df479f2fdd7 100644 --- a/test/utils/hello-world-server.js +++ b/test/utils/hello-world-server.js @@ -6,21 +6,9 @@ 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() + res.end('Hello World') }) -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}/`)