-
Notifications
You must be signed in to change notification settings - Fork 409
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: Converted agent unit tests to node:test #2411
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const selfCert = require('self-cert') | ||
module.exports = selfCert({ | ||
attrs: { | ||
stateName: 'Georgia', | ||
locality: 'Atlanta', | ||
orgName: 'New Relic', | ||
shortName: 'new_relic' | ||
}, | ||
expires: new Date('2099-12-31') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
/** | ||
* Implements https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers | ||
* | ||
* This can be removed once Node.js v22 is the minimum. | ||
* | ||
* @returns {{resolve, reject, promise: Promise<unknown>}} | ||
*/ | ||
module.exports = function promiseResolvers() { | ||
if (typeof Promise.withResolvers === 'function') { | ||
// Node.js >=22 natively supports this. | ||
return Promise.withResolvers() | ||
} | ||
|
||
let resolve | ||
let reject | ||
const promise = new Promise((a, b) => { | ||
resolve = a | ||
reject = b | ||
}) | ||
return { promise, resolve, reject } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
// This provides an in-process http server to use in place of | ||
// collector.newrelic.com. It allows for custom handlers so that test specific | ||
// assertions can be made. | ||
|
||
const https = require('node:https') | ||
const querystring = require('node:querystring') | ||
const helper = require('./agent_helper') | ||
const fakeCert = require('./fake-cert') | ||
|
||
class Collector { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be generic enough to utilize in all tests that need to talk to the remote collector. I haven't actually looked, but I bet we can replace the |
||
#handlers = new Map() | ||
#server | ||
#address | ||
|
||
constructor() { | ||
this.#server = https.createServer({ | ||
key: fakeCert.privateKey, | ||
cert: fakeCert.certificate | ||
}) | ||
this.#server.on('request', (req, res) => { | ||
const qs = querystring.decode(req.url.slice(req.url.indexOf('?') + 1)) | ||
const handler = this.#handlers.get(qs.method) | ||
if (typeof handler !== 'function') { | ||
res.writeHead(500) | ||
return res.end('handler not found: ' + req.url) | ||
} | ||
|
||
res.json = function ({ payload, code = 200 }) { | ||
this.writeHead(code, { 'content-type': 'application/json' }) | ||
this.end(JSON.stringify(payload)) | ||
} | ||
|
||
handler.isDone = true | ||
handler(req, res) | ||
}) | ||
|
||
// We don't need this server keeping the process alive. | ||
this.#server.unref() | ||
} | ||
|
||
/** | ||
* A configuration object that can be passed to an "agent" instance so that | ||
* the agent will communicate with this test server instead of the real | ||
* server. | ||
* | ||
* Important: the `.listen` method must be invoked first in order to have | ||
* the `host` and `port` defined. | ||
* | ||
* @returns {object} | ||
*/ | ||
get agentConfig() { | ||
return { | ||
host: this.host, | ||
port: this.port, | ||
license_key: 'testing', | ||
certificates: [this.cert] | ||
} | ||
} | ||
|
||
/** | ||
* The host the server is listening on. | ||
* | ||
* @returns {string} | ||
*/ | ||
get host() { | ||
return this.#address?.address | ||
} | ||
|
||
/** | ||
* The port number the server is listening on. | ||
* | ||
* @returns {number} | ||
*/ | ||
get port() { | ||
return this.#address?.port | ||
} | ||
|
||
/** | ||
* A copy of the public certificate used to secure the server. Use this | ||
* like `new Agent({ certificates: [collector.cert] })`. | ||
* | ||
* @returns {string} | ||
*/ | ||
get cert() { | ||
return fakeCert.certificate | ||
} | ||
|
||
/** | ||
* The most basic `agent_settings` handler. Useful when you do not need to | ||
* customize the handler. | ||
* | ||
* @returns {function} | ||
*/ | ||
get agentSettingsHandler() { | ||
return function (req, res) { | ||
res.json({ payload: { return_value: [] } }) | ||
} | ||
} | ||
|
||
/** | ||
* The most basic `preconnect` handler. Useful when you do not need to | ||
* customize the handler. | ||
* | ||
* @returns {function} | ||
*/ | ||
get preconnectHandler() { | ||
const host = this.host | ||
const port = this.port | ||
return function (req, res) { | ||
res.json({ | ||
payload: { | ||
return_value: { | ||
redirect_host: `${host}:${port}`, | ||
security_policies: {} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Adds a new handler for the provided endpoint. | ||
* | ||
* @param {string} endpoint A string like | ||
* `/agent_listener/invoke_raw_method?method=preconnect`. Notice that a query | ||
* string with the `method` parameter is present. This is required, as the | ||
* value of `method` will be used to look up the handler when receiving | ||
* requests. | ||
* @param {function} handler A typical `(req, res) => {}` handler. For | ||
* convenience, `res` is extended with a `json({ payload, code = 200 })` | ||
* method for easily sending JSON responses. | ||
*/ | ||
addHandler(endpoint, handler) { | ||
const qs = querystring.decode(endpoint.slice(endpoint.indexOf('?') + 1)) | ||
this.#handlers.set(qs.method, handler) | ||
} | ||
|
||
/** | ||
* Shutdown the server and forcefully close all current connections. | ||
*/ | ||
close() { | ||
this.#server.closeAllConnections() | ||
} | ||
|
||
/** | ||
* Determine if a handler has been invoked. | ||
* | ||
* @param {string} method Name of the method to check, e.g. "preconnect". | ||
* @returns {boolean} | ||
*/ | ||
isDone(method) { | ||
return this.#handlers.get(method)?.isDone === true | ||
} | ||
|
||
/** | ||
* Start the server listening for requests. | ||
* | ||
* @returns {Promise<object>} Returns a standard server address object. | ||
*/ | ||
async listen() { | ||
let address | ||
await new Promise((resolve, reject) => { | ||
this.#server.listen(0, '127.0.0.1', (err) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
address = this.#server.address() | ||
resolve() | ||
}) | ||
}) | ||
|
||
this.#address = address | ||
|
||
// Add handlers for the required agent startup connections. These should | ||
// be overwritten by tests that exercise the startup phase, but adding these | ||
// stubs makes it easier to test other connection events. | ||
this.addHandler(helper.generateCollectorPath('preconnect', 42), this.preconnectHandler) | ||
this.addHandler(helper.generateCollectorPath('connect', 42), (req, res) => { | ||
res.json({ payload: { return_value: { agent_run_id: 42 } } }) | ||
}) | ||
this.addHandler(helper.generateCollectorPath('agent_settings', 42), this.agentSettingsHandler) | ||
|
||
return address | ||
} | ||
} | ||
|
||
module.exports = Collector |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was devised to replace the usage of
nock
intest/unit/agent/agent.test.js
. I encountered an issue where one of the subtests was unable to be resolved due to a timing issue between promise resolution and the event loop finishing. Utilizing "real" network requests helped me resolve the problem. If I remember correctly, it was this test causing grief:node-newrelic/test/unit/agent/agent.test.js
Lines 494 to 504 in 09636a4
Maybe the test could have been reworked with
nock
still being used, but I am less convinced ofnock
's utility nowadays than I used to be. While the pattern is not used by any tests in this PR, by using the approach in this PR we can do things like:Yes, it is possible with
nock
, but, despite using it for many years, I find myself having to go reference the documentation every time I want to do something like that.