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: ensure proper cleanup between config tests #1373

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/instrumentation/async-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
const asyncHooks = require('async_hooks')
const shimmer = require('./shimmer')

// FOR INTERNAL TESTING PURPOSES ONLY!
const resettable = process.env._ELASTIC_APM_ASYNC_HOOKS_RESETTABLE === 'true'
let _asyncHook

module.exports = function (ins) {
const asyncHook = asyncHooks.createHook({ init, before, destroy })
const contexts = new WeakMap()

if (resettable) {
if (_asyncHook) _asyncHook.disable()
_asyncHook = asyncHook
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason not to just always do this? 🤔

Copy link
Contributor Author

@watson watson Sep 23, 2019

Choose a reason for hiding this comment

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

Good question... I did so in the first iteration but I wasn't 100% sure if there were any implications of keeping a reference to this object so it couldn't be garbage collected. This way it only kept it around during testing - but it's probably irrelevant 🤷‍♂

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair. Approved anyway just to get the fix in. I can clean it up later. 🤔


const activeTransactions = new Map()
Object.defineProperty(ins, 'currentTransaction', {
get () {
Expand Down
7 changes: 5 additions & 2 deletions test/_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ function clean () {
agent._errorFilters = new Filters()
agent._transactionFilters = new Filters()
agent._spanFilters = new Filters()
if (agent._instrumentation && agent._instrumentation._hook) {
agent._instrumentation._hook.unhook()
if (agent._instrumentation) {
agent._instrumentation._started = false
if (agent._instrumentation._hook) {
agent._instrumentation._hook.unhook()
}
}
agent._metrics.stop()
if (agent._transport && agent._transport.destroy) {
Expand Down
6 changes: 5 additions & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var apmName = require('../package').name

process.env.ELASTIC_APM_METRICS_INTERVAL = '0'
process.env.ELASTIC_APM_CENTRAL_CONFIG = 'false'
process.env._ELASTIC_APM_ASYNC_HOOKS_RESETTABLE = 'true'

var optionFixtures = [
['abortedErrorThreshold', 'ABORTED_ERROR_THRESHOLD', 25],
Expand Down Expand Up @@ -429,7 +430,7 @@ test('serviceName defaults to package name', function (t) {
var exec = promisify(cp.exec)

function testServiceConfig (pkg, handle) {
var tmp = path.join(os.tmpdir(), 'elastic-apm-node-test')
var tmp = path.join(os.tmpdir(), 'elastic-apm-node-test', String(Date.now()))
var files = [
{
action: 'mkdirp',
Expand Down Expand Up @@ -488,6 +489,9 @@ test('serviceName defaults to package name', function (t) {
// the polyfill just returns stdout as a string.
return JSON.parse(result.stdout || result)
})
.catch(err => {
t.error(err)
})

return pFinally(promise, () => {
return rimrafPromise(tmp)
Expand Down