Skip to content

Commit

Permalink
[cucumber#1044] Using array instead of iterable
Browse files Browse the repository at this point in the history
adding id to IWorker for easy lookup of in progress pickles
  • Loading branch information
eman2673 committed Feb 25, 2021
1 parent 6774382 commit a4de83b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 66 deletions.
80 changes: 27 additions & 53 deletions features/parallel_custom_assign.feature
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
Feature: Running scenarios in parallel

Scenario: running in parallel can improve speed if there are async operations
Given a file named "features/step_definitions/cucumber_steps.js" with:
"""
const {Given, setParallelCanAssign} = require('@cucumber/cucumber')
const Promise = require('bluebird')
setParallelCanAssign(() => true)
Given(/^a slow step$/, function(callback) {
setTimeout(callback, 1000)
})
"""
And a file named "features/a.feature" with:
"""
Feature: slow
Scenario: a
Given a slow step
Scenario: b
Given a slow step
"""
When I run cucumber-js with `--parallel 2`
Then it passes

Scenario: invalid parallel assignment handler fails the test
Given a file named "features/step_definitions/cucumber_steps.js" with:
"""
const {Given, setParallelCanAssign} = require('@cucumber/cucumber')
const {Given, Then, setParallelCanAssign} = require('@cucumber/cucumber')
const Promise = require('bluebird')
setParallelCanAssign(() => false)
Given(/^a step$/, function() { })
Expand All @@ -48,8 +25,9 @@ Feature: Running scenarios in parallel
"""
const {Given, setParallelCanAssign} = require('@cucumber/cucumber')
const Promise = require('bluebird')
let flag = true
setParallelCanAssign(() => true)
//Given(/^a step$/, function() { })
Given(/^a step$/, function() { })
"""
And a file named "features/a.feature" with:
"""
Expand All @@ -62,44 +40,40 @@ Feature: Running scenarios in parallel
"""
When I run cucumber-js with `--parallel 2`
Then it passes
# Then it fails
# And it outputs the text:
# """
# Please help me
# """

@spawn
Scenario: an error in BeforeAll fails the test
Scenario: assignment is appropriately applied and fails processing scenario a last
Given a file named "features/step_definitions/cucumber_steps.js" with:
"""
const {BeforeAll, Given} = require('@cucumber/cucumber')
const {Given, Then, setParallelCanAssign} = require('@cucumber/cucumber')
const Promise = require('bluebird')
Given(/^a slow step$/, function(callback) {
setTimeout(callback, 1000)
})
BeforeAll(function() {
throw new Error('my error')
let flag = true
const order = []
setParallelCanAssign(() => (flag = !flag))
Given(/^step (\d+)$/, function(step, cb) {
order.push(step)
setTimeout(cb, 250)
if (step === 1) throw Error(`#${step} this guy should be last`)
})
Then(/^log order$/, function() { console.log(order) })
"""
And a file named "features/a.feature" with:
"""
Feature: slow
Scenario: a
Given a slow step
Given step 1
Then log order
Scenario: b
Given step 2
Then log order
Scenario: c
Given step 3
Then log order
"""
When I run cucumber-js with `--parallel 2`
And the error output contains the text:
"""
BeforeAll hook errored on worker 0, process exiting:
"""
And the error output contains the text:
"""
BeforeAll hook errored on worker 1, process exiting:
"""
And the error output contains the text:
"""
my error
"""
Then it fails
And the output contains the text:
"""
#1 this guy should be last
"""
17 changes: 8 additions & 9 deletions src/runtime/parallel/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface IWorker {
closed: boolean
idle: boolean
process: ChildProcess
id: string
}

interface IPicklePlacement {
Expand All @@ -53,7 +54,7 @@ export default class Coordinator {
private nextPickleIdIndex: number
private readonly options: IRuntimeOptions
private readonly pickleIds: string[]
private readonly pickleWIP: Map<IWorker, messages.IPickle>
private inProgressPickles: Dictionary<messages.IPickle>
private workers: Dictionary<IWorker>
private supportCodeIdMap: Dictionary<string>
private readonly supportCodeLibrary: ISupportCodeLibrary
Expand Down Expand Up @@ -85,7 +86,7 @@ export default class Coordinator {
this.nextPickleIdIndex = 0
this.success = true
this.workers = {}
this.pickleWIP = new Map()
this.inProgressPickles = {}
this.supportCodeIdMap = {}
}

Expand All @@ -104,6 +105,7 @@ export default class Coordinator {
}
if (doesHaveValue(envelope.testCaseFinished)) {
worker.idle = true
this.inProgressPickles = _.omit(this.inProgressPickles, worker.id)
this.parseTestCaseResult(envelope.testCaseFinished)
}
} else {
Expand Down Expand Up @@ -159,10 +161,7 @@ export default class Coordinator {
return (oneWokeWorker = oneWokeWorker || !worker.idle)
})

if (
!oneWokeWorker &&
_.values(this.workers).every((worker) => worker.idle)
) {
if (!oneWokeWorker && _.isEmpty(this.inProgressPickles)) {
_.values(this.workers).forEach((worker) => {
if (!worker.closed) {
this.closeWorker(worker)
Expand All @@ -185,7 +184,7 @@ export default class Coordinator {
}),
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
})
const worker = { closed: false, idle: true, process: workerProcess }
const worker = { closed: false, idle: true, process: workerProcess, id }
this.workers[id] = worker
worker.process.on('message', (message: ICoordinatorReport) => {
this.parseWorkerMessage(worker, message)
Expand Down Expand Up @@ -261,7 +260,7 @@ export default class Coordinator {
if (
this.supportCodeLibrary.parallelCanAssign(
pickle,
this.pickleWIP.values()
_.values(this.inProgressPickles)
)
) {
return { index, pickle }
Expand Down Expand Up @@ -296,7 +295,7 @@ export default class Coordinator {
}
this.nextPickleIdIndex += 1
const pickle = picklePlacement.pickle
this.pickleWIP.set(worker, pickle)
this.inProgressPickles[worker.id] = pickle
const gherkinDocument = this.eventDataCollector.getGherkinDocument(
pickle.uri
)
Expand Down
4 changes: 2 additions & 2 deletions src/support_code_library_builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class SupportCodeLibraryBuilder {
private World: any
private parallelCanAssign: (
pickle: messages.IPickle,
runningPickles: IterableIterator<messages.IPickle>
runningPickles: messages.IPickle[]
) => boolean

constructor() {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class SupportCodeLibraryBuilder {
setParallelCanAssign: (
fn: (
pickle: messages.IPickle,
runningPickles: IterableIterator<messages.IPickle>
runningPickles: messages.IPickle[]
) => boolean
): void => {
this.parallelCanAssign = fn
Expand Down
4 changes: 2 additions & 2 deletions src/support_code_library_builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface IDefineSupportCodeMethods {
setParallelCanAssign: (
fn: (
pickle: messages.IPickle,
runningPickles: IterableIterator<messages.IPickle>
runningPickles: messages.IPickle[]
) => boolean
) => void
setWorldConstructor: (fn: any) => void
Expand Down Expand Up @@ -132,6 +132,6 @@ export interface ISupportCodeLibrary {
readonly World: any
readonly parallelCanAssign: (
pickle: messages.IPickle,
runningPickles: IterableIterator<messages.IPickle>
runningPickles: messages.IPickle[]
) => Boolean
}

0 comments on commit a4de83b

Please sign in to comment.