1
+ 'use strict'
2
+
1
3
const { addHook, channel } = require ( './helpers/instrument' )
2
4
const shimmer = require ( '../../datadog-shimmer' )
3
5
const log = require ( '../../dd-trace/src/log' )
6
+ const {
7
+ VITEST_WORKER_TRACE_PAYLOAD_CODE ,
8
+ VITEST_WORKER_LOGS_PAYLOAD_CODE
9
+ } = require ( '../../dd-trace/src/plugins/util/test' )
10
+
4
11
// test hooks
5
12
const testStartCh = channel ( 'ci:vitest:test:start' )
6
13
const testFinishTimeCh = channel ( 'ci:vitest:test:finish-time' )
@@ -27,7 +34,8 @@ const isEarlyFlakeDetectionFaultyCh = channel('ci:vitest:is-early-flake-detectio
27
34
const testManagementTestsCh = channel ( 'ci:vitest:test-management-tests' )
28
35
const impactedTestsCh = channel ( 'ci:vitest:modified-tests' )
29
36
30
- const workerReporterCh = channel ( 'ci:vitest:worker-report:trace' )
37
+ const workerReportTraceCh = channel ( 'ci:vitest:worker-report:trace' )
38
+ const workerReportLogsCh = channel ( 'ci:vitest:worker-report:logs' )
31
39
32
40
const taskToCtx = new WeakMap ( )
33
41
const taskToStatuses = new WeakMap ( )
@@ -39,6 +47,7 @@ const modifiedTasks = new WeakSet()
39
47
let isRetryReasonEfd = false
40
48
let isRetryReasonAttemptToFix = false
41
49
const switchedStatuses = new WeakSet ( )
50
+ const workerProcesses = new WeakSet ( )
42
51
43
52
const BREAKPOINT_HIT_GRACE_PERIOD_MS = 400
44
53
@@ -66,8 +75,6 @@ function getProvidedContext () {
66
75
_ddModifiedTests : modifiedTests
67
76
} = globalThis . __vitest_worker__ . providedContext
68
77
69
- // console.log('globalThis.__vitest_worker__', globalThis.__vitest_worker__)
70
-
71
78
return {
72
79
isDiEnabled : _ddIsDiEnabled ,
73
80
isEarlyFlakeDetectionEnabled : _ddIsEarlyFlakeDetectionEnabled ,
@@ -195,11 +202,6 @@ function getSortWrapper (sort, frameworkVersion) {
195
202
let testManagementAttemptToFixRetries = 0
196
203
let isDiEnabled = false
197
204
198
- // console.log('this.ctx.vitest', this.ctx.vitest)
199
- // console.log('this.ctx.rpc', this.ctx.rpc)
200
- console . log ( '__vitest_worker__' , global . __vitest_worker__ )
201
- // console.log('this.ctx.getCoreWorkspaceProject()', this.ctx.getCoreWorkspaceProject())
202
- // console.log('this.ctx', this)
203
205
try {
204
206
const { err, libraryConfig } = await getChannelPromise ( libraryConfigurationCh , frameworkVersion )
205
207
if ( ! err ) {
@@ -275,7 +277,6 @@ function getSortWrapper (sort, frameworkVersion) {
275
277
log . warn ( 'Could not send Dynamic Instrumentation configuration to workers.' )
276
278
}
277
279
}
278
- debugger
279
280
280
281
if ( isTestManagementTestsEnabled ) {
281
282
const { err, testManagementTests : receivedTestManagementTests } = await getChannelPromise ( testManagementTestsCh )
@@ -369,32 +370,18 @@ function getCreateCliWrapper (vitestPackage, frameworkVersion) {
369
370
return vitestPackage
370
371
}
371
372
372
- // UNUSED RIGHT NOW, but we can use it to bind the async resource to the test fn
373
- // getFn is what's used to get the test fn to run it with vitest:
374
- // https://github.com/vitest-dev/vitest/blob/0cbad1b0d0d56f1ec60f8496678d1435f8bb8977/packages/runner/src/run.ts#L315-L321
375
- let getFn = null
376
-
377
- // run in workers only
378
- addHook ( {
379
- name : '@vitest/runner' ,
380
- versions : [ '>=1.6.0' ] ,
381
- file : 'dist/index.js'
382
- } , ( suitePackage ) => {
383
- getFn = suitePackage . getFn
384
-
385
- return suitePackage
386
- } )
387
-
388
- const processToHandler = new WeakSet ( )
389
-
390
373
function threadHandler ( thread ) {
391
- if ( processToHandler . has ( thread . process ) ) {
374
+ if ( workerProcesses . has ( thread . process ) ) {
392
375
return
393
376
}
394
- processToHandler . add ( thread . process )
377
+ workerProcesses . add ( thread . process )
395
378
thread . process . on ( 'message' , ( message ) => {
396
379
if ( message . __tinypool_worker_message__ && message . data ) {
397
- workerReporterCh . publish ( message . data )
380
+ if ( message . interprocessCode === VITEST_WORKER_TRACE_PAYLOAD_CODE ) {
381
+ workerReportTraceCh . publish ( message . data )
382
+ } else if ( message . interprocessCode === VITEST_WORKER_LOGS_PAYLOAD_CODE ) {
383
+ workerReportLogsCh . publish ( message . data )
384
+ }
398
385
}
399
386
} )
400
387
}
@@ -405,12 +392,11 @@ addHook({
405
392
file : 'dist/index.js'
406
393
} , ( TinyPool ) => {
407
394
shimmer . wrap ( TinyPool . prototype , 'run' , run => async function ( ) {
408
- // we need to do this before and after because the threads list gets recycled
409
- // (the processes are re-created)
395
+ // We have to do this before and after because the threads list gets recycled, that is, the processes are re-created
410
396
this . threads . forEach ( threadHandler )
411
- const res = await run . apply ( this , arguments )
397
+ const runResult = await run . apply ( this , arguments )
412
398
this . threads . forEach ( threadHandler )
413
- return res
399
+ return runResult
414
400
} )
415
401
416
402
return TinyPool
@@ -426,7 +412,6 @@ addHook({
426
412
// `onBeforeRunTask` is run before any repetition or attempt is run
427
413
// `onBeforeRunTask` is an async function
428
414
shimmer . wrap ( VitestTestRunner . prototype , 'onBeforeRunTask' , onBeforeRunTask => function ( task ) {
429
- // console.log('on before run task', process.env)
430
415
const testName = getTestName ( task )
431
416
432
417
const {
@@ -512,7 +497,6 @@ addHook({
512
497
// `onAfterRunTask` is run after all repetitions or attempts are run
513
498
// `onAfterRunTask` is an async function
514
499
shimmer . wrap ( VitestTestRunner . prototype , 'onAfterRunTask' , onAfterRunTask => function ( task ) {
515
- // console.log('task', task)
516
500
const { isEarlyFlakeDetectionEnabled, isTestManagementTestsEnabled } = getProvidedContext ( )
517
501
518
502
if ( isTestManagementTestsEnabled ) {
@@ -913,8 +897,6 @@ addHook({
913
897
914
898
testSuiteFinishCh . publish ( { status : testSuiteResult . state , onFinish, ...testSuiteCtx . currentStore } )
915
899
916
- // console.log('test usite finish!')
917
- // TODO: fix too frequent flushes
918
900
await onFinishPromise
919
901
920
902
return startTestsResponse
0 commit comments