Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial mocha tests #26

Merged
merged 3 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';



/**
* Our 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;
});
});
});