Skip to content

Commit 30a2687

Browse files
sosukesuzukicrysmags
authored andcommitted
feat: port autoselectfamily.js tests to node:test runner (nodejs#2570)
1 parent 128c850 commit 30a2687

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

test/autoselectfamily.js test/node-test/autoselectfamily.js

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3-
const { test, skip } = require('tap')
3+
const { test, skip } = require('node:test')
44
const dgram = require('dgram')
55
const { Resolver } = require('dns')
66
const dnsPacket = require('dns-packet')
77
const { createServer } = require('http')
8-
const { Client, Agent, request } = require('..')
9-
const { nodeHasAutoSelectFamily } = require('../lib/core/util')
8+
const { Client, Agent, request } = require('../..')
9+
const { nodeHasAutoSelectFamily } = require('../../lib/core/util')
10+
const { tspl } = require('@matteo.collina/tspl')
1011

1112
/*
1213
* IMPORTANT
@@ -67,15 +68,15 @@ function createDnsServer (ipv6Addr, ipv4Addr, cb) {
6768
})
6869
}
6970

70-
test('with autoSelectFamily enable the request succeeds when using request', (t) => {
71-
t.plan(3)
71+
test('with autoSelectFamily enable the request succeeds when using request', async (t) => {
72+
const p = tspl(t, { plan: 3 })
7273

7374
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
7475
const server = createServer((req, res) => {
7576
res.end('hello')
7677
})
7778

78-
t.teardown(() => {
79+
t.after(() => {
7980
server.close()
8081
dnsServer.close()
8182
})
@@ -88,7 +89,7 @@ test('with autoSelectFamily enable the request succeeds when using request', (t)
8889
method: 'GET',
8990
dispatcher: agent
9091
}, (err, { statusCode, body }) => {
91-
t.error(err)
92+
p.ifError(err)
9293

9394
let response = Buffer.alloc(0)
9495

@@ -97,37 +98,39 @@ test('with autoSelectFamily enable the request succeeds when using request', (t)
9798
})
9899

99100
body.on('end', () => {
100-
t.strictSame(statusCode, 200)
101-
t.strictSame(response.toString('utf-8'), 'hello')
101+
p.strictEqual(statusCode, 200)
102+
p.strictEqual(response.toString('utf-8'), 'hello')
102103
})
103104
})
104105
})
105106
})
107+
108+
await p.completed
106109
})
107110

108-
test('with autoSelectFamily enable the request succeeds when using a client', (t) => {
109-
t.plan(3)
111+
test('with autoSelectFamily enable the request succeeds when using a client', async (t) => {
112+
const p = tspl(t, { plan: 3 })
110113

111114
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
112115
const server = createServer((req, res) => {
113116
res.end('hello')
114117
})
115118

116-
t.teardown(() => {
119+
t.after(() => {
117120
server.close()
118121
dnsServer.close()
119122
})
120123

121124
server.listen(0, '127.0.0.1', () => {
122125
const client = new Client(`http://example.org:${server.address().port}`, { connect: { lookup }, autoSelectFamily: true })
123126

124-
t.teardown(client.destroy.bind(client))
127+
t.after(client.destroy.bind(client))
125128

126129
client.request({
127130
path: '/',
128131
method: 'GET'
129132
}, (err, { statusCode, body }) => {
130-
t.error(err)
133+
p.ifError(err)
131134

132135
let response = Buffer.alloc(0)
133136

@@ -136,23 +139,25 @@ test('with autoSelectFamily enable the request succeeds when using a client', (t
136139
})
137140

138141
body.on('end', () => {
139-
t.strictSame(statusCode, 200)
140-
t.strictSame(response.toString('utf-8'), 'hello')
142+
p.strictEqual(statusCode, 200)
143+
p.strictEqual(response.toString('utf-8'), 'hello')
141144
})
142145
})
143146
})
144147
})
148+
149+
await p.completed
145150
})
146151

147-
test('with autoSelectFamily disabled the request fails when using request', (t) => {
148-
t.plan(1)
152+
test('with autoSelectFamily disabled the request fails when using request', async (t) => {
153+
const p = tspl(t, { plan: 1 })
149154

150155
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
151156
const server = createServer((req, res) => {
152157
res.end('hello')
153158
})
154159

155-
t.teardown(() => {
160+
t.after(() => {
156161
server.close()
157162
dnsServer.close()
158163
})
@@ -164,35 +169,39 @@ test('with autoSelectFamily disabled the request fails when using request', (t)
164169
method: 'GET',
165170
dispatcher: agent
166171
}, (err, { statusCode, body }) => {
167-
t.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
172+
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
168173
})
169174
})
170175
})
176+
177+
await p.completed
171178
})
172179

173-
test('with autoSelectFamily disabled the request fails when using a client', (t) => {
174-
t.plan(1)
180+
test('with autoSelectFamily disabled the request fails when using a client', async (t) => {
181+
const p = tspl(t, { plan: 1 })
175182

176183
createDnsServer('::1', '127.0.0.1', function (_, { dnsServer, lookup }) {
177184
const server = createServer((req, res) => {
178185
res.end('hello')
179186
})
180187

181-
t.teardown(() => {
188+
t.after(() => {
182189
server.close()
183190
dnsServer.close()
184191
})
185192

186193
server.listen(0, '127.0.0.1', () => {
187194
const client = new Client(`http://example.org:${server.address().port}`, { connect: { lookup, autoSelectFamily: false } })
188-
t.teardown(client.destroy.bind(client))
195+
t.after(client.destroy.bind(client))
189196

190197
client.request({
191198
path: '/',
192199
method: 'GET'
193200
}, (err, { statusCode, body }) => {
194-
t.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
201+
p.ok(['ECONNREFUSED', 'EAFNOSUPPORT'].includes(err.code))
195202
})
196203
})
197204
})
205+
206+
await p.completed
198207
})

0 commit comments

Comments
 (0)