Skip to content

Commit 6cf7c46

Browse files
ilteooodcrysmags
authored andcommitted
feat: port diagnostic-channel tests to node test runner (nodejs#2559)
1 parent a909a0f commit 6cf7c46

File tree

6 files changed

+447
-416
lines changed

6 files changed

+447
-416
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"fuzz": "jsfuzz test/fuzzing/fuzz.js corpus"
9797
},
9898
"devDependencies": {
99+
"@matteo.collina/tspl": "^0.1.0",
99100
"@sinonjs/fake-timers": "^11.1.0",
100101
"@types/node": "^18.0.3",
101102
"abort-controller": "^3.0.0",
Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11
'use strict'
22

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

56
let diagnosticsChannel
67

78
try {
89
diagnosticsChannel = require('diagnostics_channel')
910
} catch {
10-
t.skip('missing diagnostics_channel')
11+
skip('missing diagnostics_channel')
1112
process.exit(0)
1213
}
1314

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

16-
t.plan(16)
17-
18-
const connectError = new Error('custom error')
19-
20-
let _connector
21-
diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => {
22-
_connector = connector
23-
24-
t.equal(typeof _connector, 'function')
25-
t.equal(Object.keys(connectParams).length, 6)
26-
27-
const { host, hostname, protocol, port, servername } = connectParams
28-
29-
t.equal(host, 'localhost:1234')
30-
t.equal(hostname, 'localhost')
31-
t.equal(port, '1234')
32-
t.equal(protocol, 'http:')
33-
t.equal(servername, null)
34-
})
35-
36-
diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
37-
t.equal(Object.keys(connectParams).length, 6)
38-
t.equal(_connector, connector)
39-
40-
const { host, hostname, protocol, port, servername } = connectParams
41-
42-
t.equal(error, connectError)
43-
t.equal(host, 'localhost:1234')
44-
t.equal(hostname, 'localhost')
45-
t.equal(port, '1234')
46-
t.equal(protocol, 'http:')
47-
t.equal(servername, null)
48-
})
49-
50-
const client = new Client('http://localhost:1234', {
51-
connect: (_, cb) => { cb(connectError, null) }
52-
})
53-
54-
t.teardown(client.close.bind(client))
55-
56-
client.request({
57-
path: '/',
58-
method: 'GET'
59-
}, (err, data) => {
60-
t.equal(err, connectError)
17+
test('Diagnostics channel - connect error', (t) => {
18+
const connectError = new Error('custom error')
19+
const assert = tspl(t, { plan: 16 })
20+
21+
let _connector
22+
diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => {
23+
_connector = connector
24+
25+
assert.equal(typeof _connector, 'function')
26+
assert.equal(Object.keys(connectParams).length, 6)
27+
28+
const { host, hostname, protocol, port, servername } = connectParams
29+
30+
assert.equal(host, 'localhost:1234')
31+
assert.equal(hostname, 'localhost')
32+
assert.equal(port, '1234')
33+
assert.equal(protocol, 'http:')
34+
assert.equal(servername, null)
35+
})
36+
37+
diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
38+
assert.equal(Object.keys(connectParams).length, 6)
39+
assert.equal(_connector, connector)
40+
41+
const { host, hostname, protocol, port, servername } = connectParams
42+
43+
assert.equal(error, connectError)
44+
assert.equal(host, 'localhost:1234')
45+
assert.equal(hostname, 'localhost')
46+
assert.equal(port, '1234')
47+
assert.equal(protocol, 'http:')
48+
assert.equal(servername, null)
49+
})
50+
51+
const client = new Client('http://localhost:1234', {
52+
connect: (_, cb) => { cb(connectError, null) }
53+
})
54+
55+
return new Promise((resolve) => {
56+
client.request({
57+
path: '/',
58+
method: 'GET'
59+
}, (err, data) => {
60+
assert.equal(err, connectError)
61+
client.close()
62+
resolve()
63+
})
64+
})
6165
})

test/diagnostics-channel/error.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
'use strict'
22

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

56
let diagnosticsChannel
67

78
try {
89
diagnosticsChannel = require('diagnostics_channel')
910
} catch {
10-
t.skip('missing diagnostics_channel')
11+
skip('missing diagnostics_channel')
1112
process.exit(0)
1213
}
1314

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

17-
t.plan(3)
18+
test('Diagnostics channel - error', (t) => {
19+
const assert = tspl(t, { plan: 3 })
20+
const server = createServer((req, res) => {
21+
res.destroy()
22+
})
23+
after(server.close.bind(server))
1824

19-
const server = createServer((req, res) => {
20-
res.destroy()
21-
})
22-
t.teardown(server.close.bind(server))
25+
const reqHeaders = {
26+
foo: undefined,
27+
bar: 'bar'
28+
}
2329

24-
const reqHeaders = {
25-
foo: undefined,
26-
bar: 'bar'
27-
}
28-
29-
let _req
30-
diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
31-
_req = request
32-
})
33-
34-
diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
35-
t.equal(_req, request)
36-
t.equal(error.code, 'UND_ERR_SOCKET')
37-
})
30+
let _req
31+
diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
32+
_req = request
33+
})
3834

39-
server.listen(0, () => {
40-
const client = new Client(`http://localhost:${server.address().port}`, {
41-
keepAliveTimeout: 300e3
35+
diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
36+
assert.equal(_req, request)
37+
assert.equal(error.code, 'UND_ERR_SOCKET')
4238
})
43-
t.teardown(client.close.bind(client))
44-
45-
client.request({
46-
path: '/',
47-
method: 'GET',
48-
headers: reqHeaders
49-
}, (err, data) => {
50-
t.equal(err.code, 'UND_ERR_SOCKET')
39+
40+
return new Promise((resolve) => {
41+
server.listen(0, () => {
42+
const client = new Client(`http://localhost:${server.address().port}`, {
43+
keepAliveTimeout: 300e3
44+
})
45+
46+
client.request({
47+
path: '/',
48+
method: 'GET',
49+
headers: reqHeaders
50+
}, (err, data) => {
51+
assert.equal(err.code, 'UND_ERR_SOCKET')
52+
client.close()
53+
resolve()
54+
})
55+
})
5156
})
5257
})

0 commit comments

Comments
 (0)