Skip to content

Commit 5ac870e

Browse files
authored
chore: Removed repetitive cache busting (#2160)
1 parent a957304 commit 5ac870e

29 files changed

+114
-155
lines changed

test/lib/agent_helper.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { EventEmitter } = require('events')
1717
const Transaction = require('../../lib/transaction')
1818
const symbols = require('../../lib/symbols')
1919
const InstrumentationTracker = require('../../lib/instrumentation-tracker')
20+
const { removeModules } = require('./cache-buster')
2021
const http = require('http')
2122
const https = require('https')
2223
const semver = require('semver')
@@ -220,11 +221,7 @@ helper.maybeLoadSecurityAgent = function maybeLoadSecurityAgent(agent) {
220221
*/
221222
helper.maybeUnloadSecurityAgent = function maybeUnloadSecurityAgent(agent) {
222223
if (helper.isSecurityAgentEnabled(agent)) {
223-
Object.keys(require.cache).forEach((key) => {
224-
if (key.includes('@newrelic/security-agent')) {
225-
delete require.cache[key]
226-
}
227-
})
224+
removeModules(['@newrelic/security-agent'])
228225
}
229226
}
230227

test/lib/cache-buster.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
/**
9+
* Utility method to remove a set of modules from the require cache.
10+
*
11+
* @param {string[]} modules The set of module names to remove from the cache.
12+
*/
13+
module.exports = {
14+
/**
15+
* Removes explicitly named modules from the require cache.
16+
*
17+
* @param {string[]} modules
18+
*
19+
* @returns {number} The number of cache entries removed.
20+
*/
21+
removeModules(modules = []) {
22+
let removed = 0
23+
const keys = Object.keys(require.cache)
24+
for (const mod of modules) {
25+
for (const key of keys) {
26+
if (key.includes(mod) === false) {
27+
continue
28+
}
29+
delete require.cache[key]
30+
removed += 1
31+
}
32+
}
33+
return removed
34+
},
35+
36+
/**
37+
* Removes modules from the require cache that are identified by a matcher.
38+
*
39+
* @param {RegExp} matcher
40+
*
41+
* @returns {number} The number of cache entries removed.
42+
*/
43+
removeMatchedModules(matcher) {
44+
let removed = 0
45+
const keys = Object.keys(require.cache)
46+
for (const key of keys) {
47+
if (matcher.test(key) === false) {
48+
continue
49+
}
50+
delete require.cache[key]
51+
removed += 1
52+
}
53+
return removed
54+
}
55+
}

test/unit/config/config-location.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const fs = require('fs')
1111
const fsPromises = require('fs/promises')
1212
const sinon = require('sinon')
1313

14+
const { removeMatchedModules } = require('../../lib/cache-buster')
1415
const Config = require('../../../lib/config')
1516

1617
tap.test('when overriding the config file location via NEW_RELIC_HOME', (t) => {
@@ -153,11 +154,7 @@ tap.test('Selecting config file path', (t) => {
153154
process.chdir(originalWorkingDirectory)
154155

155156
const mainModuleRegex = new RegExp(MAIN_MODULE_DIR)
156-
Object.keys(require.cache).forEach((key) => {
157-
if (mainModuleRegex.test(key)) {
158-
delete require.cache[key]
159-
}
160-
})
157+
removeMatchedModules(mainModuleRegex)
161158
})
162159

163160
t.test('should load the default newrelic.js config file', (t) => {

test/unit/instrumentation/koa/koa.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const tap = require('tap')
99
const helper = require('../../../lib/agent_helper')
10+
const { removeModules } = require('../../../lib/cache-buster')
1011
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')
1112

1213
tap.beforeEach((t) => {
@@ -23,11 +24,7 @@ tap.beforeEach((t) => {
2324

2425
tap.afterEach((t) => {
2526
helper.unloadAgent(t.context.agent)
26-
Object.keys(require.cache).forEach((key) => {
27-
if (key.includes('koa')) {
28-
delete require.cache[key]
29-
}
30-
})
27+
removeModules(['koa'])
3128
})
3229

3330
tap.test('Koa instrumentation', async (t) => {

test/unit/instrumentation/koa/route.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const tap = require('tap')
99
const { METHODS } = require('../../../../lib/instrumentation/http-methods')
1010
const helper = require('../../../lib/agent_helper')
11+
const { removeModules } = require('../../../lib/cache-buster')
1112
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')
1213

1314
tap.beforeEach((t) => {
@@ -24,11 +25,7 @@ tap.beforeEach((t) => {
2425

2526
tap.afterEach((t) => {
2627
helper.unloadAgent(t.context.agent)
27-
Object.keys(require.cache).forEach((key) => {
28-
if (key.includes('koa-route')) {
29-
delete require.cache[key]
30-
}
31-
})
28+
removeModules(['koa-route'])
3229
})
3330

3431
tap.test('methods', function (t) {

test/unit/instrumentation/koa/router.test.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const tap = require('tap')
1010
const instrumentation = require('../../../../lib/instrumentation/koa/router-instrumentation')
1111
const { METHODS } = require('../../../../lib/instrumentation/http-methods')
1212
const helper = require('../../../lib/agent_helper')
13+
const { removeModules } = require('../../../lib/cache-buster')
1314
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')
1415
const WRAPPED_METHODS = ['param', 'register', 'routes', 'middleware', 'allowedMethods']
1516
const UNWRAPPED_METHODS = METHODS.concat([
@@ -45,11 +46,7 @@ tap.test('koa-router', (t) => {
4546

4647
t.afterEach((t) => {
4748
helper.unloadAgent(t.context.agent)
48-
Object.keys(require.cache).forEach((key) => {
49-
if (key.includes(koaRouterMod)) {
50-
delete require.cache[key]
51-
}
52-
})
49+
removeModules([koaRouterMod])
5350
})
5451

5552
t.test('mounting paramware', async (t) => {
@@ -99,11 +96,7 @@ tap.test('koa-router', (t) => {
9996

10097
t.afterEach((t) => {
10198
helper.unloadAgent(t.context.agent)
102-
Object.keys(require.cache).forEach((key) => {
103-
if (key.includes(koaRouterMod)) {
104-
delete require.cache[key]
105-
}
106-
})
99+
removeModules([koaRouterMod])
107100
})
108101

109102
t.test('mounting paramware', async (t) => {

test/versioned/amqplib/callback.tap.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const amqpUtils = require('./amqp-utils')
99
const API = require('../../../api')
1010
const helper = require('../../lib/agent_helper')
11+
const { removeMatchedModules } = require('../../lib/cache-buster')
1112
const tap = require('tap')
1213

1314
/*
@@ -64,11 +65,7 @@ tap.test('amqplib callback instrumentation', function (t) {
6465

6566
t.afterEach(function () {
6667
helper.unloadAgent(agent)
67-
Object.keys(require.cache).forEach(function (key) {
68-
if (/amqplib/.test(key)) {
69-
delete require.cache[key]
70-
}
71-
})
68+
removeMatchedModules(/amqplib/)
7269

7370
if (!conn) {
7471
return

test/versioned/amqplib/promises.tap.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const amqpUtils = require('./amqp-utils')
99
const API = require('../../../api')
1010
const helper = require('../../lib/agent_helper')
11+
const { removeMatchedModules } = require('../../lib/cache-buster')
1112
const tap = require('tap')
1213

1314
/*
@@ -37,11 +38,7 @@ tap.test('amqplib promise instrumentation', function (t) {
3738
// which the test itself re-requires, but second-order modules (deps of
3839
// instrumented methods) are not reloaded and thus not re-instrumented. To
3940
// resolve this we just delete everything. Kill it all.
40-
Object.keys(require.cache).forEach(function (key) {
41-
if (/amqplib|bluebird/.test(key)) {
42-
delete require.cache[key]
43-
}
44-
})
41+
removeMatchedModules(/amqplib|bluebird/)
4542

4643
agent = helper.instrumentMockedAgent({
4744
attributes: {

test/versioned/bunyan/bunyan.tap.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const tap = require('tap')
99
const helper = require('../../lib/agent_helper')
10+
const { removeMatchedModules } = require('../../lib/cache-buster')
1011
require('../../lib/logging-helper')
1112
const { LOGGING } = require('../../../lib/metrics/names')
1213
const { makeSink, logStuff, originalMsgAssertion, logForwardingMsgAssertion } = require('./helpers')
@@ -28,11 +29,7 @@ tap.test('bunyan instrumentation', (t) => {
2829
bunyan = null
2930
// must purge require cache of bunyan related instrumentation
3031
// to ensure it re-registers on subsequent test runs
31-
Object.keys(require.cache).forEach((key) => {
32-
if (/bunyan/.test(key)) {
33-
delete require.cache[key]
34-
}
35-
})
32+
removeMatchedModules(/bunyan/)
3633
})
3734

3835
t.test('logging disabled', (t) => {

test/versioned/grpc/client-bidi-streaming.tap.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const tap = require('tap')
99
const helper = require('../../lib/agent_helper')
10+
const { removeModules } = require('../../lib/cache-buster')
1011
const { ERR_CODE, ERR_MSG } = require('./constants.cjs')
1112

1213
const {
@@ -41,11 +42,7 @@ tap.test('gRPC Client: Bidi Streaming', (t) => {
4142
client.close()
4243
grpc = null
4344
proto = null
44-
Object.keys(require.cache).forEach((key) => {
45-
if (key.includes('@grpc/grpc-js')) {
46-
delete require.cache[key]
47-
}
48-
})
45+
removeModules(['@grpc/grpc-js'])
4946
})
5047

5148
t.test(

0 commit comments

Comments
 (0)