From 72e577fe09a446be25677bdf9bbf5e96cd687ddc Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Mon, 23 Sep 2019 22:41:05 +0200 Subject: [PATCH] test: ensure proper cleanup between config tests (#1373) --- lib/instrumentation/async-hooks.js | 9 +++++++++ test/_agent.js | 7 +++++-- test/config.js | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/instrumentation/async-hooks.js b/lib/instrumentation/async-hooks.js index 46806c2c48..7339a67477 100644 --- a/lib/instrumentation/async-hooks.js +++ b/lib/instrumentation/async-hooks.js @@ -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 + } + const activeTransactions = new Map() Object.defineProperty(ins, 'currentTransaction', { get () { diff --git a/test/_agent.js b/test/_agent.js index ea36303add..ab23169b70 100644 --- a/test/_agent.js +++ b/test/_agent.js @@ -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) { diff --git a/test/config.js b/test/config.js index b6604ac648..26783f200f 100644 --- a/test/config.js +++ b/test/config.js @@ -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], @@ -428,7 +429,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', @@ -487,6 +488,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)