log4js-node/test/hookioAppender-test.js

135 lines
4.1 KiB
JavaScript
Raw Normal View History

2013-05-27 06:14:51 +08:00
"use strict";
var vows = require('vows')
, assert = require('assert')
, sandbox = require('sandboxed-module');
2011-10-27 13:03:06 +08:00
function fancyResultingHookioAppender(opts) {
var result = { ons: {}, emissions: {}, logged: [], configs: [] };
2011-10-27 13:25:38 +08:00
2011-10-27 13:03:06 +08:00
var fakeLog4Js = {
2011-10-27 13:25:38 +08:00
appenderMakers: {}
};
fakeLog4Js.loadAppender = function (appender) {
fakeLog4Js.appenderMakers[appender] = function (config) {
result.actualLoggerConfig = config;
2011-10-27 13:03:06 +08:00
return function log(logEvent) {
result.logged.push(logEvent);
2013-05-27 06:14:51 +08:00
};
2011-10-27 13:25:38 +08:00
};
2011-10-27 13:03:06 +08:00
};
2011-10-27 13:25:38 +08:00
var fakeHookIo = { Hook: function(config) { result.configs.push(config); } };
2011-10-27 13:03:06 +08:00
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]) {
2013-05-27 06:14:51 +08:00
result.ons[on].callingCount =
result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1;
2011-10-27 13:03:06 +08:00
result.ons[on].functionToExec(data);
}
};
2011-10-27 13:25:38 +08:00
2011-10-27 13:03:06 +08:00
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();
2013-05-27 06:14:51 +08:00
var logger = fancy.theModule.configure(
{
name: 'ohno',
mode: 'master',
'hook-port': 5001,
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'
}
);
2011-10-27 13:03:06 +08:00
return fancy.theResult;
},
'should write to the actual appender': function (result) {
assert.isTrue(result.startCalled);
assert.equal(result.configs.length, 1);
assert.equal(result.configs[0]['hook-port'], 5001);
2011-10-27 13:03:06 +08:00
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');
2011-10-27 13:03:06 +08:00
}
},
'worker': {
'should emit logging events to the master': {
topic: function() {
var fancy = fancyResultingHookioAppender();
2013-05-27 06:14:51 +08:00
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'
});
2011-10-27 13:03:06 +08:00
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);