Skip to content

Commit

Permalink
feat: port diagnostic-channel tests to node test runner (#2559)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood authored Dec 30, 2023
1 parent d97613f commit e29231d
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 416 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"fuzz": "jsfuzz test/fuzzing/fuzz.js corpus"
},
"devDependencies": {
"@matteo.collina/tspl": "^0.1.0",
"@sinonjs/fake-timers": "^11.1.0",
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
Expand Down
98 changes: 51 additions & 47 deletions test/diagnostics-channel/connect-error.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
'use strict'

const t = require('tap')
const { test, skip } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')

let diagnosticsChannel

try {
diagnosticsChannel = require('diagnostics_channel')
} catch {
t.skip('missing diagnostics_channel')
skip('missing diagnostics_channel')
process.exit(0)
}

const { Client } = require('../..')

t.plan(16)

const connectError = new Error('custom error')

let _connector
diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => {
_connector = connector

t.equal(typeof _connector, 'function')
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

t.equal(host, 'localhost:1234')
t.equal(hostname, 'localhost')
t.equal(port, '1234')
t.equal(protocol, 'http:')
t.equal(servername, null)
})

diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
t.equal(Object.keys(connectParams).length, 6)
t.equal(_connector, connector)

const { host, hostname, protocol, port, servername } = connectParams

t.equal(error, connectError)
t.equal(host, 'localhost:1234')
t.equal(hostname, 'localhost')
t.equal(port, '1234')
t.equal(protocol, 'http:')
t.equal(servername, null)
})

const client = new Client('http://localhost:1234', {
connect: (_, cb) => { cb(connectError, null) }
})

t.teardown(client.close.bind(client))

client.request({
path: '/',
method: 'GET'
}, (err, data) => {
t.equal(err, connectError)
test('Diagnostics channel - connect error', (t) => {
const connectError = new Error('custom error')
const assert = tspl(t, { plan: 16 })

let _connector
diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => {
_connector = connector

assert.equal(typeof _connector, 'function')
assert.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

assert.equal(host, 'localhost:1234')
assert.equal(hostname, 'localhost')
assert.equal(port, '1234')
assert.equal(protocol, 'http:')
assert.equal(servername, null)
})

diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
assert.equal(Object.keys(connectParams).length, 6)
assert.equal(_connector, connector)

const { host, hostname, protocol, port, servername } = connectParams

assert.equal(error, connectError)
assert.equal(host, 'localhost:1234')
assert.equal(hostname, 'localhost')
assert.equal(port, '1234')
assert.equal(protocol, 'http:')
assert.equal(servername, null)
})

const client = new Client('http://localhost:1234', {
connect: (_, cb) => { cb(connectError, null) }
})

return new Promise((resolve) => {
client.request({
path: '/',
method: 'GET'
}, (err, data) => {
assert.equal(err, connectError)
client.close()
resolve()
})
})
})
69 changes: 37 additions & 32 deletions test/diagnostics-channel/error.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
'use strict'

const t = require('tap')
const { test, skip, after } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')

let diagnosticsChannel

try {
diagnosticsChannel = require('diagnostics_channel')
} catch {
t.skip('missing diagnostics_channel')
skip('missing diagnostics_channel')
process.exit(0)
}

const { Client } = require('../..')
const { createServer } = require('http')

t.plan(3)
test('Diagnostics channel - error', (t) => {
const assert = tspl(t, { plan: 3 })
const server = createServer((req, res) => {
res.destroy()
})
after(server.close.bind(server))

const server = createServer((req, res) => {
res.destroy()
})
t.teardown(server.close.bind(server))
const reqHeaders = {
foo: undefined,
bar: 'bar'
}

const reqHeaders = {
foo: undefined,
bar: 'bar'
}

let _req
diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
_req = request
})

diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
t.equal(_req, request)
t.equal(error.code, 'UND_ERR_SOCKET')
})
let _req
diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
_req = request
})

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3
diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
assert.equal(_req, request)
assert.equal(error.code, 'UND_ERR_SOCKET')
})
t.teardown(client.close.bind(client))

client.request({
path: '/',
method: 'GET',
headers: reqHeaders
}, (err, data) => {
t.equal(err.code, 'UND_ERR_SOCKET')

return new Promise((resolve) => {
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3
})

client.request({
path: '/',
method: 'GET',
headers: reqHeaders
}, (err, data) => {
assert.equal(err.code, 'UND_ERR_SOCKET')
client.close()
resolve()
})
})
})
})
Loading

0 comments on commit e29231d

Please sign in to comment.