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: Fixed mongodb-esm tests in combination with security agent #2444

Merged
merged 1 commit into from
Aug 7, 2024

Conversation

jsumners-nr
Copy link
Contributor

@jsumners-nr jsumners-nr commented Aug 7, 2024

This is a follow up to #2434. After the tests were restored, we started having CI failures when the security agent tests are run. See https://github.com/newrelic/node-newrelic/actions/runs/10245880392 for the first failure.

What I discovered is that the test:

t.test('should generate the correct metrics and segments', function (t) {

Should be ordered before the test:

t.test('should not error outside of a transaction', function (t) {

This is due to the .clear() in:

t.afterEach(function () {
// since we do not bootstrap a new agent between tests
// metrics will leak across runs if we do not clear
agent.metrics.clear()
return common.close(client, db)
})

In short: the .clear() removes the Supportability/API/instrumentDatastore segment added by the security agent when the mongodb module is loaded.

However, swapping the order is not enough. We still end up with global state issues due to the structure of the tests. Initially, I attempted to figure out how to fix the problem within the existing structure, but the tests were just too complicated for me to put them all together in my head. Given all of this, I decided it best to go ahead and convert the whole mongodb-esm test suite to use node:test instead of tap. I was needing to do significant restructuring anyway to get the tests working, and I needed to deconstruct them to understand them. So it made sense to me to solve both jobs in one go.

I maintained a fair bit of the abstraction present in these tests, but I refactored what is left such that it fits with test-scoped "globals" instead of suite scoped globals. Some of the abstraction I removed. As an example, the db.test.mjs suite no longer passes off each test to a dbTest function that internally ran two separate tests. I'm not convinced every test in the suite needs to run with and without transaction tests, but I carried them through. In my view, the structure in this PR is easier to reason through (even though a lot of the tests are still difficult to follow).

@jsumners-nr jsumners-nr marked this pull request as ready for review August 7, 2024 13:04
@jsumners-nr jsumners-nr added the dev:tests Indicates only changes to tests label Aug 7, 2024
@jsumners-nr jsumners-nr merged commit 5d617de into newrelic:main Aug 7, 2024
31 checks passed
@jsumners-nr jsumners-nr deleted the mongodb-esm-test-rewrite branch August 7, 2024 16:05
This was referenced Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev:tests Indicates only changes to tests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants