Stryker

IsolatedTestRunnerAdapter.js - Stryker report

Summary

File Based on all code Based on code coverage
IsolatedTestRunnerAdapter.js
98%
53/54 98% 53/54

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');
var MAX_WAIT_FOR_DISPOSE = 2000;
var TestRunnerChildProcessAdapter = (function () 0{
    function TestRunnerChildProcessAdapter(realTestRunnerName, options) 1{
        this.realTestRunnerName = realTestRunnerName;
        this.options = options;
        this.startWorker();
    }

    TestRunnerChildProcessAdapter.prototype.startWorker = function () 2{
        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:
                    if (1920!_this.isDisposing) 21{
                        _this.handleResultMessage(message);
                    }

                    break;
                case Message_1.MessageType.InitDone:
                    _this.initPromiseFulfillmentCallback();
                    break;
                case Message_1.MessageType.DisposeDone:
                    _this.disposePromiseFulfillmentCallback();
                    break;
                default:
                    log.error(22"Retrieved unrecognized message from child process: " + JSON.stringify(message));
                    break;
            }
        });
    };
    TestRunnerChildProcessAdapter.prototype.init = function () 23{
        var _this = this;
        this.initPromise = new Promise(function (resolve) 24{ return _this.initPromiseFulfillmentCallback = resolve; });
        this.sendInitCommand();
        return this.initPromise;
    };
    TestRunnerChildProcessAdapter.prototype.run = function (options) 25{
        var _this = this;
        this.clearCurrentTimer();
        if (2627options.timeout) 28{
            this.markNoResultTimeout(options.timeout);
        }

        this.runPromise = new Promise(function (resolve) 29{
            _this.runPromiseFulfillmentCallback = resolve;
            _this.sendRunCommand(options);
            _this.currentRunStartedTimestamp = new Date();
        });
        return this.runPromise;
    };
    TestRunnerChildProcessAdapter.prototype.dispose = function () 30{
        var _this = this;
        if (3132this.isDisposing) 33{
            return this.disposingPromise;
        }

        else 34{
            this.isDisposing = true;
            this.disposingPromise = new Promise(function (resolve) 35{ return _this.disposePromiseFulfillmentCallback = resolve; })
                .then(function () 36{
                clearTimeout(timer_1);
                _this.workerProcess.kill();
                _this.isDisposing = false;
            });
            this.clearCurrentTimer();
            this.sendDisposeCommand();
            var timer_1 = setTimeout(this.disposePromiseFulfillmentCallback, MAX_WAIT_FOR_DISPOSE);
            return this.disposingPromise;
        }

    };
    TestRunnerChildProcessAdapter.prototype.sendRunCommand = function (options) 37{
        var message = {
            type: Message_1.MessageType.Run,
            body: {
                runOptions: options
            }
        };
        this.workerProcess.send(message);
    };
    TestRunnerChildProcessAdapter.prototype.sendStartCommand = function () 38{
        var startMessage = {
            type: Message_1.MessageType.Start,
            body: {
                runnerName: this.realTestRunnerName,
                runnerOptions: this.options
            }
        };
        this.workerProcess.send(startMessage);
    };
    TestRunnerChildProcessAdapter.prototype.sendInitCommand = function () 39{
        this.workerProcess.send(this.emptyMessage(Message_1.MessageType.Init));
    };
    TestRunnerChildProcessAdapter.prototype.sendDisposeCommand = function () 40{
        this.workerProcess.send(this.emptyMessage(Message_1.MessageType.Dispose));
    };
    TestRunnerChildProcessAdapter.prototype.handleResultMessage = function (message) 41{
        message.body.result.timeSpent = (42new Date().getTime() - this.currentRunStartedTimestamp.getTime());
        this.runPromiseFulfillmentCallback(message.body.result);
    };
    TestRunnerChildProcessAdapter.prototype.clearCurrentTimer = function () 43{
        if (4445this.currentTimeoutTimer) 46{
            clearTimeout(this.currentTimeoutTimer);
        }

    };
    TestRunnerChildProcessAdapter.prototype.markNoResultTimeout = function (timeoutMs) 47{
        var _this = this;
        this.currentTimeoutTimer = setTimeout(function () 48{
            _this.handleTimeout();
        }, timeoutMs);
    };
    TestRunnerChildProcessAdapter.prototype.handleTimeout = function () 49{
        var _this = this;
        this.dispose()
            .then(function () 50{ return _this.startWorker(); })
            .then(function () 51{ return _this.init(); })
            .then(function () 52{ return _this.runPromiseFulfillmentCallback({ result: test_runner_1.TestResult.Timeout }); });
    };
    TestRunnerChildProcessAdapter.prototype.emptyMessage = function (type) 53{
        return { type: type };
    };
    return TestRunnerChildProcessAdapter;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TestRunnerChildProcessAdapter;
//# sourceMappingURL=IsolatedTestRunnerAdapter.js.map