Stryker

IsolatedTestRunnerAdapter.js - Stryker report

Summary

File Based on all code Based on code coverage
IsolatedTestRunnerAdapter.js
100%
37/37 100% 37/37

Code

"use strict";
var test_runner_1 = require('stryker-api/test_runner');
var child_process_1 = require('child_process');
var Message_1 = require('./Message');
var _ = require('lodash');
var log4js = require('log4js');
var log = log4js.getLogger('IsolatedTestRunnerAdapter');
/**
 * Runs the given test runner in a child process and forwards reports about test results
 * Also implements timeout-mechanisme (on timeout, restart the child runner and report timeout)
 */
var TestRunnerChildProcessAdapter = (function () 0{
    function TestRunnerChildProcessAdapter(realTestRunnerName, options) 1{
        this.realTestRunnerName = realTestRunnerName;
        this.options = options;
        this.startWorker();
    }

    TestRunnerChildProcessAdapter.prototype.startWorker = function () 2{
        // Remove --debug-brk from process arguments. 
        // When debugging, it will try to reuse the same debug port, which will be taken by the main process.
        var execArgv = _.clone(process.execArgv);
        _.remove(execArgv, function (arg) 3{ return 4arg.substr(0, 11) === '--debug-brk'; });
        this.workerProcess = child_process_1.fork(5__dirname + "/IsolatedTestRunnerAdapterWorker", [], { silent: true, execArgv: execArgv });
        this.sendStartCommand();
        this.listenToWorkerProcess();
    };
    TestRunnerChildProcessAdapter.prototype.listenToWorkerProcess = function () 6{
        var _this = this;
        if (78this.workerProcess.stdout) 9{
            var traceEnabled_1 = log.isTraceEnabled();
            this.workerProcess.stdout.on('data', function (data) 10{
                if (1112traceEnabled_1) 13{
                    log.trace(data.toString());
                }

            });
        }

        if (1415this.workerProcess.stderr) 16{
            this.workerProcess.stderr.on('data', function (data) 17{
                log.error(data.toString());
            });
        }

        this.workerProcess.on('message', function (message) 18{
            _this.clearCurrentTimer();
            switch (message.type) {
                case Message_1.MessageType.Result:
                    _this.handleResultMessage(message);
                    break;
                default:
                    log.error(19"Retrieved unrecognized message from child process: " + JSON.stringify(message));
                    break;
            }
        });
    };
    TestRunnerChildProcessAdapter.prototype.run = function (options) 20{
        var _this = this;
        this.clearCurrentTimer();
        if (2122options.timeout) 23{
            this.markNoResultTimeout(options.timeout);
        }

        this.currentPromise = new Promise(function (resolve) 24{
            _this.currentPromiseFulfillmentCallback = resolve;
            _this.sendRunCommand(options);
            _this.currentRunStartedTimestamp = new Date();
        });
        return this.currentPromise;
    };
    TestRunnerChildProcessAdapter.prototype.dispose = function () 25{
        this.clearCurrentTimer();
        this.workerProcess.kill();
    };
    TestRunnerChildProcessAdapter.prototype.sendRunCommand = function (options) 26{
        var message = {
            type: Message_1.MessageType.Run,
            body: {
                runOptions: options
            }
        };
        this.workerProcess.send(message);
    };
    TestRunnerChildProcessAdapter.prototype.sendStartCommand = function () 27{
        var startMessage = {
            type: Message_1.MessageType.Start,
            body: {
                runnerName: this.realTestRunnerName,
                runnerOptions: this.options
            }
        };
        this.workerProcess.send(startMessage);
    };
    TestRunnerChildProcessAdapter.prototype.handleResultMessage = function (message) 28{
        message.body.result.timeSpent = (29new Date().getTime() - this.currentRunStartedTimestamp.getTime());
        this.currentPromiseFulfillmentCallback(message.body.result);
    };
    TestRunnerChildProcessAdapter.prototype.clearCurrentTimer = function () 30{
        if (3132this.currentTimeoutTimer) 33{
            clearTimeout(this.currentTimeoutTimer);
        }

    };
    TestRunnerChildProcessAdapter.prototype.markNoResultTimeout = function (timeoutMs) 34{
        var _this = this;
        this.currentTimeoutTimer = setTimeout(function () 35{
            _this.handleTimeout();
        }, timeoutMs);
    };
    TestRunnerChildProcessAdapter.prototype.handleTimeout = function () 36{
        this.workerProcess.kill();
        this.startWorker();
        this.currentPromiseFulfillmentCallback({ result: test_runner_1.TestResult.Timeout });
    };
    return TestRunnerChildProcessAdapter;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TestRunnerChildProcessAdapter;
//# sourceMappingURL=IsolatedTestRunnerAdapter.js.map