Skip to content

Commit 203ae96

Browse files
[test optimization] Fix threads config in vitest (#6824)
1 parent 05caa13 commit 203ae96

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

integration-tests/vitest/vitest.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ const {
5252
TEST_RETRY_REASON_TYPES,
5353
TEST_IS_MODIFIED,
5454
DD_CAPABILITIES_IMPACTED_TESTS,
55-
VITEST_POOL
55+
VITEST_POOL,
56+
TEST_IS_TEST_FRAMEWORK_WORKER
5657
} = require('../../packages/dd-trace/src/plugins/util/test')
5758
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
5859
const { NODE_MAJOR } = require('../../version')
@@ -209,12 +210,20 @@ versions.forEach((version) => {
209210
)
210211

211212
testEvents.forEach(test => {
213+
// `threads` config will report directly. TODO: update this once we're testing vitest@>=4
214+
if (poolConfig === 'forks') {
215+
assert.equal(test.content.meta[TEST_IS_TEST_FRAMEWORK_WORKER], 'true')
216+
}
212217
assert.equal(test.content.meta[TEST_COMMAND], 'vitest run')
213218
assert.exists(test.content.metrics[DD_HOST_CPU_COUNT])
214219
assert.equal(test.content.meta[DD_TEST_IS_USER_PROVIDED_SERVICE], 'false')
215220
})
216221

217222
testSuiteEvents.forEach(testSuite => {
223+
// `threads` config will report directly. TODO: update this once we're testing vitest@>=4
224+
if (poolConfig === 'forks') {
225+
assert.equal(testSuite.content.meta[TEST_IS_TEST_FRAMEWORK_WORKER], 'true')
226+
}
218227
assert.equal(testSuite.content.meta[TEST_COMMAND], 'vitest run')
219228
assert.isTrue(
220229
testSuite.content.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/vitest-tests/test-visibility')

packages/datadog-plugin-vitest/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ class VitestPlugin extends CiPlugin {
345345
finishAllTraceSpans(testSuiteSpan)
346346
}
347347
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'suite')
348-
// TODO: too frequent flush - find for method in worker to decrease frequency
349348
this.tracer._exporter.flush(onFinish)
350349
if (this.runningTestProbe) {
351350
this.removeDiProbe(this.runningTestProbe)

packages/dd-trace/src/ci-visibility/exporters/test-worker/writer.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict'
22
const { JSONEncoder } = require('../../encode/json-encoder')
33
const { getEnvironmentVariable } = require('../../../config-helper')
4+
const log = require('../../../log')
5+
const {
6+
VITEST_WORKER_TRACE_PAYLOAD_CODE,
7+
VITEST_WORKER_LOGS_PAYLOAD_CODE
8+
} = require('../../../plugins/util/test')
49

510
class Writer {
611
constructor (interprocessCode) {
@@ -26,25 +31,42 @@ class Writer {
2631
_sendPayload (data, onDone = () => {}) {
2732
// ## Jest
2833
// Only available when `child_process` is used for the jest worker.
29-
// https://github.com/facebook/jest/blob/bb39cb2c617a3334bf18daeca66bd87b7ccab28b/packages/jest-worker/README.md#experimental-worker
3034
// If worker_threads is used, this will not work
31-
// TODO: make it compatible with worker_threads
35+
// TODO: make `jest` instrumentation compatible with worker_threads
36+
// https://github.com/facebook/jest/blob/bb39cb2c617a3334bf18daeca66bd87b7ccab28b/packages/jest-worker/README.md#experimental-worker
3237

3338
// ## Cucumber
3439
// This reports to the test's main process the same way test data is reported by Cucumber
3540
// See cucumber code:
3641
// https://github.com/cucumber/cucumber-js/blob/5ce371870b677fe3d1a14915dc535688946f734c/src/runtime/parallel/run_worker.ts#L13
37-
if (process.send) { // it only works if process.send is available
38-
// Old because vitest@>=4 use DD_VITEST_WORKER and report arrays just like other frameworks
39-
const isVitestWorkerOld = !!getEnvironmentVariable('TINYPOOL_WORKER_ID')
4042

41-
const payload = isVitestWorkerOld
42-
? { __tinypool_worker_message__: true, interprocessCode: this._interprocessCode, data }
43-
: [this._interprocessCode, data]
43+
// Old because vitest@>=4 uses `DD_VITEST_WORKER` and reports arrays just like other frameworks
44+
// Before vitest@>=4, we need the `__tinypool_worker_message__` property, or tinypool will crash
45+
const isVitestWorkerOld = !!getEnvironmentVariable('TINYPOOL_WORKER_ID')
46+
const payload = isVitestWorkerOld
47+
? { __tinypool_worker_message__: true, interprocessCode: this._interprocessCode, data }
48+
: [this._interprocessCode, data]
4449

50+
const isVitestTestWorker =
51+
this._interprocessCode === VITEST_WORKER_TRACE_PAYLOAD_CODE ||
52+
this._interprocessCode === VITEST_WORKER_LOGS_PAYLOAD_CODE
53+
54+
if (process.send) {
4555
process.send(payload, () => {
4656
onDone()
4757
})
58+
} else if (isVitestTestWorker) { // TODO: worker_threads are only supported in vitest right now
59+
const { isMainThread, parentPort } = require('worker_threads')
60+
if (isMainThread) {
61+
return onDone()
62+
}
63+
try {
64+
parentPort.postMessage(payload)
65+
} catch (error) {
66+
log.error('Error posting message to parent port', error)
67+
} finally {
68+
onDone()
69+
}
4870
} else {
4971
onDone()
5072
}

packages/dd-trace/src/plugins/ci_plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const {
3434
getLibraryCapabilitiesTags,
3535
getPullRequestDiff,
3636
getModifiedFilesFromDiff,
37-
getPullRequestBaseBranch
37+
getPullRequestBaseBranch,
38+
TEST_IS_TEST_FRAMEWORK_WORKER
3839
} = require('./util/test')
3940
const { getRepositoryRoot } = require('./util/git')
4041
const Plugin = require('./plugin')
@@ -311,6 +312,7 @@ module.exports = class CiPlugin extends Plugin {
311312
span.parent_id = id(span.parent_id)
312313

313314
if (span.name?.startsWith(`${this.constructor.id}.`)) {
315+
span.meta[TEST_IS_TEST_FRAMEWORK_WORKER] = 'true'
314316
// augment with git information (since it will not be available in the worker)
315317
for (const key in this.testEnvironmentMetadata) {
316318
// CAREFUL: this bypasses the metadata/metrics distinction

packages/dd-trace/src/plugins/util/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ const PLAYWRIGHT_WORKER_TRACE_PAYLOAD_CODE = 90
132132
const VITEST_WORKER_TRACE_PAYLOAD_CODE = 100
133133
const VITEST_WORKER_LOGS_PAYLOAD_CODE = 102
134134

135+
const TEST_IS_TEST_FRAMEWORK_WORKER = 'test.is_test_framework_worker'
136+
135137
// Library Capabilities Tagging
136138
const DD_CAPABILITIES_TEST_IMPACT_ANALYSIS = '_dd.library_capabilities.test_impact_analysis'
137139
const DD_CAPABILITIES_EARLY_FLAKE_DETECTION = '_dd.library_capabilities.early_flake_detection'
@@ -226,6 +228,7 @@ module.exports = {
226228
PLAYWRIGHT_WORKER_TRACE_PAYLOAD_CODE,
227229
VITEST_WORKER_TRACE_PAYLOAD_CODE,
228230
VITEST_WORKER_LOGS_PAYLOAD_CODE,
231+
TEST_IS_TEST_FRAMEWORK_WORKER,
229232
TEST_SOURCE_START,
230233
TEST_SKIPPED_BY_ITR,
231234
TEST_IS_NEW,

0 commit comments

Comments
 (0)