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
|
|
|
|
2013-07-08 06:46:11 +08:00
|
|
|
function fancyResultingHookioAppender(hookNotReady) {
|
|
|
|
var emitHook = !hookNotReady
|
|
|
|
, 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) {
|
2011-10-27 13:43:55 +08:00
|
|
|
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
|
|
|
|
2011-10-28 07:07:48 +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 };
|
2013-07-08 06:46:11 +08:00
|
|
|
if (emitHook && eventName === 'hook::ready') {
|
2011-10-27 13:03:06 +08:00
|
|
|
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);
|
2011-10-28 07:07:48 +08:00
|
|
|
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');
|
2011-10-27 13:43:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'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']);
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 06:46:11 +08:00
|
|
|
},
|
|
|
|
'when hook not ready': {
|
|
|
|
topic: function() {
|
|
|
|
var fancy = fancyResultingHookioAppender(true)
|
|
|
|
, logger = fancy.theModule.configure({
|
|
|
|
name: 'ohno',
|
|
|
|
mode: 'worker'
|
|
|
|
});
|
|
|
|
|
|
|
|
logger({
|
|
|
|
level: { levelStr: 'INFO' },
|
|
|
|
data: "something",
|
|
|
|
startTime: '2011-10-27T03:45:12.031Z'
|
|
|
|
});
|
|
|
|
return fancy;
|
|
|
|
},
|
|
|
|
'should buffer the log events': function(fancy) {
|
|
|
|
assert.isUndefined(fancy.theResult.emissions['ohno::log']);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'when hook ready': {
|
|
|
|
topic: function() {
|
|
|
|
var fancy = fancyResultingHookioAppender(true)
|
|
|
|
, logger = fancy.theModule.configure({
|
|
|
|
name: 'ohno',
|
|
|
|
mode: 'worker'
|
|
|
|
});
|
|
|
|
|
|
|
|
logger({
|
|
|
|
level: { levelStr: 'INFO' },
|
|
|
|
data: "something",
|
|
|
|
startTime: '2011-10-27T03:45:12.031Z'
|
|
|
|
});
|
|
|
|
|
|
|
|
fancy.theResult.ons['hook::ready'].functionToExec();
|
|
|
|
return fancy;
|
|
|
|
},
|
|
|
|
'should emit the buffered events': function(fancy) {
|
|
|
|
assert.equal(fancy.theResult.emissions['ohno::log'].length, 1);
|
|
|
|
}
|
2011-10-27 13:03:06 +08:00
|
|
|
}
|
2013-07-08 06:46:11 +08:00
|
|
|
|
2011-10-27 13:03:06 +08:00
|
|
|
}).exportTo(module);
|