Skip to content

Commit

Permalink
#192 Setup for running tests with worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeijer committed Feb 26, 2015
1 parent 23de9e5 commit 8ce7988
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 11 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
"run-sequence": "^1.0.2",
"esprima": "=2.0.0",
"istanbul": ">=0.3.5",
"chance": ">=0.7.3"
"chance": ">=0.7.3",
"pkginfo": ">=0.3.0",
"rimraf": "^2.2.8",
"ssl-root-cas": "^1.1.7"
},
"main": "webgme",
"scripts": {
Expand Down
18 changes: 14 additions & 4 deletions src/middleware/executor/worker/node_worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*jshint node:true*/
var nodeRequire = require;

if (typeof define !== 'undefined') {
Expand Down Expand Up @@ -58,8 +59,18 @@ if (nodeRequire.main === module) {
path = nodeRequire('path'),
cas = nodeRequire('ssl-root-cas/latest'),
superagent = nodeRequire('superagent'),
configFileName = 'config.json',
workingDirectory = 'executor-temp',
https = nodeRequire('https');

// This is used for tests
if (process.argv.length > 2) {
configFileName = process.argv[2];
if (process.argv.length > 3) {
workingDirectory = process.argv[3];
}
}

cas.inject();
fs.readdirSync(__dirname).forEach(function (file) {
var filename = path.resolve(__dirname, file);
Expand Down Expand Up @@ -103,10 +114,10 @@ if (nodeRequire.main === module) {

function readConfig() {
var config = {
"http://localhost:8888": {}
'http://127.0.0.1:8888': {}
};
try {
var configJSON = fs.readFileSync('config.json', {
var configJSON = fs.readFileSync(configFileName, {
encoding: 'utf8'
});
config = JSON.parse(configJSON);
Expand Down Expand Up @@ -152,7 +163,6 @@ if (nodeRequire.main === module) {
}

var workingDirectoryCount = 0;
var workingDirectory = 'executor-temp';
var rimraf = nodeRequire('rimraf');
rimraf(workingDirectory, function (err) {
if (err) {
Expand All @@ -164,7 +174,7 @@ if (nodeRequire.main === module) {
}

readConfig();
fs.watch("config.json", function () {
fs.watch(configFileName, function () {
setTimeout(readConfig, 200);
}); // setTimeout: likely handle O_TRUNC of config.json (though `move config.json.tmp config.json` is preferred)
});
Expand Down
7 changes: 1 addition & 6 deletions test/middleware/executor/ExecutorClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,5 @@ describe('ExecutorClient', function () {
});
});
});
//it('getInfoByStatus SUCCESS should succeed', function (done) {
// executorClient.getInfoByStatus('SUCCESS', function(err, res) {
// should.equal(err, null);
// done();
// });
//});

});
117 changes: 117 additions & 0 deletions test/middleware/executor/worker/node_worker.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*globals require, describe, it, before, after, WebGMEGlobal, WebGME*/

/**
* @author pmeijer / https://github.com/pmeijer
*/

require('../../../_globals.js');

var requirejs = require('requirejs'),
fs = require('fs'),
childProcess = require('child_process'),
should = require('chai').should(),
ExecutorClient = requirejs('executor/ExecutorClient'),
executorClient,
server,
nodeWorkerProcess,
serverBaseUrl;

describe('ExecutorClient', function () {
'use strict';

before(function (done) {
// we have to set the config here
var config = WebGMEGlobal.getConfig(),
param = {},
workerConfig = {};
config.port = 9002;
config.authentication = false;
config.enableExecutor = true;

param.serverPort = config.port;

serverBaseUrl = 'http://127.0.0.1:' + config.port;
workerConfig[serverBaseUrl] = {};

server = WebGME.standaloneServer(config);

fs.writeFile('test-tmp/worker_config.json', JSON.stringify(workerConfig), function (err) {
if (err) {
done(err);
} else {

server.start(function () {
executorClient = new ExecutorClient(param);
nodeWorkerProcess = childProcess.spawn('node',
['node_worker.js', '../../../../test-tmp/worker_config.json', '../../../../test-tmp/executor-tmp'],
{cwd: 'src/middleware/executor/worker'});

nodeWorkerProcess.stdout.on('data', function (data) {
var str = data.toString();
if (str.indexOf('Connected to') > -1) {
done();
}
});
});
}
});
});

after(function (done) {
nodeWorkerProcess.kill('SIGINT');
server.stop(done);
try {
fs.unlinkSync('test-tmp/jobList.nedb');
} catch (err) {
//console.log(err);
}
try {
fs.unlinkSync('test-tmp/workerList.nedb');
} catch (err) {
//console.log(err);
}
try {
fs.unlinkSync('test-tmp/worker_config.json');
} catch (err) {
//console.log(err);
}
});

it('getWorkersInfo should return one worker with empty jobs', function (done) {
executorClient.getWorkersInfo(function (err, res) {
var keys = Object.keys(res);
if (err) {
done(err);
return;
}
should.equal(typeof res, 'object', 'getWorkersInfo did not work');
should.equal(keys.length, 1, 'No one worker attached!');
should.equal(res[keys[0]].jobs.length, 0, 'job list not empty');
done();
});
});

//it('createJob followed by getInfo should return CREATED jobInfo', function (done) {
// var jobInfo = {
// hash: '77704f10a36aa4214f5b0095ba8099e729a10f46'
// };
// executorClient.createJob(jobInfo, function (err, res) {
// var createTime;
// if (err) {
// done(err);
// return;
// }
//
// executorClient.getInfo(jobInfo.hash, function (err, res) {
// if (err) {
// done(err);
// return;
// }
// should.equal(typeof res, 'object');
// should.equal(res.createTime, createTime);
// should.equal(res.status, 'CREATED');
// done();
// });
// });
//});
});

0 comments on commit 8ce7988

Please sign in to comment.