Skip to content

Commit 7a066a5

Browse files
committed
test(child_process): drain ls spans in span-maintenance tests
The shared mock agent now stays alive across every test in the Integration suite. The "should maintain previous span" cases spawn an async ls child process but never await its command_execution span, so a late flush can land during a later test whose expectSomeSpan carries the same ls expectation and be consumed there, masking a regression in that later command instead of failing. Awaiting the span in the originating test consumes it before the next test runs. Refs: #9113 (comment)
1 parent 78c984b commit 7a066a5

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

packages/datadog-plugin-child_process/test/index.spec.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const assert = require('node:assert/strict')
4+
const events = require('node:events')
45

56
const { after, afterEach, before, beforeEach, describe, it } = require('mocha')
67
const sinon = require('sinon')
@@ -528,47 +529,49 @@ describe('Child process plugin', () => {
528529

529530
methods.forEach(({ methodName, async }) => {
530531
describe(methodName, () => {
531-
it('should be instrumented', (done) => {
532-
const expected = {
533-
type: 'system',
534-
name: 'command_execution',
535-
error: 0,
536-
meta: {
537-
component: 'subprocess',
538-
'cmd.shell': 'ls',
539-
'cmd.exit_code': '0',
540-
},
541-
}
532+
const lsExpected = {
533+
type: 'system',
534+
name: 'command_execution',
535+
error: 0,
536+
meta: {
537+
component: 'subprocess',
538+
'cmd.shell': 'ls',
539+
'cmd.exit_code': '0',
540+
},
541+
}
542542

543-
expectSomeSpan(agent, expected).then(done, done)
543+
it('should be instrumented', (done) => {
544+
expectSomeSpan(agent, lsExpected).then(done, done)
544545

545546
const res = childProcess[methodName]('ls')
546547
if (async) {
547548
res.on('close', noop)
548549
}
549550
})
550551

551-
it('should maintain previous span after the execution', (done) => {
552+
it('should maintain previous span after the execution', async () => {
553+
const drained = expectSomeSpan(agent, lsExpected)
554+
552555
const res = childProcess[methodName]('ls')
553-
const span = storage('legacy').getStore()?.span
554-
assert.strictEqual(span, parentSpan)
556+
assert.strictEqual(storage('legacy').getStore()?.span, parentSpan)
555557
if (async) {
556-
res.on('close', () => {
557-
assert.strictEqual(span, parentSpan)
558-
done()
559-
})
558+
await Promise.all([drained, (async () => {
559+
await events.once(res, 'close')
560+
assert.strictEqual(storage('legacy').getStore()?.span, parentSpan)
561+
})()])
560562
} else {
561-
done()
563+
await drained
562564
}
563565
})
564566

565567
if (async) {
566-
it('should maintain previous span in the callback', (done) => {
567-
childProcess[methodName]('ls', () => {
568-
const span = storage('legacy').getStore()?.span
569-
assert.strictEqual(span, parentSpan)
570-
done()
571-
})
568+
it('should maintain previous span in the callback', async () => {
569+
await Promise.all([expectSomeSpan(agent, lsExpected), new Promise(resolve => {
570+
childProcess[methodName]('ls', () => {
571+
assert.strictEqual(storage('legacy').getStore()?.span, parentSpan)
572+
resolve()
573+
})
574+
})])
572575
})
573576
}
574577

0 commit comments

Comments
 (0)