Skip to content

Commit

Permalink
Initial tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
valera-rozuvan authored and imor committed May 18, 2018
1 parent 8a2b17f commit d878047
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 8 deletions.
13 changes: 13 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function(config) {
config.set({
frameworks: [
'mocha'
],
plugins: [
'karma-mocha'
],
files: [
'test/*.js'
]
});
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
},
"license": "MIT",
"engines": {
"node": ">=0.10"
"node": ">=4.0"
},
"devDependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"grunt": "^0.4.5",
"grunt-cafe-mocha": "^0.1.13",
"grunt-contrib-jshint": "^1.1.0",
"grunt-groc": "^0.7.1"
"grunt-groc": "^0.7.1",
"mocha": "^5.1.1",
"mock-spawn": "^0.2.6"
},
"scripts": {
"test": "mocha"
}
}
21 changes: 15 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,29 @@ util.inherits(Engine, events.EventEmitter);
Engine.prototype.runProcess = function () {
var self = this;
var deferred = Q.defer();
this.engineProcess = spawn(this.engineFile);
var numberOfTriesBeforeTimeout = 10;
var currentIntervalNumber = 0
var timer;

this.engineProcess = spawn(this.engineFile);

this.engineProcess.once('error', function (error) {
clearInterval(timer);
deferred.reject(error);
});

timer = setInterval(function () {
currentIntervalNumber += 1;

if (utilities.isProcessRunning(self.engineProcess)) {
clearInterval(timer);
deferred.resolve();
} else if (currentIntervalNumber === numberOfTriesBeforeTimeout) {
clearInterval(timer);
deferred.reject(new Error('timeout while starting process'));
}

}, 100);

return deferred.promise;
};

Expand Down Expand Up @@ -189,7 +198,7 @@ Engine.prototype.timeLimitedGoCommand = function (infoHandler,
var self = this;
var deferred = Q.defer();
var pendingData = "";

var engineStdoutListener = function (data) {
var lines = utilities.getLines(pendingData+data);
pendingData = lines.incompleteLine ? lines.incompleteLine : "";
Expand Down Expand Up @@ -231,7 +240,7 @@ Engine.prototype.timeLimitedGoCommand = function (infoHandler,
//called for each info line output by the engine.
Engine.prototype.goCommand = function (commands, infoHandler) {
var pendingData = "";

var engineStdoutListener = function (data) {
var lines = utilities.getLines(pendingData+data);
pendingData = lines.incompleteLine ? lines.incompleteLine : "";
Expand Down Expand Up @@ -280,7 +289,7 @@ Engine.prototype.goCommand = function (commands, infoHandler) {
//called for each info line output by the engine.
Engine.prototype.goInfiniteCommand = function (infoHandler) {
var pendingData = "";

var engineStdoutListener = function (data) {
var lines = utilities.getLines(pendingData+data);
pendingData = lines.incompleteLine ? lines.incompleteLine : "";
Expand Down Expand Up @@ -308,7 +317,7 @@ Engine.prototype.stopCommand = function () {
var self = this;
var deferred = Q.defer();
var pendingData = "";

var engineStdoutListener = function (data) {
var lines = utilities.getLines(pendingData+data);
pendingData = lines.incompleteLine ? lines.incompleteLine : "";
Expand Down
106 changes: 106 additions & 0 deletions test/engine-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Setup testing with `chai`.
*/

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();
var expect = chai.expect;



/**
* Setup `child_process.spawn` mocks.
*/
var mockSpawn = require('mock-spawn');
var mySpawn = mockSpawn();
require('child_process').spawn = mySpawn;

// First spawn -> emit error, exit with status (1).
mySpawn.sequence.add(function (cb) {
this.emit('error', new Error('spawn ' + badEnginePath + ' ENOENT'));
setTimeout(function() { return cb(1); }, 10);
});
// Second spawn -> slow start of application. Simulate timeout error.
mySpawn.sequence.add(function (cb) {
var self = this;

setTimeout(function () {
self.stdout.write('some output data');

return cb(0);
}, 1500);
});
// Third spawn -> normal application start.
mySpawn.sequence.add(function (cb) {
var self = this;

setTimeout(function () {
self.stdout.write('some output data');

return cb(0);
}, 100);
});



/**
* Some predefined constants to be used in tests.
*/

var badEnginePath = 'non_existent_engine';
var testEcecutableFile = 'test_executable';



/**
* Out subject-under-test.
*/

var Engine = require('../src/main');



/**
* Now the tests start :)
*/

describe('Engine', function () {
describe('contructor', function () {
it('throws when no engine path is provided', function () {
return expect(function () {
new Engine();
}).to.throw('Path must be a string. Received undefined')
});

it('does not throw on bad engine path', function () {
return expect(function () {
new Engine('non_existent_engine');
}).to.not.throw()
});
});

describe('runProcess', function () {
it('throws when tries to launch non existent path', function () {
var engine = new Engine(badEnginePath);
var promise = engine.runProcess();

return promise.should.be.rejectedWith('spawn ' + badEnginePath + ' ENOENT');
});

it('throws timeout error if executable does not start within allowed timeout period', function () {
var engine = new Engine(testEcecutableFile);
var promise = engine.runProcess();

return promise.should.be.rejectedWith('timeout while starting process');
});

it('does not throw if executable launched OK', function () {
var engine = new Engine(testEcecutableFile);
var promise = engine.runProcess();

return promise.should.not.be.rejected;
});
});
});

0 comments on commit d878047

Please sign in to comment.