Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an additional argument setParallelCanAssign #2232

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ setParallelCanAssign(function (pickleInQuestion, picklesInProgress) {
})
```

Additionally, you can make use of the third argument to get information regarding the worker and decide wether or not to run a scenario.

Example:
```javascript
setParallelCanAssign(function (pickleInQuestion, picklesInProgress, worker) {
const inQuestionTags = pickleInQuestion.tags.map(tag => tag.name);
// Does the scenario have a tag that matches this worker (E.g. @WORKER_0)?
return inQuestionTags.includes(`WORKER_${worker.id}`);
})
```

For convenience, the following helpers exist to build a `canAssignFn`:

```javascript
Expand Down
11 changes: 6 additions & 5 deletions src/runtime/parallel/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export interface INewCoordinatorOptions {
numberOfWorkers: number
}

const enum WorkerState {
export const enum WorkerState {
'idle',
'closed',
'running',
'new',
}

interface IWorker {
export interface IWorker {
state: WorkerState
process: ChildProcess
id: string
Expand Down Expand Up @@ -246,13 +246,14 @@ export default class Coordinator implements IRuntime {
})
}

nextPicklePlacement(): IPicklePlacement {
nextPicklePlacement(worker: IWorker): IPicklePlacement {
for (let index = 0; index < this.pickleIds.length; index++) {
const placement = this.placementAt(index)
if (
this.supportCodeLibrary.parallelCanAssign(
placement.pickle,
Object.values(this.inProgressPickles)
Object.values(this.inProgressPickles),
worker
)
) {
return placement
Expand All @@ -279,7 +280,7 @@ export default class Coordinator implements IRuntime {

const picklePlacement = force
? this.placementAt(0)
: this.nextPicklePlacement()
: this.nextPicklePlacement(worker)

if (picklePlacement === null) {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { atMostOnePicklePerTag } from './parallel_can_assign_helpers'
import * as messages from '@cucumber/messages'
import { IWorker, WorkerState } from '../runtime/parallel/coordinator'
import { expect } from 'chai'

const worker: IWorker = { state: WorkerState.new, process: null, id: '1' }

function pickleWithTags(tagNames: string[]): messages.Pickle {
return {
id: 'test',
Expand All @@ -24,7 +27,7 @@ describe('parallel can assign helpers', () => {
const inProgress: messages.Pickle[] = []

// Act
const result = testCanAssignFn(inQuestion, inProgress)
const result = testCanAssignFn(inQuestion, inProgress, worker)

// Assert
expect(result).to.eql(true)
Expand All @@ -39,7 +42,7 @@ describe('parallel can assign helpers', () => {
]

// Act
const result = testCanAssignFn(inQuestion, inProgress)
const result = testCanAssignFn(inQuestion, inProgress, worker)

// Assert
expect(result).to.eql(true)
Expand All @@ -51,7 +54,7 @@ describe('parallel can assign helpers', () => {
const inProgress: messages.Pickle[] = [pickleWithTags(['@simple'])]

// Act
const result = testCanAssignFn(inQuestion, inProgress)
const result = testCanAssignFn(inQuestion, inProgress, worker)

// Assert
expect(result).to.eql(true)
Expand All @@ -63,7 +66,7 @@ describe('parallel can assign helpers', () => {
const inProgress: messages.Pickle[] = [pickleWithTags(['@complex'])]

// Act
const result = testCanAssignFn(inQuestion, inProgress)
const result = testCanAssignFn(inQuestion, inProgress, worker)

// Assert
expect(result).to.eql(false)
Expand Down
4 changes: 3 additions & 1 deletion src/support_code_library_builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import TestRunHookDefinition from '../models/test_run_hook_definition'
import StepDefinition from '../models/step_definition'
import { ParameterTypeRegistry } from '@cucumber/cucumber-expressions'
import { IWorld } from './world'
import { IWorker } from '../runtime/parallel/coordinator'

export type DefineStepPattern = string | RegExp
export type ParallelAssignmentValidator = (
pickle: messages.Pickle,
runningPickles: messages.Pickle[]
runningPickles: messages.Pickle[],
worker: IWorker
) => boolean
export interface ITestCaseHookParameter {
gherkinDocument: messages.GherkinDocument
Expand Down