|
| 1 | +import _ from 'lodash' |
| 2 | +import childProcess from 'child_process' |
| 3 | +import commandTypes from './command_types' |
| 4 | +import path from 'path' |
| 5 | +import readline from 'readline' |
| 6 | +import Status from '../../status' |
| 7 | + |
| 8 | +const slaveCommand = path.resolve( |
| 9 | + __dirname, |
| 10 | + '..', |
| 11 | + '..', |
| 12 | + '..', |
| 13 | + 'bin', |
| 14 | + 'run_slave' |
| 15 | +) |
| 16 | + |
| 17 | +export default class Master { |
| 18 | + // options - {dryRun, failFast, filterStacktraces, strict} |
| 19 | + constructor({ |
| 20 | + eventBroadcaster, |
| 21 | + options, |
| 22 | + supportCodePaths, |
| 23 | + supportCodeRequiredModules, |
| 24 | + testCases |
| 25 | + }) { |
| 26 | + this.eventBroadcaster = eventBroadcaster |
| 27 | + this.options = options || {} |
| 28 | + this.supportCodePaths = supportCodePaths |
| 29 | + this.supportCodeRequiredModules = supportCodeRequiredModules |
| 30 | + this.testCases = testCases || [] |
| 31 | + this.nextTestCaseIndex = 0 |
| 32 | + this.testCasesCompleted = 0 |
| 33 | + this.result = { |
| 34 | + duration: 0, |
| 35 | + success: true |
| 36 | + } |
| 37 | + this.slaves = {} |
| 38 | + } |
| 39 | + |
| 40 | + parseSlaveLine(slave, line) { |
| 41 | + const input = JSON.parse(line) |
| 42 | + switch (input.command) { |
| 43 | + case commandTypes.READY: |
| 44 | + this.giveSlaveWork(slave) |
| 45 | + break |
| 46 | + case commandTypes.EVENT: |
| 47 | + this.eventBroadcaster.emit(input.name, input.data) |
| 48 | + if (input.name === 'test-case-finished') { |
| 49 | + this.parseTestCaseResult(input.data.result) |
| 50 | + } |
| 51 | + break |
| 52 | + default: |
| 53 | + throw new Error(`Unexpected message from slave: ${line}`) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + startSlave(id, total) { |
| 58 | + const slaveProcess = childProcess.spawn(slaveCommand, [], { |
| 59 | + env: _.assign({}, process.env, { |
| 60 | + CUCUMBER_PARALLEL: 'true', |
| 61 | + CUCUMBER_TOTAL_SLAVES: total, |
| 62 | + CUCUMBER_SLAVE_ID: id |
| 63 | + }), |
| 64 | + stdio: ['pipe', 'pipe', process.stderr] |
| 65 | + }) |
| 66 | + const rl = readline.createInterface({ input: slaveProcess.stdout }) |
| 67 | + const slave = { process: slaveProcess } |
| 68 | + this.slaves[id] = slave |
| 69 | + rl.on('line', line => { |
| 70 | + this.parseSlaveLine(slave, line) |
| 71 | + }) |
| 72 | + rl.on('close', () => { |
| 73 | + slave.closed = true |
| 74 | + this.onSlaveClose() |
| 75 | + }) |
| 76 | + slave.process.stdin.write( |
| 77 | + JSON.stringify({ |
| 78 | + command: commandTypes.INITIALIZE, |
| 79 | + filterStacktraces: this.options.filterStacktraces, |
| 80 | + supportCodePaths: this.supportCodePaths, |
| 81 | + supportCodeRequiredModules: this.supportCodeRequiredModules, |
| 82 | + worldParameters: this.options.worldParameters |
| 83 | + }) + '\n' |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + onSlaveClose() { |
| 88 | + if (_.every(this.slaves, 'closed')) { |
| 89 | + this.eventBroadcaster.emit('test-run-finished', { result: this.result }) |
| 90 | + this.onFinish(this.result.success) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + parseTestCaseResult(testCaseResult) { |
| 95 | + this.testCasesCompleted += 1 |
| 96 | + if (testCaseResult.duration) { |
| 97 | + this.result.duration += testCaseResult.duration |
| 98 | + } |
| 99 | + if (this.shouldCauseFailure(testCaseResult.status)) { |
| 100 | + this.result.success = false |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + run(numberOfSlaves, done) { |
| 105 | + this.eventBroadcaster.emit('test-run-started') |
| 106 | + _.times(numberOfSlaves, id => this.startSlave(id, numberOfSlaves)) |
| 107 | + this.onFinish = done |
| 108 | + } |
| 109 | + |
| 110 | + giveSlaveWork(slave) { |
| 111 | + if (this.nextTestCaseIndex === this.testCases.length) { |
| 112 | + slave.process.stdin.write( |
| 113 | + JSON.stringify({ command: commandTypes.FINALIZE }) + '\n' |
| 114 | + ) |
| 115 | + return |
| 116 | + } |
| 117 | + const testCase = this.testCases[this.nextTestCaseIndex] |
| 118 | + this.nextTestCaseIndex += 1 |
| 119 | + const skip = |
| 120 | + this.options.dryRun || (this.options.failFast && !this.result.success) |
| 121 | + slave.process.stdin.write( |
| 122 | + JSON.stringify({ command: commandTypes.RUN, skip, testCase }) + '\n' |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + shouldCauseFailure(status) { |
| 127 | + return ( |
| 128 | + _.includes([Status.AMBIGUOUS, Status.FAILED, Status.UNDEFINED], status) || |
| 129 | + (status === Status.PENDING && this.options.strict) |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
0 commit comments