From cd275266ed9f9da6b698d3ef2b8238fe1fb2c8a1 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Sun, 14 Jan 2024 16:12:49 +0100 Subject: [PATCH 1/4] test: experimental --- package.json | 2 +- test/node-test/debug.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 43d11d25389..d8115a85897 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "test:fetch": "npm run build:node && borp --expose-gc --coverage -p \"test/fetch/*.js\" && borp --coverage -p \"test/webidl/*.js\"", "test:jest": "jest", "test:tap": "tap test/*.js", - "test:node-test": "borp --coverage -p \"test/node-test/**/*.js\"", + "test:node-test": "borp --coverage -p \"test/node-test/debug.js\"", "test:tdd": "tap test/*.js --coverage -w", "test:tdd:node-test": "borp -p \"test/node-test/**/*.js\" -w", "test:typescript": "tsd && tsc --skipLibCheck test/imports/undici-import.ts", diff --git a/test/node-test/debug.js b/test/node-test/debug.js index caebbaf126d..b1f93835a59 100644 --- a/test/node-test/debug.js +++ b/test/node-test/debug.js @@ -25,9 +25,11 @@ test('debug#websocket', async t => { for await (const chunk of child.stderr) { if (chunk.includes('[UNDICI-WS] Warning')) { + // Ignoring experimental warning continue } + console.log(chunk) assert.match( chunk, /(WEBSOCKET [0-9]+:) (connecting to|connected to|sending request|connection opened|closed connection)/ From dcff4551e22eb5f2c7f7799acc6c7b089be911e5 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Sun, 14 Jan 2024 16:19:53 +0100 Subject: [PATCH 2/4] test: isolate test --- .github/workflows/nodejs.yml | 3 ++- test/fixtures/websocket.js | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index cf613911706..b3249b946a5 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -18,7 +18,8 @@ jobs: uses: pkgjs/action/.github/workflows/node-test.yaml@v0.1 with: runs-on: ubuntu-latest, windows-latest - test-command: npm run coverage:ci + # test-command: npm run coverage:ci + test-command: npm run test:node-test timeout-minutes: 15 post-test-steps: | - name: Coverage Report 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() + }) }) From ca7b88d778ac31522da78b669ab2c0042db177a5 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Sun, 14 Jan 2024 16:24:36 +0100 Subject: [PATCH 3/4] ci: restore previous commands --- .github/workflows/nodejs.yml | 3 +-- package.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index b3249b946a5..cf613911706 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -18,8 +18,7 @@ jobs: uses: pkgjs/action/.github/workflows/node-test.yaml@v0.1 with: runs-on: ubuntu-latest, windows-latest - # test-command: npm run coverage:ci - test-command: npm run test:node-test + test-command: npm run coverage:ci timeout-minutes: 15 post-test-steps: | - name: Coverage Report diff --git a/package.json b/package.json index d8115a85897..43d11d25389 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "test:fetch": "npm run build:node && borp --expose-gc --coverage -p \"test/fetch/*.js\" && borp --coverage -p \"test/webidl/*.js\"", "test:jest": "jest", "test:tap": "tap test/*.js", - "test:node-test": "borp --coverage -p \"test/node-test/debug.js\"", + "test:node-test": "borp --coverage -p \"test/node-test/**/*.js\"", "test:tdd": "tap test/*.js --coverage -w", "test:tdd:node-test": "borp -p \"test/node-test/**/*.js\" -w", "test:typescript": "tsd && tsc --skipLibCheck test/imports/undici-import.ts", From 3637ad33fabc66657cc0efefbd6750e2fbf21380 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Wed, 17 Jan 2024 10:40:36 +0100 Subject: [PATCH 4/4] test: attempt to fix flaky --- test/fixtures/fetch.js | 22 +++++++++-- test/fixtures/undici.js | 19 ++++++++-- test/node-test/debug.js | 82 +++++++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 36 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/node-test/debug.js b/test/node-test/debug.js index b1f93835a59..53eec7aa774 100644 --- a/test/node-test/debug.js +++ b/test/node-test/debug.js @@ -16,52 +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')) { - // Ignoring experimental 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]) } + }) - console.log(chunk) - 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 => { @@ -76,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 })