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

test: Fix DNS lookup in remote-method test #1937

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
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
19 changes: 12 additions & 7 deletions test/unit/collector/remote-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict'

const tap = require('tap')
const dns = require('dns')

const url = require('url')
const Config = require('../../../lib/config')
Expand Down Expand Up @@ -261,6 +262,16 @@ tap.test('when the connection fails', (t) => {
})

t.test('should correctly handle a DNS lookup failure', (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you also want to mock the test above too?

const lookup = dns.lookup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine but could just use sinon too

    const err = Error('no dns')
    err.code = dns.NOTFOUND
    sinon.stub(dns, 'lookup').yields(err)
    t.teardown(() => {
      dns.lookup.restore()
    })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that provide anything we need here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really. we typically use sinon for mocking but older tests will still have patching style like this. I just wanted to call it out, feel free to merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not at all familiar with the sinon API. I like the least amount of mystery in tests as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair. I suggest you familiarize yourself as this repo does use it heavily. but again not a big deal here

dns.lookup = (a, b, cb) => {
const error = Error('no dns')
error.code = dns.NOTFOUND
return cb(error)
}
t.teardown(() => {
dns.lookup = lookup
})

const config = {
max_payload_size_in_bytes: 100000
}
Expand All @@ -271,13 +282,7 @@ tap.test('when the connection fails', (t) => {
const method = new RemoteMethod('TEST', { ...BARE_AGENT, config }, endpoint)
method.invoke([], {}, (error) => {
t.ok(error)

// https://github.com/joyent/node/commit/7295bb9435c
t.match(
error.message,
/^getaddrinfo E(NOENT|NOTFOUND)( failed.domain.cxlrg)?( failed.domain.cxlrg:80)?$/ // eslint-disable-line max-len
)

t.equal(error.message, 'no dns')
t.end()
})
})
Expand Down