Skip to content

Commit

Permalink
chore: Removed repetitive cache busting
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Apr 24, 2024
1 parent a957304 commit bfb2245
Show file tree
Hide file tree
Showing 29 changed files with 112 additions and 155 deletions.
7 changes: 2 additions & 5 deletions test/lib/agent_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { EventEmitter } = require('events')
const Transaction = require('../../lib/transaction')
const symbols = require('../../lib/symbols')
const InstrumentationTracker = require('../../lib/instrumentation-tracker')
const { removeModules } = require('./cache-buster')
const http = require('http')
const https = require('https')
const semver = require('semver')
Expand Down Expand Up @@ -220,11 +221,7 @@ helper.maybeLoadSecurityAgent = function maybeLoadSecurityAgent(agent) {
*/
helper.maybeUnloadSecurityAgent = function maybeUnloadSecurityAgent(agent) {
if (helper.isSecurityAgentEnabled(agent)) {
Object.keys(require.cache).forEach((key) => {
if (key.includes('@newrelic/security-agent')) {
delete require.cache[key]
}
})
removeModules(['@newrelic/security-agent'])
}
}

Expand Down
53 changes: 53 additions & 0 deletions test/lib/cache-buster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

/**
* Utility method to remove a set of modules from the require cache.
*
* @param {string[]} modules The set of module names to remove from the cache.
*/
module.exports = {
/**
* Removes explicitly named modules from the require cache.
*
* @param {string[]} modules
*
* @returns {number} The number of cache entries removed.
*/
removeModules(modules = []) {
let removed = 0
const keys = Object.keys(require.cache)
for (const key of keys) {
if (modules.includes(key) === false) {
continue
}
delete require.cache[key]
removed += 1
}
return removed
},

/**
* Removes modules from the require cache that are identified by a matcher.
*
* @param {RegExp} matcher
*
* @returns {number} The number of cache entries removed.
*/
removeMatchedModules(matcher) {
let removed = 0
const keys = Object.keys(require.cache)
for (const key of keys) {
if (matcher.test(key) === false) {
continue
}
delete require.cache[key]
removed += 1
}
return removed
}
}
7 changes: 2 additions & 5 deletions test/unit/config/config-location.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const fs = require('fs')
const fsPromises = require('fs/promises')
const sinon = require('sinon')

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

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

const mainModuleRegex = new RegExp(MAIN_MODULE_DIR)
Object.keys(require.cache).forEach((key) => {
if (mainModuleRegex.test(key)) {
delete require.cache[key]
}
})
removeMatchedModules(mainModuleRegex)
})

t.test('should load the default newrelic.js config file', (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/unit/instrumentation/koa/koa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../../lib/agent_helper')
const { removeModules } = require('../../../lib/cache-buster')
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')

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

tap.afterEach((t) => {
helper.unloadAgent(t.context.agent)
Object.keys(require.cache).forEach((key) => {
if (key.includes('koa')) {
delete require.cache[key]
}
})
removeModules(['koa'])
})

tap.test('Koa instrumentation', async (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/unit/instrumentation/koa/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const tap = require('tap')
const { METHODS } = require('../../../../lib/instrumentation/http-methods')
const helper = require('../../../lib/agent_helper')
const { removeModules } = require('../../../lib/cache-buster')
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')

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

tap.afterEach((t) => {
helper.unloadAgent(t.context.agent)
Object.keys(require.cache).forEach((key) => {
if (key.includes('koa-route')) {
delete require.cache[key]
}
})
removeModules(['koa-route'])
})

tap.test('methods', function (t) {
Expand Down
13 changes: 3 additions & 10 deletions test/unit/instrumentation/koa/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const tap = require('tap')
const instrumentation = require('../../../../lib/instrumentation/koa/router-instrumentation')
const { METHODS } = require('../../../../lib/instrumentation/http-methods')
const helper = require('../../../lib/agent_helper')
const { removeModules } = require('../../../lib/cache-buster')
const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor')
const WRAPPED_METHODS = ['param', 'register', 'routes', 'middleware', 'allowedMethods']
const UNWRAPPED_METHODS = METHODS.concat([
Expand Down Expand Up @@ -45,11 +46,7 @@ tap.test('koa-router', (t) => {

t.afterEach((t) => {
helper.unloadAgent(t.context.agent)
Object.keys(require.cache).forEach((key) => {
if (key.includes(koaRouterMod)) {
delete require.cache[key]
}
})
removeModules([koaRouterMod])
})

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

t.afterEach((t) => {
helper.unloadAgent(t.context.agent)
Object.keys(require.cache).forEach((key) => {
if (key.includes(koaRouterMod)) {
delete require.cache[key]
}
})
removeModules([koaRouterMod])
})

t.test('mounting paramware', async (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/amqplib/callback.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const amqpUtils = require('./amqp-utils')
const API = require('../../../api')
const helper = require('../../lib/agent_helper')
const { removeMatchedModules } = require('../../lib/cache-buster')
const tap = require('tap')

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

t.afterEach(function () {
helper.unloadAgent(agent)
Object.keys(require.cache).forEach(function (key) {
if (/amqplib/.test(key)) {
delete require.cache[key]
}
})
removeMatchedModules(/amqplib/)

if (!conn) {
return
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/amqplib/promises.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const amqpUtils = require('./amqp-utils')
const API = require('../../../api')
const helper = require('../../lib/agent_helper')
const { removeMatchedModules } = require('../../lib/cache-buster')
const tap = require('tap')

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

agent = helper.instrumentMockedAgent({
attributes: {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/bunyan/bunyan.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeMatchedModules } = require('../../lib/cache-buster')
require('../../lib/logging-helper')
const { LOGGING } = require('../../../lib/metrics/names')
const { makeSink, logStuff, originalMsgAssertion, logForwardingMsgAssertion } = require('./helpers')
Expand All @@ -28,11 +29,7 @@ tap.test('bunyan instrumentation', (t) => {
bunyan = null
// must purge require cache of bunyan related instrumentation
// to ensure it re-registers on subsequent test runs
Object.keys(require.cache).forEach((key) => {
if (/bunyan/.test(key)) {
delete require.cache[key]
}
})
removeMatchedModules(/bunyan/)
})

t.test('logging disabled', (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/client-bidi-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const { ERR_CODE, ERR_MSG } = require('./constants.cjs')

const {
Expand Down Expand Up @@ -41,11 +42,7 @@ tap.test('gRPC Client: Bidi Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test(
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/client-server-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const { ERR_CODE, ERR_MSG } = require('./constants.cjs')

const {
Expand Down Expand Up @@ -41,11 +42,7 @@ tap.test('gRPC Client: Server Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track server streaming requests as an external when in a transaction', (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/client-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const { ERR_CODE, ERR_MSG, HALT_CODE, HALT_SERVER_ERR_MSG } = require('./constants.cjs')

const {
Expand Down Expand Up @@ -41,11 +42,7 @@ tap.test('gRPC Client: Client Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track client streaming requests as an external when in a transaction', (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/client-unary.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const { ERR_CODE, ERR_MSG } = require('./constants.cjs')

const {
Expand Down Expand Up @@ -41,11 +42,7 @@ tap.test('gRPC Client: Unary Requests', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track unary client requests as an external when in a transaction', (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/server-bidi-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS
const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT
const { ERR_CODE, ERR_SERVER_MSG } = require('./constants.cjs')
Expand Down Expand Up @@ -45,11 +46,7 @@ tap.test('gRPC Server: Bidi Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track bidirectional requests', async (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/server-client-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS
const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT
const { ERR_CODE, ERR_SERVER_MSG, HALT_CODE, HALT_GRPC_SERVER_MSG } = require('./constants.cjs')
Expand Down Expand Up @@ -45,11 +46,7 @@ tap.test('gRPC Server: Client Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track client streaming requests', async (t) => {
Expand Down
7 changes: 2 additions & 5 deletions test/versioned/grpc/server-streaming.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
const { removeModules } = require('../../lib/cache-buster')
const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS
const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT
const { ERR_CODE, ERR_SERVER_MSG } = require('./constants.cjs')
Expand Down Expand Up @@ -45,11 +46,7 @@ tap.test('gRPC Server: Server Streaming', (t) => {
client.close()
grpc = null
proto = null
Object.keys(require.cache).forEach((key) => {
if (key.includes('@grpc/grpc-js')) {
delete require.cache[key]
}
})
removeModules(['@grpc/grpc-js'])
})

t.test('should track server-streaming requests', async (t) => {
Expand Down
Loading

0 comments on commit bfb2245

Please sign in to comment.