|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { EOL } = require('node:os') |
4 | | -const { spawn } = require('node:child_process') |
5 | 3 | const { randomUUID } = require('node:crypto') |
6 | 4 | const assert = require('node:assert') |
7 | 5 |
|
8 | | -const getPort = require('get-port') |
9 | 6 | const Axios = require('axios') |
10 | 7 |
|
11 | | -const { createSandbox, FakeAgent, assertObjectContains } = require('../helpers') |
| 8 | +const { createSandbox, FakeAgent, assertObjectContains, spawnProc } = require('../helpers') |
12 | 9 | const { generateProbeConfig } = require('../../packages/dd-trace/test/debugger/devtools_client/utils') |
13 | 10 |
|
14 | 11 | // A race condition exists where the tracer receives a probe via RC, before Node.js has had a chance to load all the JS |
@@ -50,48 +47,22 @@ describe('Dynamic Instrumentation Probe Re-Evaluation', function () { |
50 | 47 |
|
51 | 48 | function genTestsForSourceFile (sourceFile) { |
52 | 49 | return function () { |
53 | | - let rcConfig, appPort, agent, proc, axios |
| 50 | + let rcConfig, agent, proc, axios |
54 | 51 |
|
55 | 52 | beforeEach(async function () { |
56 | 53 | rcConfig = { |
57 | 54 | product: 'LIVE_DEBUGGING', |
58 | 55 | id: `logProbe_${randomUUID()}`, |
59 | 56 | config: generateProbeConfig({ sourceFile, line: 4 }) |
60 | 57 | } |
61 | | - appPort = await getPort() |
62 | 58 | agent = await new FakeAgent().start() |
63 | | - proc = spawn( |
64 | | - process.execPath, |
65 | | - ['--import', 'dd-trace/initialize.mjs', sourceFile], |
66 | | - { |
67 | | - cwd: sandbox.folder, |
68 | | - env: { |
69 | | - APP_PORT: appPort, |
70 | | - DD_DYNAMIC_INSTRUMENTATION_ENABLED: true, |
71 | | - DD_TRACE_AGENT_PORT: agent.port, |
72 | | - DD_TRACE_DEBUG: process.env.DD_TRACE_DEBUG // inherit to make debugging the sandbox easier |
73 | | - } |
74 | | - } |
75 | | - ) |
76 | | - proc |
77 | | - .on('exit', (code) => { |
78 | | - if (code !== 0) { |
79 | | - throw new Error(`Child process exited with code ${code}`) |
80 | | - } |
81 | | - }) |
82 | | - .on('error', (error) => { |
83 | | - throw error |
84 | | - }) |
85 | | - proc.stdout.on('data', log.bind(null, '[child process stdout]')) |
86 | | - proc.stderr.on('data', log.bind(null, '[child process stderr]')) |
87 | | - axios = Axios.create({ |
88 | | - baseURL: `http://localhost:${appPort}` |
89 | | - }) |
| 59 | + proc = undefined |
90 | 60 | }) |
91 | 61 |
|
92 | 62 | afterEach(async function () { |
93 | 63 | proc?.kill(0) |
94 | 64 | await agent?.stop() |
| 65 | + axios = undefined |
95 | 66 | }) |
96 | 67 |
|
97 | 68 | for (let attempt = 1; attempt <= 5; attempt++) { |
@@ -138,13 +109,24 @@ describe('Dynamic Instrumentation Probe Re-Evaluation', function () { |
138 | 109 | }) |
139 | 110 |
|
140 | 111 | agent.addRemoteConfig(rcConfig) |
| 112 | + |
| 113 | + spawnProc(sourceFile, { |
| 114 | + cwd: sandbox.folder, |
| 115 | + env: { |
| 116 | + NODE_OPTIONS: '--import dd-trace/initialize.mjs', |
| 117 | + DD_DYNAMIC_INSTRUMENTATION_ENABLED: 'true', |
| 118 | + DD_TRACE_AGENT_PORT: agent.port, |
| 119 | + DD_TRACE_DEBUG: process.env.DD_TRACE_DEBUG, // inherit to make debugging the sandbox easier |
| 120 | + DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS: 0.1 |
| 121 | + } |
| 122 | + }).then(_proc => { |
| 123 | + proc = _proc |
| 124 | + // Possible race condition, in case axios.get() is called in the test before it's created here. But we have |
| 125 | + // to start the test quickly in order to test the re-evaluation of the probe. |
| 126 | + axios = Axios.create({ baseURL: proc.url }) |
| 127 | + }) |
141 | 128 | }) |
142 | 129 | } |
143 | 130 | } |
144 | 131 | } |
145 | 132 | }) |
146 | | - |
147 | | -function log (prefix, data) { |
148 | | - const msg = data.toString().trim().split(EOL).map((line) => `${prefix} ${line}`).join(EOL) |
149 | | - console.log(msg) // eslint-disable-line no-console |
150 | | -} |
0 commit comments