All files / wdio-local-runner/src worker.js

98.31% Statements 58/59
84.62% Branches 22/26
100% Functions 8/8
98.31% Lines 58/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                  2x 2x                                       13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x             6x 6x   6x       6x 6x     6x 6x             6x 6x 6x     6x           6x       5x         5x 1x           5x 2x 1x   1x 1x 1x   2x           3x 1x     1x 1x   1x           2x       2x       1x 1x       1x         1x 1x   1x 1x 1x                   11x   11x 1x             10x 6x     10x 10x      
import path from 'path'
import child from 'child_process'
import EventEmitter from 'events'
 
import logger from '@wdio/logger'
 
import RunnerTransformStream from './transformStream'
import ReplQueue from './replQueue'
 
const log = logger('@wdio/local-runner')
const replQueue = new ReplQueue()
 
/**
 * WorkerInstance
 * responsible for spawning a sub process to run the framework in and handle its
 * session lifetime.
 */
export default class WorkerInstance extends EventEmitter {
    /**
     * assigns paramters to scope of instance
     * @param  {object}   config      parsed configuration object
     * @param  {string}   cid         capability id (e.g. 0-1)
     * @param  {string}   configFile  path to config file (for sub process to parse)
     * @param  {object}   caps        capability object
     * @param  {string[]} specs       list of paths to test files to run in this worker
     * @param  {object}   server      configuration details about automation backend this session is using
     * @param  {number}   retries     number of retries remaining
     * @param  {object}   execArgv    execution arguments for the test run
     */
    constructor (config, { cid, configFile, caps, specs, server, execArgv, retries }, stdout, stderr) {
        super()
        this.cid = cid
        this.config = config
        this.configFile = configFile
        this.caps = caps
        this.specs = specs
        this.server = server || {}
        this.execArgv = execArgv
        this.retries = retries
        this.isBusy = false
        this.stdout = stdout
        this.stderr = stderr
    }
 
    /**
     * spawns process to kick of wdio-runner
     */
    startProcess () {
        const { cid, execArgv } = this
        const argv = process.argv.slice(2)
 
        const runnerEnv = Object.assign(process.env, this.config.runnerEnv, {
            WDIO_WORKER: true
        })
 
        Eif (this.config.outputDir) {
            runnerEnv.WDIO_LOG_PATH = path.join(this.config.outputDir, `wdio-${cid}.log`)
        }
 
        log.info(`Start worker ${cid} with arg: ${argv}`)
        const childProcess = this.childProcess = child.fork(path.join(__dirname, 'run.js'), argv, {
            cwd: process.cwd(),
            env: runnerEnv,
            execArgv,
            silent: true
        })
 
        childProcess.on('message', ::this._handleMessage)
        childProcess.on('error', ::this._handleError)
        childProcess.on('exit', ::this._handleExit)
 
        /* istanbul ignore if */
        if (!process.env.JEST_WORKER_ID) {
            childProcess.stdout.pipe(new RunnerTransformStream(cid)).pipe(process.stdout)
            childProcess.stderr.pipe(new RunnerTransformStream(cid)).pipe(process.stderr)
            process.stdin.pipe(childProcess.stdin)
        }
 
        return childProcess
    }
 
    _handleMessage (payload) {
        const { cid, childProcess } = this
 
        /**
         * resolve pending commands
         */
        if (payload.name === 'finisedCommand') {
            this.isBusy = false
        }
 
        /**
         * store sessionId and connection data to worker instance
         */
        if (payload.name === 'sessionStarted') {
            if (payload.content.isMultiremote) {
                Object.assign(this, payload.content)
            } else {
                this.sessionId = payload.content.sessionId
                delete payload.content.sessionId
                Object.assign(this.server, payload.content)
            }
            return
        }
 
        /**
         * handle debug command called within worker process
         */
        if (payload.origin === 'debugger' && payload.name === 'start') {
            replQueue.add(
                childProcess,
                { prompt: `[${cid}] \u203A `, ...payload.params },
                () => this.emit('message', Object.assign(payload, { cid })),
                (ev) => this.emit('message', ev)
            )
            return replQueue.next()
        }
 
        /**
         * handle debugger results
         */
        Iif (replQueue.isRunning && payload.origin === 'debugger' && payload.name === 'result') {
            replQueue.runningRepl.onResult(payload.params)
        }
 
        this.emit('message', Object.assign(payload, { cid }))
    }
 
    _handleError (payload) {
        const { cid } = this
        this.emit('error', Object.assign(payload, { cid }))
    }
 
    _handleExit (exitCode) {
        const { cid, childProcess, specs, retries } = this
 
        /**
         * delete process of worker
         */
        delete this.childProcess
        this.isBusy = false
 
        log.debug(`Runner ${cid} finished with exit code ${exitCode}`)
        this.emit('exit', { cid, exitCode, specs, retries })
        childProcess.kill('SIGTERM')
    }
 
    /**
     * sends message to sub process to execute functions in wdio-runner
     * @param  {string} command  method to run in wdio-runner
     * @param  {object} argv     arguments for functions to call
     * @return null
     */
    postMessage (command, argv) {
        const { cid, configFile, caps, specs, server, retries, isBusy } = this
 
        if (isBusy && command !== 'endSession') {
            return log.info(`worker with cid ${cid} already busy and can't take new commands`)
        }
 
        /**
         * start up process if worker hasn't done yet or if child process
         * closes after running its job
         */
        if (!this.childProcess) {
            this.childProcess = this.startProcess()
        }
 
        this.childProcess.send({ cid, command, configFile, argv, caps, specs, server, retries })
        this.isBusy = true
    }
}