Skip to content

Commit 628e90a

Browse files
committed
[cucumber#1044] Refactoring out nextPickleIndex + README example as test
1 parent 381d5aa commit 628e90a

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

features/parallel_custom_assign.feature

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,57 @@ Feature: Running scenarios in parallel with custom assignment
7878
And the output contains the text:
7979
"""
8080
#1 was test 2 on this worker
81-
"""
81+
"""
82+
83+
Scenario: assignment is appropriately applied and fails at last processed scenario 'a'
84+
Given a file named "features/step_definitions/cucumber_steps.js" with:
85+
"""
86+
const {Given, setParallelCanAssign} = require('@cucumber/cucumber')
87+
const _ = require('lodash')
88+
const {expect} = require('chai')
89+
let worker = null;
90+
let processed = 0;
91+
setParallelCanAssign((pickleInQuestion, picklesInProgress) => _.isEmpty(pickleInQuestion.tags)
92+
|| _.every(picklesInProgress, ({tags}) => _.isEmpty(tags) || tags[0].name !== pickleInQuestion.tags[0].name))
93+
94+
function step_def(name, delay) {
95+
return function(scenario, cb) {
96+
if (worker == null) worker = name;
97+
expect(worker).to.eq(name)
98+
expect(scenario).to.eq(++processed)
99+
setTimeout(cb, delay)
100+
}
101+
}
102+
103+
Given(/^scenario complex (\d+)$/, step_def('A', 300))
104+
Given(/^scenario simple (\d+)$/, step_def('B', 200))
105+
"""
106+
And a file named "features/a.feature" with:
107+
"""
108+
Feature: adheres to setParallelCanAssign handler
109+
@complex
110+
Scenario: 1
111+
Given scenario complex 1
112+
113+
@complex
114+
Scenario: 2
115+
Given scenario complex 2
116+
117+
@complex
118+
Scenario: 3
119+
Given scenario complex 3
120+
121+
@simple
122+
Scenario: 4
123+
Given scenario simple 1
124+
125+
@simple
126+
Scenario: 5
127+
Given scenario simple 2
128+
129+
@simple
130+
Scenario: 6
131+
Given scenario simple 3
132+
"""
133+
When I run cucumber-js with `--parallel 2`
134+
Then it passes

src/runtime/parallel/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ where the first tag doesn't match the first tag of any in progress tests.
1010
import { setParallelCanAssign } from '@cucumber/cucumber'
1111
// Accept tests missing tags or no test is running having the same first tag
1212
setParallelCanAssign((pickleInQuestion, picklesInProgress) => _.isEmpty(pickleInQuestion.tags)
13-
|| _.every(picklesInProgress, ({tags}) => _.isEmpty(tags) || tags[0].name !== picklesInProgress.tags[0].name))
13+
|| _.every(picklesInProgress, ({tags}) => _.isEmpty(tags) || tags[0].name !== pickleInQuestion.tags[0].name))
1414
```
1515
* Example using the handler above
1616
* 2 workers, `A` and `B`

src/runtime/parallel/coordinator.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export default class Coordinator {
5757
private readonly eventDataCollector: EventDataCollector
5858
private readonly stopwatch: ITestRunStopwatch
5959
private onFinish: (success: boolean) => void
60-
private nextPickleIdIndex: number
6160
private readonly options: IRuntimeOptions
6261
private readonly pickleIds: string[]
6362
private inProgressPickles: Dictionary<messages.IPickle>
@@ -90,7 +89,6 @@ export default class Coordinator {
9089
this.supportCodePaths = supportCodePaths
9190
this.supportCodeRequiredModules = supportCodeRequiredModules
9291
this.pickleIds = pickleIds
93-
this.nextPickleIdIndex = 0
9492
this.success = true
9593
this.workers = {}
9694
this.inProgressPickles = {}
@@ -169,10 +167,7 @@ export default class Coordinator {
169167
return worker.state !== WorkerState.idle
170168
})
171169

172-
if (
173-
_.isEmpty(this.inProgressPickles) &&
174-
this.pickleIds.length > this.nextPickleIdIndex
175-
) {
170+
if (_.isEmpty(this.inProgressPickles) && this.pickleIds.length > 0) {
176171
this.giveWork(workers[0], true)
177172
this.idleInterventions++
178173
}
@@ -262,54 +257,47 @@ export default class Coordinator {
262257
}
263258

264259
nextPicklePlacement(): IPicklePlacement {
265-
for (
266-
let index = this.nextPickleIdIndex;
267-
index < this.pickleIds.length;
268-
index++
269-
) {
270-
const pickle = this.eventDataCollector.getPickle(this.pickleIds[index])
260+
for (let index = 0; index < this.pickleIds.length; index++) {
261+
const placement = this.placementAt(index)
271262
if (
272263
this.supportCodeLibrary.parallelCanAssign(
273-
pickle,
264+
placement.pickle,
274265
_.values(this.inProgressPickles)
275266
)
276267
) {
277-
return { index, pickle }
268+
return placement
278269
}
279270
}
280271

281272
return null
282273
}
283274

275+
placementAt(index: number): IPicklePlacement {
276+
return {
277+
index,
278+
pickle: this.eventDataCollector.getPickle(this.pickleIds[index]),
279+
}
280+
}
281+
284282
giveWork(worker: IWorker, force: boolean = false): void {
285-
if (this.nextPickleIdIndex >= this.pickleIds.length) {
283+
if (this.pickleIds.length < 1) {
286284
const finalizeCommand: IWorkerCommand = { finalize: true }
287285
worker.state = WorkerState.running
288286
worker.process.send(finalizeCommand)
289287
return
290288
}
291289

292290
const picklePlacement = force
293-
? {
294-
index: this.nextPickleIdIndex,
295-
pickle: this.eventDataCollector.getPickle(
296-
this.pickleIds[this.nextPickleIdIndex]
297-
),
298-
}
291+
? this.placementAt(0)
299292
: this.nextPicklePlacement()
293+
300294
if (picklePlacement === null) {
301295
return
302296
}
303297

304-
if (this.nextPickleIdIndex !== picklePlacement.index) {
305-
this.pickleIds.splice(
306-
picklePlacement.index,
307-
0,
308-
this.pickleIds.splice(this.nextPickleIdIndex, 1)[0]
309-
)
310-
}
311-
this.nextPickleIdIndex += 1
312-
const pickle = picklePlacement.pickle
298+
const { index: nextPickleIndex, pickle } = picklePlacement
299+
300+
this.pickleIds.splice(nextPickleIndex, 1)
313301
this.inProgressPickles[worker.id] = pickle
314302
const gherkinDocument = this.eventDataCollector.getGherkinDocument(
315303
pickle.uri

0 commit comments

Comments
 (0)