Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port diagnostic-channel tests to node test runner #2559

Merged
merged 8 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 47 additions & 47 deletions test/diagnostics-channel/connect-error.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
'use strict'

const t = require('tap')
const { test, skip } = require('node:test')
const assert = require('node:assert')

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', () => {
const connectError = new Error('custom error')

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) }
})

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

const t = require('tap')
const { test, skip, after } = require('node:test')
const assert = require('node:assert')

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', () => {
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')

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()
})
})
})