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

94.29% Statements 33/35
91.67% Branches 11/12
100% Functions 8/8
94.29% Lines 33/35

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            1x       6x 6x 6x 6x 6x                 6x             6x 6x         6x 6x 6x   6x                   4x   4x 5x 5x           5x 2x 3x 1x 1x     4x     4x 4x 8x 10x   8x 8x 4x 4x 4x       4x        
import logger from '@wdio/logger'
import { WritableStreamBuffer } from 'stream-buffers'
 
import WorkerInstance from './worker'
import { SHUTDOWN_TIMEOUT, BUFFER_OPTIONS } from './constants'
 
const log = logger('@wdio/local-runner')
 
export default class LocalRunner {
    constructor (configFile, config) {
        this.configFile = configFile
        this.config = config
        this.workerPool = {}
        this.stdout = new WritableStreamBuffer(BUFFER_OPTIONS)
        this.stderr = new WritableStreamBuffer(BUFFER_OPTIONS)
    }
 
    /**
     * nothing to initialise when running locally
     */
    initialise () {}
 
    getWorkerCount () {
        return Object.keys(this.workerPool).length
    }
 
    run ({ command, argv, ...options }) {
        /**
         * adjust max listeners on stdout/stderr when creating listeners
         */
        const workerCnt = this.getWorkerCount()
        Iif (workerCnt >= process.stdout.getMaxListeners() - 2) {
            process.stdout.setMaxListeners(workerCnt + 2)
            process.stderr.setMaxListeners(workerCnt + 2)
        }
 
        const worker = new WorkerInstance(this.config, options, this.stdout, this.stderr)
        this.workerPool[options.cid] = worker
        worker.postMessage(command, argv)
 
        return worker
    }
 
    /**
     * shutdown all worker processes
     *
     * @return {Promise}  resolves when all worker have been shutdown or
     *                    a timeout was reached
     */
    shutdown () {
        log.info('Shutting down spawned worker')
 
        for (const [cid, worker] of Object.entries(this.workerPool)) {
            const { caps, server, sessionId, config, isMultiremote, instances } = worker
            let payload = {}
 
            /**
             * put connection information to payload if in watch mode
             * in order to attach to browser session and kill it
             */
            if (config && config.watch && (sessionId || isMultiremote)) {
                payload = { config: { ...server, sessionId }, caps, watch: true, isMultiremote, instances }
            } else if (!worker.isBusy) {
                delete this.workerPool[cid]
                continue
            }
 
            worker.postMessage('endSession', payload)
        }
 
        return new Promise((resolve) => {
            const interval = setInterval(() => {
                const busyWorker = Object.entries(this.workerPool)
                    .filter(([, worker]) => worker.isBusy).length
 
                log.info(`Waiting for ${busyWorker} to shut down gracefully`)
                if (busyWorker === 0) {
                    clearInterval(interval)
                    log.info('shutting down')
                    return resolve()
                }
            }, 250)
 
            setTimeout(resolve, SHUTDOWN_TIMEOUT)
        })
    }
}