completed testHarness and added testHarness test.

This commit is contained in:
Brandon Nozaki Miller 2014-09-03 18:05:10 -07:00
parent 0189a44ee3
commit 4063264542
4 changed files with 180 additions and 81 deletions

View file

@ -2,107 +2,165 @@ var ipc = require('../node-ipc'),
cmd = require('node-cmd'), cmd = require('node-cmd'),
fs = require('fs'), fs = require('fs'),
events= require('event-pubsub')(), events= require('event-pubsub')(),
tests = {}; tests = {},
testCount=0,
fails=[],
passes=[];
ipc.config.id = 'testHarness'; ipc.config.id = 'testHarness';
events.on( ipc.serve(
'startFailed', function(){
function(test){
ipc.log(test.warn," failed to start ".error)
} }
); );
ipc.serve( ipc.server.on(
function(){
ipc.server.on(
'pass', 'pass',
function(data,socket){ function(test,socket){
ipc.log(socket.id.good); ipc.log(socket.id.good,'passed',test.data);
socket tests[socket.id].pass.push(test);
passes.push(test);
} }
); );
ipc.server.on( ipc.server.on(
'fail', 'fail',
function(err,socket){ function(test,socket){
ipc.log(socket.id.warn,err); ipc.log(socket.id.warn,'failed',test.data);
socket tests[socket.id].fail.push(test);
fails.push(test);
} }
); );
ipc.server.on( ipc.server.on(
'start', 'start.test',
function(data,socket){ function(data,socket){
ipc.log(socket.id.notice, 'started'.debug);
events.trigger(
'started-test-'+socket.id,
socket.id
);
}
);
socket.id=data.id;
ipc.log('TestHarness started.'.debug, 'Loading Tests.'.notice); if(!data.id){
ipc.log('start.test duration not passed for unknown test, so failing test'.error);
fs.readdir( socket.destroy();
'tests',
function(err,tests){
if(err){
ipc.log('You must execute the testHarness from the testHarness directory')
return; return;
} }
for(var i =0; i<tests.length; i++){
tests[tests[i]]={ if(!data.duration){
ipc.log(data.id.notice, 'start.test duration not passed, so failing test'.error);
ipc.disconnect(data.id);
return;
}
ipc.log(data.id.notice, 'started'.debug);
tests[data.id].started=true;
clearTimeout(
tests[data.id].delay
);
tests[data.id].delay=setTimeout(
(
function(test){
return function(){
ipc.log(test.warn," failed to complete ".error);
tests[test].fail.push('end.test');
fails.push(test+' >> end.test');
testCount--;
tests[socket.id].delay=null;
checkComplete();
}
}
)(data.id),
data.duration
)
}
);
ipc.server.on(
'end.test',
function(data,socket){
ipc.log(socket.id.notice, 'completed'.debug);
clearTimeout(
tests[socket.id].delay
);
tests[socket.id].delay=null;
testCount--;
checkComplete();
}
);
ipc.log('TestHarness started.'.debug, 'Loading Tests.'.notice);
ipc.server.define.listen['start.test']='This event should be called when a test is starting. It accepts an object with test details including the test id and duration';
ipc.server.define.listen['end.test']='This event should be called when a test is completed.';
ipc.server.define.listen['pass']='This event should be called when a test has passed, it accepts the name of the test portion which passed';
ipc.server.define.listen['fail']='This event should be called when a test has failed, it accepts the name of the test portion which failed';
ipc.server.start();
fs.readdir(
'tests',
function(err,testFolders){
if(err){
ipc.log(err,'You must execute the testHarness from the testHarness directory'.error)
return;
}
testCount=testFolders.length;
for(var i =0; i<testFolders.length; i++){
tests[testFolders[i]]={
started : false, started : false,
pass : false, pass : [],
fail : true, fail : [],
delay : setTimeout( delay : setTimeout(
( (
function(test){ function(test){
return function(){ return function(){
events.trigger( ipc.log(test.warn," failed to start ".error);
'startFailed', tests[test].fail.push('start.test');
test fails.push(test+' >> start.test');
); testCount--;
checkComplete();
} }
} }
)(tests[i]), )(testFolders[i]),
1000 1000
) )
}; };
runTests(tests[i])
runTests(testFolders[i])
} }
} }
);
}
); );
ipc.server.define.listen['pass']='This event should be called when a test has passed';
ipc.server.define.listen['fail']='This event should be called when a test has failed, it accepts an argument for error conditions';
ipc.server.start();
function runTests(dir){ function runTests(dir){
fs.readdir( fs.readdir(
'tests/'+dir, 'tests/'+dir,
function(err,tests){ function(err,testFiles){
if(err){ if(err){
ipc.log('You must execute the testHarness from the testHarness directory') ipc.log(err,'You must execute the testHarness from the testHarness directory'.error)
return; return;
} }
for(var i =0; i<tests.length; i++){ for(var i =0; i<testFiles.length; i++){
events.on( cmd.run(
'started-test-'+tests[i], 'node tests/'+dir+'/'+testFiles[i]
function(id){
tests[id].started=true;
clearTimeout(
tests[id].delay
); );
} }
);
cmd.run('node '+tests[i]);
}
} }
); );
} }
function checkComplete(){
if(testCount)
return;
ipc.log('####################################\n\n RESULTS\n\n####################################\n\n'.rainbow,tests,'\n\n');
ipc.log('####################################\n\n PASSES\n\n####################################\n\n'.good,passes.join('\n').good,'\n\n');
ipc.log('####################################\n\n FAILS\n\n####################################\n\n'.warn,fails.join('\n').warn,'\n\n');
ipc.log('####################################\n\n COUNT\n\n####################################\n\n'.data,(passes.length+' ').good,(fails.length+' ').warn,'\n\n');
process.exit(0);
}

View file

@ -0,0 +1,41 @@
var ipc = require('../../../node-ipc');
ipc.config.id = __dirname.split('/');
ipc.config.id = ipc.config.id[ipc.config.id.length-1]
ipc.config.maxRetries=1;
ipc.connectTo(
'testHarness',
function(){
ipc.of.testHarness.on(
'connect',
function(){
ipc.of.testHarness.emit(
'start.test',
{
id : ipc.config.id,
duration: 1000
}
);
ipc.of.testHarness.emit(
'pass',
'test-harness-pass-test'
);
ipc.of.testHarness.emit(
'fail',
'test-harness-fail-test'
);
setTimeout(
function(){
//delay exit incase you want to test failure of start.test by modifying event name
ipc.of.testHarness.emit(
'end.test'
);
ipc.of.testHarness.disconnect();
},
1000
);
}
);
}
);