Skip to content

Commit

Permalink
test: ensure proper cleanup between config tests (elastic#1373)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Sep 24, 2019
1 parent 703e48b commit 72e577f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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
}

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 @@ -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',
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 72e577f

Please sign in to comment.