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

chore: Renable autoSelectFamily tests. #2180

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- runs-on: ubuntu-latest
node-version: 16.8
exclude: |
- runs-on: windows-latest
node-version: 14
- runs-on: windows-latest
node-version: 16
automerge:
Expand Down
128 changes: 67 additions & 61 deletions test/autoselectfamily.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ const { Resolver } = require('dns')
const dnsPacket = require('dns-packet')
const { createServer } = require('http')
const { Client, Agent, request } = require('..')
const { nodeHasAutoSelectFamily, nodeMajor } = require('../lib/core/util')

if (nodeMajor >= 20) {
skip('some tests are failing')
const { nodeHasAutoSelectFamily } = require('../lib/core/util')

/*
* IMPORTANT
*
* As only some version of Node have autoSelectFamily enabled by default (>= 20), make sure the option is always
* explicitly passed in tests in this file to avoid compatibility problems across release lines.
*
*/

if (!nodeHasAutoSelectFamily) {
skip('autoSelectFamily is not supportee')
process.exit()
}

Expand Down Expand Up @@ -59,84 +67,82 @@ function createDnsServer (ipv6Addr, ipv4Addr, cb) {
})
}

if (nodeHasAutoSelectFamily) {
test('with autoSelectFamily enable the request succeeds when using request', (t) => {
t.plan(3)
test('with autoSelectFamily enable the request succeeds when using request', (t) => {
t.plan(3)

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

t.teardown(() => {
server.close()
dnsServer.close()
})
t.teardown(() => {
server.close()
dnsServer.close()
})

server.listen(0, '127.0.0.1', () => {
const agent = new Agent({ connect: { lookup }, autoSelectFamily: true })
server.listen(0, '127.0.0.1', () => {
const agent = new Agent({ connect: { lookup }, autoSelectFamily: true })

request(
`http://example.org:${server.address().port}/`, {
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
t.error(err)
request(
`http://example.org:${server.address().port}/`, {
method: 'GET',
dispatcher: agent
}, (err, { statusCode, body }) => {
t.error(err)

let response = Buffer.alloc(0)
let response = Buffer.alloc(0)

body.on('data', chunk => {
response = Buffer.concat([response, chunk])
})
body.on('data', chunk => {
response = Buffer.concat([response, chunk])
})

body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
})
})
})
})
})

test('with autoSelectFamily enable the request succeeds when using a client', (t) => {
t.plan(3)
test('with autoSelectFamily enable the request succeeds when using a client', (t) => {
t.plan(3)

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

t.teardown(() => {
server.close()
dnsServer.close()
})
t.teardown(() => {
server.close()
dnsServer.close()
})

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

t.teardown(client.destroy.bind(client))
t.teardown(client.destroy.bind(client))

client.request({
path: '/',
method: 'GET'
}, (err, { statusCode, body }) => {
t.error(err)
client.request({
path: '/',
method: 'GET'
}, (err, { statusCode, body }) => {
t.error(err)

let response = Buffer.alloc(0)
let response = Buffer.alloc(0)

body.on('data', chunk => {
response = Buffer.concat([response, chunk])
})
body.on('data', chunk => {
response = Buffer.concat([response, chunk])
})

body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
body.on('end', () => {
t.strictSame(statusCode, 200)
t.strictSame(response.toString('utf-8'), 'hello')
})
})
})
})
}
})

test('with autoSelectFamily disabled the request fails when using request', (t) => {
t.plan(1)
Expand All @@ -152,7 +158,7 @@ test('with autoSelectFamily disabled the request fails when using request', (t)
})

server.listen(0, '127.0.0.1', () => {
const agent = new Agent({ connect: { lookup } })
const agent = new Agent({ connect: { lookup, autoSelectFamily: false } })

request(`http://example.org:${server.address().port}`, {
method: 'GET',
Expand All @@ -178,7 +184,7 @@ test('with autoSelectFamily disabled the request fails when using a client', (t)
})

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

client.request({
Expand Down