Skip to content

Commit

Permalink
better org
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Apr 25, 2024
1 parent 7a7ef61 commit d325a31
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 54 deletions.
6 changes: 3 additions & 3 deletions integration-tests/cucumber/cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { exec } = require('child_process')

const getPort = require('get-port')
const semver = require('semver')
// const semver = require('semver')
const { assert } = require('chai')

const {
Expand Down Expand Up @@ -34,10 +34,10 @@ const {
CUCUMBER_IS_PARALLEL
} = require('../../packages/dd-trace/src/plugins/util/test')

const isOldNode = semver.satisfies(process.version, '<=16')
// const isOldNode = semver.satisfies(process.version, '<=16')
// const versions = ['7.0.0', isOldNode ? '9' : 'latest']

// does not work with '7.0.0' yet
// TODO: fix 7.0.0 or set another limit
const versions = ['latest']

const moduleType = [
Expand Down
109 changes: 58 additions & 51 deletions packages/datadog-instrumentations/src/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,58 @@ function getWrappedGiveWork (giveWorkFunction) {
}
}

function getWrappedParseWorkerMessage (parseWorkerMessageFunction) {
return function (worker, message) {
// If the message is an array, it's a dd-trace message, so we need to stop cucumber processing
// TODO: identify the message better
if (Array.isArray(message)) {
const [messageCode, payload] = message
if (messageCode === JEST_WORKER_TRACE_PAYLOAD_CODE) {
sessionAsyncResource.runInAsyncScope(() => {
workerReportTraceCh.publish(payload)
})
}
return
}

const { jsonEnvelope } = message
if (!jsonEnvelope) {
return parseWorkerMessageFunction.apply(this, arguments)
}
let parsed = jsonEnvelope

if (typeof parsed === 'string') {
try {
parsed = JSON.parse(jsonEnvelope)
} catch (e) {
// ignore errors and continue
return parseWorkerMessageFunction.apply(this, arguments)
}
}

if (parsed.testCaseFinished) {
// we can grab worstTestStepResult in addition to pickle to get the actual status
const { pickle } =
this.eventDataCollector.getTestCaseAttempt(parsed.testCaseFinished.testCaseStartedId)

const testFileAbsolutePath = pickle.uri

// TODO: GET ACTUAL STATUS from worstTestStepResult
pickleResultByFile[testFileAbsolutePath].finished.push('pass')

if (pickleResultByFile[testFileAbsolutePath].finished.length === pickleByFile[testFileAbsolutePath].length) {
const testSuiteStatus = getSuiteStatusFromTestStatuses(pickleResultByFile[testFileAbsolutePath].finished)
testSuiteFinishCh.publish({
status: testSuiteStatus,
testSuitePath: getTestSuitePath(testFileAbsolutePath, process.cwd())
})
}
}

return parseWorkerMessageFunction.apply(this, arguments)
}
}

// Test start / finish for older versions. The only hook executed in workers when in parallel mode
addHook({
name: '@cucumber/cucumber',
Expand Down Expand Up @@ -501,68 +553,23 @@ addHook({
// Only executed in parallel mode.
// `getWrappedStart` generates session start and finish events
// `getWrappedGiveWork` generates suite start events and sets pickleResultByFile (used by suite finish events)
// `getWrappedParseWorkerMessage` generates suite finish events
addHook({
name: '@cucumber/cucumber',
versions: ['>=7.3.0'],
file: 'lib/runtime/parallel/coordinator.js'
}, (coordinatorPackage, frameworkVersion) => {
shimmer.wrap(coordinatorPackage.default.prototype, 'start', start => getWrappedStart(start, frameworkVersion, true))
shimmer.wrap(coordinatorPackage.default.prototype, 'giveWork', giveWork => getWrappedGiveWork(giveWork))

shimmer.wrap(coordinatorPackage.default.prototype, 'parseWorkerMessage', parseWorkerMessage =>
function (worker, message) {
// If the message is an array, it's a dd-trace message, so we need to stop cucumber processing
// TODO: identify the message better
if (Array.isArray(message)) {
const [messageCode, payload] = message
if (messageCode === JEST_WORKER_TRACE_PAYLOAD_CODE) {
sessionAsyncResource.runInAsyncScope(() => {
workerReportTraceCh.publish(payload)
})
}
return
}

const { jsonEnvelope } = message
if (!jsonEnvelope) {
return parseWorkerMessage.apply(this, arguments)
}
let parsed = jsonEnvelope

if (typeof parsed === 'string') {
try {
parsed = JSON.parse(jsonEnvelope)
} catch (e) {
// ignore errors and continue
return parseWorkerMessage.apply(this, arguments)
}
}

if (parsed.testCaseFinished) {
// we can grab worstTestStepResult in addition to pickle to get the actual status
const { pickle } =
this.eventDataCollector.getTestCaseAttempt(parsed.testCaseFinished.testCaseStartedId)

const testFileAbsolutePath = pickle.uri

// TODO: GET ACTUAL STATUS from worstTestStepResult
pickleResultByFile[testFileAbsolutePath].finished.push('pass')

if (pickleResultByFile[testFileAbsolutePath].finished.length === pickleByFile[testFileAbsolutePath].length) {
const testSuiteStatus = getSuiteStatusFromTestStatuses(pickleResultByFile[testFileAbsolutePath].finished)
testSuiteFinishCh.publish({
status: testSuiteStatus,
testSuitePath: getTestSuitePath(testFileAbsolutePath, process.cwd())
})
}
}

return parseWorkerMessage.apply(this, arguments)
}
shimmer.wrap(
coordinatorPackage.default.prototype,
'parseWorkerMessage',
parseWorkerMessage => getWrappedParseWorkerMessage(parseWorkerMessage)
)
return coordinatorPackage
})

// TODO: remove unnecesary hooks
// In `giveWork` I can modify the data that is sent to the workers it shows up in `runTestCase`.
// This might be useful for "interprocess tracing".
addHook({
Expand Down

0 comments on commit d325a31

Please sign in to comment.