Skip to content

Commit

Permalink
test: fix flaky debug test (#2613)
Browse files Browse the repository at this point in the history
* test: experimental

* test: isolate test

* ci: restore previous commands

* test: attempt to fix flaky
  • Loading branch information
metcoder95 authored Jan 17, 2024
1 parent a7551ce commit 9fb1f8a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 40 deletions.
22 changes: 18 additions & 4 deletions test/fixtures/fetch.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
19 changes: 15 additions & 4 deletions test/fixtures/undici.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
12 changes: 6 additions & 6 deletions test/fixtures/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
80 changes: 54 additions & 26 deletions test/node-test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
})

0 comments on commit 9fb1f8a

Please sign in to comment.