From 9fb1f8a4a21faeb13cfe22b2741d3657247b5b11 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Wed, 17 Jan 2024 11:01:13 +0100 Subject: [PATCH] test: fix flaky debug test (#2613) * test: experimental * test: isolate test * ci: restore previous commands * test: attempt to fix flaky --- test/fixtures/fetch.js | 22 +++++++++-- test/fixtures/undici.js | 19 +++++++-- test/fixtures/websocket.js | 12 +++--- test/node-test/debug.js | 80 +++++++++++++++++++++++++------------- 4 files changed, 93 insertions(+), 40 deletions(-) diff --git a/test/fixtures/fetch.js b/test/fixtures/fetch.js index ef2ea7b6e98..ae4934a4e58 100644 --- a/test/fixtures/fetch.js +++ b/test/fixtures/fetch.js @@ -1,6 +1,20 @@ +const { createServer } = require('http') const { fetch } = require('../..') -fetch('https://nodejs.org').then( - res => res.body.cancel(), - () => {} -) +const server = createServer((_req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }) + res.end('hello world') +}) + +server.listen(0, () => { + const { port, address, family } = server.address() + const hostname = family === 'IPv6' ? `[${address}]` : address + fetch(`http://${hostname}:${port}`) + .then( + res => res.body.cancel(), + () => {} + ) + .then(() => { + server.close() + }) +}) diff --git a/test/fixtures/undici.js b/test/fixtures/undici.js index d322fcf7846..8c6d5aac1d1 100644 --- a/test/fixtures/undici.js +++ b/test/fixtures/undici.js @@ -1,6 +1,17 @@ +const { createServer } = require('http') const { request } = require('../..') -request('https://nodejs.org', { maxRedirections: 0 }).then( - res => res.body.dump(), - () => {} -) +const server = createServer((_req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }) + res.end('hello world') +}) + +server.listen(0, () => { + const { port, address, family } = server.address() + const hostname = family === 'IPv6' ? `[${address}]` : address + request(`http://${hostname}:${port}`) + .then(res => res.body.dump()) + .then(() => { + server.close() + }) +}) diff --git a/test/fixtures/websocket.js b/test/fixtures/websocket.js index ca8c83513fc..67d283aa96e 100644 --- a/test/fixtures/websocket.js +++ b/test/fixtures/websocket.js @@ -6,11 +6,11 @@ const server = new WebSocketServer({ port: 0 }) server.on('connection', ws => { ws.close(1000, 'goodbye') }) +server.on('listening', () => { + const { port } = server.address() + const ws = new WebSocket(`ws://localhost:${port}`, 'chat') -const { port } = server.address() - -const ws = new WebSocket(`ws://localhost:${port}`, 'chat') - -ws.addEventListener('close', () => { - server.close() + ws.addEventListener('close', () => { + server.close() + }) }) diff --git a/test/node-test/debug.js b/test/node-test/debug.js index caebbaf126d..53eec7aa774 100644 --- a/test/node-test/debug.js +++ b/test/node-test/debug.js @@ -16,50 +16,67 @@ test('debug#websocket', async t => { } } ) + const chunks = [] + const assertions = [ + /(WEBSOCKET [0-9]+:) (connecting to)/, + // Skip the chunk that comes with the experimental warning + /(\[UNDICI-WS\])/, + /(WEBSOCKET [0-9]+:) (connected to)/, + /(WEBSOCKET [0-9]+:) (sending request)/, + /(WEBSOCKET [0-9]+:) (connection opened)/, + /(WEBSOCKET [0-9]+:) (closed connection to)/ + ] t.after(() => { child.kill() }) child.stderr.setEncoding('utf8') - - for await (const chunk of child.stderr) { - if (chunk.includes('[UNDICI-WS] Warning')) { - continue + child.stderr.on('data', chunk => { + chunks.push(chunk) + }) + child.stderr.on('end', () => { + for (let i = 1; i < chunks.length; i++) { + assert.match(chunks[i], assertions[i]) } + }) - assert.match( - chunk, - /(WEBSOCKET [0-9]+:) (connecting to|connected to|sending request|connection opened|closed connection)/ - ) - } + await assert.completed }) test('debug#fetch', async t => { - // Due to Node.js webpage redirect - const assert = tspl(t, { plan: 10 }) + const assert = tspl(t, { plan: 5 }) const child = spawn( process.execPath, [join(__dirname, '../fixtures/fetch.js')], { - env: { - NODE_DEBUG: 'fetch' - } + env: Object.assign({}, process.env, { NODE_DEBUG: 'fetch' }) } ) + const chunks = [] + const assertions = [ + /(FETCH [0-9]+:) (connecting to)/, + /(FETCH [0-9]+:) (connected to)/, + /(FETCH [0-9]+:) (sending request)/, + /(FETCH [0-9]+:) (received response)/, + /(FETCH [0-9]+:) (trailers received)/ + ] t.after(() => { child.kill() }) child.stderr.setEncoding('utf8') + child.stderr.on('data', chunk => { + chunks.push(chunk) + }) + child.stderr.on('end', () => { + for (let i = 0; i < chunks.length; i++) { + assert.match(chunks[i], assertions[i]) + } + }) - for await (const chunk of child.stderr) { - assert.match( - chunk, - /(FETCH [0-9]+:) (connecting to|connected to|sending request|received response|trailers received|request to)/ - ) - } + await assert.completed }) test('debug#undici', async t => { @@ -74,17 +91,28 @@ test('debug#undici', async t => { } } ) + const chunks = [] + const assertions = [ + /(UNDICI [0-9]+:) (connecting to)/, + /(UNDICI [0-9]+:) (connected to)/, + /(UNDICI [0-9]+:) (sending request)/, + /(UNDICI [0-9]+:) (received response)/, + /(UNDICI [0-9]+:) (trailers received)/ + ] t.after(() => { child.kill() }) child.stderr.setEncoding('utf8') + child.stderr.on('data', chunk => { + chunks.push(chunk) + }) + child.stderr.on('end', () => { + for (let i = 0; i < chunks.length; i++) { + assert.match(chunks[i], assertions[i]) + } + }) - for await (const chunk of child.stderr) { - assert.match( - chunk, - /(UNDICI [0-9]+:) (connecting to|connected to|sending request|received response|trailers received|request to)/ - ) - } + await assert.completed })