This commit is contained in:
Gareth Jones
2011-10-31 08:42:58 +11:00
7 changed files with 271 additions and 50 deletions

99
test/hookioAppender.js Normal file
View File

@@ -0,0 +1,99 @@
var vows = require('vows');
var assert = require('assert');
var sandbox = require('sandboxed-module');
function fancyResultingHookioAppender(opts) {
var result = { ons: {}, emissions: {}, logged: [] };
var fakeLog4Js = {
appenderMakers: {}
};
fakeLog4Js.loadAppender = function (appender) {
fakeLog4Js.appenderMakers[appender] = function (config) {
result.actualLoggerConfig = config;
return function log(logEvent) {
result.logged.push(logEvent);
}
};
};
var fakeHookIo = { Hook: function() { } };
fakeHookIo.Hook.prototype.start = function () {
result.startCalled = true;
};
fakeHookIo.Hook.prototype.on = function (eventName, functionToExec) {
result.ons[eventName] = { functionToExec: functionToExec };
if (eventName === 'hook::ready') {
functionToExec();
}
};
fakeHookIo.Hook.prototype.emit = function (eventName, data) {
result.emissions[eventName] = result.emissions[eventName] || [];
result.emissions[eventName].push({data: data});
var on = '*::' + eventName;
if (eventName !== 'hook::ready' && result.ons[on]) {
result.ons[on].callingCount = result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1;
result.ons[on].functionToExec(data);
}
};
return { theResult: result,
theModule: sandbox.require('../lib/appenders/hookio', {
requires: {
'../log4js': fakeLog4Js,
'hook.io': fakeHookIo
}
})
};
}
vows.describe('log4js hookioAppender').addBatch({
'master': {
topic: function() {
var fancy = fancyResultingHookioAppender();
var logger = fancy.theModule.configure({ name: 'ohno', mode: 'master', appender: { type: 'file' } });
logger({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' });
logger({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'});
return fancy.theResult;
},
'should write to the actual appender': function (result) {
assert.isTrue(result.startCalled);
assert.equal(result.logged.length, 2);
assert.equal(result.emissions['ohno::log'].length, 2);
assert.equal(result.ons['*::ohno::log'].callingCount, 2);
},
'data written should be formatted correctly': function (result) {
assert.equal(result.logged[0].level.toString(), 'INFO');
assert.equal(result.logged[0].data, 'ALRIGHTY THEN');
assert.isTrue(typeof(result.logged[0].startTime) === 'object');
assert.equal(result.logged[1].level.toString(), 'DEBUG');
assert.equal(result.logged[1].data, 'OH WOW');
assert.isTrue(typeof(result.logged[1].startTime) === 'object');
},
'the actual logger should get the right config': function (result) {
assert.equal(result.actualLoggerConfig.type, 'file');
}
},
'worker': {
'should emit logging events to the master': {
topic: function() {
var fancy = fancyResultingHookioAppender();
var logger = fancy.theModule.configure({ name: 'ohno', mode: 'worker', appender: { type: 'file' } });
logger({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' });
logger({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'});
return fancy.theResult;
},
'should not write to the actual appender': function (result) {
assert.isTrue(result.startCalled);
assert.equal(result.logged.length, 0);
assert.equal(result.emissions['ohno::log'].length, 2);
assert.isUndefined(result.ons['*::ohno::log']);
}
}
}
}).exportTo(module);

View File

@@ -69,7 +69,7 @@ vows.describe('log4js').addBatch({
, log4js = sandbox.require(
'../lib/log4js'
, { requires:
{ './appenders/file.js':
{ './appenders/file':
{
name: "file"
, appender: function() {}
@@ -123,10 +123,10 @@ vows.describe('log4js').addBatch({
});
},
readdirSync: function() {
return ['file.js'];
return ['file'];
}
}
, './appenders/file.js':
, './appenders/file':
{
name: "file"
, appender: function() {}
@@ -168,7 +168,7 @@ vows.describe('log4js').addBatch({
'../lib/log4js'
, {
requires: {
'./appenders/console.js': fakeConsoleAppender
'./appenders/console': fakeConsoleAppender
}
}
);
@@ -303,7 +303,7 @@ vows.describe('log4js').addBatch({
{
requires: {
'fs': fakeFS
, './appenders/console.js': fakeConsole
, './appenders/console': fakeConsole
}
}
);
@@ -434,7 +434,7 @@ vows.describe('log4js').addBatch({
{
requires: {
'fs': fakeFS,
'./appenders/console.js': fakeConsole
'./appenders/console': fakeConsole
},
globals: {
'console': fakeConsole,
@@ -462,7 +462,7 @@ vows.describe('log4js').addBatch({
assert.equal(logEvents[2].data[0], 'debug4');
}
},
'configuration reload with configuration staying the same' : {
topic: function() {
var pathsChecked = [],
@@ -494,10 +494,10 @@ vows.describe('log4js').addBatch({
}
},
fakeConsole = {
'name': 'console',
'name': 'console',
'appender': function () {
return function(evt) { logEvents.push(evt); };
},
},
'configure': function (config) {
return fakeConsole.appender();
}
@@ -510,8 +510,8 @@ vows.describe('log4js').addBatch({
'../lib/log4js',
{
requires: {
'fs': fakeFS,
'./appenders/console.js': fakeConsole
'fs': fakeFS,
'./appenders/console': fakeConsole
},
globals: {
'console': fakeConsole,
@@ -538,7 +538,7 @@ vows.describe('log4js').addBatch({
var logEvents = args[1];
assert.length(logEvents, 2);
assert.equal(logEvents[0].data[0], 'info1');
assert.equal(logEvents[1].data[0], 'info3');
assert.equal(logEvents[1].data[0], 'info3');
}
}