Skip to content

Commit

Permalink
[Closes #4] Switched to using nodejs vm for execution context.
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorrustynelson committed Dec 20, 2014
1 parent e28fd7f commit 9b35293
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 35 deletions.
7 changes: 5 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module.exports = function( grunt ){
all: [
'tests/**/*tests.js'
],
banzai: [
'tests/banzai-server-tests.js'
],
connection: [
'tests/connection-manager-tests.js'
],
Expand All @@ -36,8 +39,8 @@ module.exports = function( grunt ){
ed: [
'tests/ed-server-tests.js'
],
banzai: [
'tests/banzai-server-tests.js'
filesystem: [
'tests/joule-fileSystem-tests.js'
],
integration: [
'tests/local-integration-tests.js'
Expand Down
17 changes: 15 additions & 2 deletions lib/ed-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var os = require( 'os' );
var vm = require( 'vm' );
var LOGGER = require( './utils/logger' )( 'ALL', 'ed' );
var DataManager = require( './data-manager' );
var config = require( './utils/config' );
Expand Down Expand Up @@ -51,9 +52,21 @@ function Ed( ){
case 'anonymous':
LOGGER.debug( 'Running Anonymous Joule.' );
LOGGER.debug( joule.func );
var func = eval( '(' + joule.func + ')' );

var result = func( step.input, { /* TODO */ }, 'other' );
var fileSystem = { 'Hello': true };
var context = vm.createContext( {
fileSystem: fileSystem,
input: step.input,
console: console
} );

//var func = eval( '(' + joule.func + ')' );
//var result = func( step.input, { /* TODO */ }, 'other' );

vm.runInContext( '(' + joule.func + ')()', context, joule.name + '.vm' );

var result = context.result;

step.success = true;
step.output = result;
shenzi_socket.emit( 'joule:complete', step );
Expand Down
9 changes: 4 additions & 5 deletions tests/ed-server-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ module.exports.processJouleTests = {
input: undefined,
joule: {
deploy: 'anonymous',
func: 'function( input, output, utils ){ return 12345; }'
func: 'function( ){ result = 12345; }'
}
} );
} );
Expand Down Expand Up @@ -320,7 +320,7 @@ module.exports.processJouleTests = {
input: undefined,
joule: {
deploy: 'bad-deploy-type',
func: 'function( input, output, utils ){ return 12345; }'
func: 'function( ){ result = 12345; }'
}
} );
} );
Expand Down Expand Up @@ -363,12 +363,11 @@ module.exports.processJouleTests = {
socket.emit( 'joule', {
joule: {
deploy: 'anonymous',
func: [ 'function( input, output, utils ){',
'var result = {};',
func: [ 'function( ){',
'result = {};',
'input.value.split( " " ).forEach( function( word ){',
'result[ word ] = ( result[ word ] === undefined ? 1 : result[ word ] + 1 );',
'} );',
'return result;',
'}'
].join( '\n' ),
},
Expand Down
100 changes: 100 additions & 0 deletions tests/joule-fileSystem-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Node unit quick reference:
*
* ok(value, [message])
* - Tests if value is a true value.
* equal(actual, expected, [message])
* - Tests shallow, coercive equality with the equal comparison operator ( == ).
* notEqual(actual, expected, [message])
* - Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
* deepEqual(actual, expected, [message])
* - Tests for deep equality.
* notDeepEqual(actual, expected, [message])
* - Tests for any deep inequality.
* strictEqual(actual, expected, [message])
* - Tests strict equality, as determined by the strict equality operator ( === )
* notStrictEqual(actual, expected, [message])
* - Tests strict non-equality, as determined by the strict not equal operator ( !== )
* throws(block, [error], [message])
* - Expects block to throw an error.
* doesNotThrow(block, [error], [message])
* - Expects block not to throw an error.
* ifError(value)
* - Tests if value is not a false value, throws if it is a true value.
*
* expect(amount)
* - Specify how many assertions are expected to run within a test.
* done()
* - Finish the current test function, and move on to the next. ALL tests should call this!
*/

var path = require( 'path' );
var config = require( '../lib/utils/config' );
config.reinitialize( path.resolve( __dirname, './config/test.config.json' ) );

var Shenzi = require( '../lib/shenzi-server.js' );
var Banzai = require( '../lib/banzai-server.js' );
var Ed = require( '../lib/ed-server.js' );

var shenzi = null;
var banzai = null;
var ed = null;

module.exports.integrationTest = {

setUp: function( callback ){
shenzi = new Shenzi( );
banzai = new Banzai( );
ed = new Ed( );
callback( );
},

tearDown: function( callback ){
shenzi.stop();
banzai.stop();
ed.stop();
callback( );
},

test: function( unit ){
var Crocuta = require( '../lib/client' );
var crocuta = new Crocuta( );

var timeout = setTimeout( function( ){
unit.ok( false, 'Test Timed Out.' );
crocuta.stop();
unit.done();
}, 10000 );


var job = crocuta.createJob( 'hello world' ).joule( function( /* input, output, reporter */ ){
/* global result: true */
/* global fileSystem */

console.log( typeof fileSystem );
console.log( typeof LOGGER );

result = {
fileSystem: typeof fileSystem,
LOGGER: typeof LOGGER
};
} );

crocuta.onReady( function( ){
unit.ok( true, 'onReady fired.' );
job.send( function( err, job ){
unit.ok( !err, 'No error on send' );
job.start( undefined, function( err, result ){
unit.deepEqual( result, {
fileSystem: 'object',
LOGGER: 'undefined'
}, 'Job returned correct result.' );
clearTimeout( timeout );
crocuta.stop();
unit.done( );
} );
} );
} );
}

};

Loading

0 comments on commit 9b35293

Please sign in to comment.