Skip to content

Commit

Permalink
chore: Renable autoSelectFamily tests. (#2180)
Browse files Browse the repository at this point in the history
* chore: Renable autoSelectFamily tests.

* chore: Disable CI for Node 14 on Windows.
  • Loading branch information
ShogunPanda authored Jul 7, 2023
1 parent 06f51a6 commit 22bdbd8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 61 deletions.
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

0 comments on commit 22bdbd8

Please sign in to comment.