Add tests for multiprocess file appender

This commit is contained in:
Danny Brain 2011-11-03 09:10:02 +11:00
parent 097390bc89
commit a95117c0d3
3 changed files with 176 additions and 2 deletions

View File

@ -20,7 +20,7 @@ var LogServer = function createLogServer(config) {
// JSON.parse failed, just log the contents probably a naughty. // JSON.parse failed, just log the contents probably a naughty.
actualAppender(createLoggingEvent('ERROR', 'Unable to parse log: ' + logMessage)); actualAppender(createLoggingEvent('ERROR', 'Unable to parse log: ' + logMessage));
} }
}) });
}); });
}); });
server.listen(config.loggerPort || 5000, config.loggerHost || 'localhost'); server.listen(config.loggerPort || 5000, config.loggerHost || 'localhost');

View File

@ -26,6 +26,7 @@
"devDependencies": { "devDependencies": {
"vows": ">=0.5.2", "vows": ">=0.5.2",
"sandboxed-module": ">= 0.1.1", "sandboxed-module": ">= 0.1.1",
"hook.io": "0.7.7" "hook.io": "0.7.7",
"underscore": "1.2.1"
} }
} }

View File

@ -0,0 +1,173 @@
var vows = require('vows');
var assert = require('assert');
var sandbox = require('sandboxed-module');
var _ = require('underscore');
function fancyResultingMultiprocessAppender(opts) {
var result = { clientOns: {}, serverOns: {}, logged: [], ended: [] };
var fakeSocket = {
on: function (event, fn) {
result.clientOns[event] = fn;
if (event === 'connect') {
fn();
}
},
end: function (data, encoding) {
result.ended.push({ data: data, encoding: encoding });
}
}
var fakeServerSocket = {
on: function (event, fn) {
result.serverOns[event] = fn;
if (event === 'connect') {
fn();
}
}
}
var fakeServer = {
listen: function (port, host) {
result.listenPort = port;
result.listenHost = host;
}
}
var fakeNet = {
createServer: function (fn) {
fn(fakeServerSocket);
return fakeServer;
},
createConnection: function (port, host) {
result.connectPort = port;
result.connectHost = host;
return fakeSocket;
}
}
var fakeLog4Js = {
appenderMakers: {}
};
fakeLog4Js.loadAppender = function (appender) {
fakeLog4Js.appenderMakers[appender] = function (config) {
result.actualLoggerConfig = config;
return function log(logEvent) {
result.logged.push(logEvent);
}
};
};
return { theResult: result,
theModule: sandbox.require('../lib/appenders/multiprocess', {
requires: {
'../log4js': fakeLog4Js,
'net': fakeNet
}
})
};
}
function logMessages(result, logs, raw) {
logs.forEach(function log(item) {
var logItem = { startTime: "Wed, 02 Nov 2011 21:46:39 GMT", level: { levelStr: 'DEBUG' }, data: [ item ] };
result.serverOns.data(JSON.stringify(logItem));
result.serverOns.end();
result.serverOns.connect();
});
if (raw) {
raw.forEach(function log(rawItem) {
result.serverOns.data(rawItem);
result.serverOns.end();
result.serverOns.connect();
});
}
}
vows.describe('log4js multiprocessAppender').addBatch({
'master': {
topic: function() {
var fancy = fancyResultingMultiprocessAppender();
var logger = fancy.theModule.configure({ mode: 'master', 'loggerPort': 5001, 'loggerHost': 'abba', appender: { type: 'file' } });
logMessages(fancy.theResult, [ 'ALRIGHTY THEN', 'OH WOW' ]);
return fancy.theResult;
},
'should write to the actual appender': function (result) {
assert.equal(result.listenPort, 5001);
assert.equal(result.listenHost, 'abba');
assert.equal(result.logged.length, 2);
assert.equal(result.logged[0].data[0], 'ALRIGHTY THEN');
assert.equal(result.logged[1].data[0], 'OH WOW');
},
'data written should be formatted correctly': function (result) {
assert.equal(result.logged[0].level.toString(), 'DEBUG');
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');
},
'client should not be called': function (result) {
assert.equal(_.keys(result.clientOns).length, 0);
}
},
'master with bad request': {
topic: function() {
var fancy = fancyResultingMultiprocessAppender();
var logger = fancy.theModule.configure({ mode: 'master', 'loggerPort': 5001, 'loggerHost': 'abba', appender: { type: 'file' } });
logMessages(fancy.theResult, [], [ 'ALRIGHTY THEN', 'OH WOW' ]);
return fancy.theResult;
},
'should write to the actual appender': function (result) {
assert.equal(result.listenPort, 5001);
assert.equal(result.listenHost, 'abba');
assert.equal(result.logged.length, 2);
assert.equal(result.logged[0].data[0], 'Unable to parse log: ALRIGHTY THEN');
assert.equal(result.logged[1].data[0], 'Unable to parse log: OH WOW');
},
'data written should be formatted correctly': function (result) {
assert.equal(result.logged[0].level.toString(), 'ERROR');
assert.equal(result.logged[0].data, 'Unable to parse log: ALRIGHTY THEN');
assert.isTrue(typeof(result.logged[0].startTime) === 'object');
assert.equal(result.logged[1].level.toString(), 'ERROR');
assert.equal(result.logged[1].data, 'Unable to parse log: OH WOW');
assert.isTrue(typeof(result.logged[1].startTime) === 'object');
}
},
'worker': {
'should emit logging events to the master': {
topic: function() {
var fancy = fancyResultingMultiprocessAppender();
var logger = fancy.theModule.configure({ loggerHost: 'baba', loggerPort: 1232, 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;
},
'client configuration should be correct': function (result) {
assert.equal(result.connectHost, 'baba');
assert.equal(result.connectPort, 1232);
},
'should not write to the actual appender': function (result) {
assert.equal(result.logged.length, 0);
assert.equal(result.ended.length, 2);
assert.equal(result.ended[0].data, JSON.stringify({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' }));
assert.equal(result.ended[0].encoding, 'utf8');
assert.equal(result.ended[1].data, JSON.stringify({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'}));
assert.equal(result.ended[1].encoding, 'utf8');
assert.equal(_.keys(result.serverOns).length, 0);
}
}
}
}).exportTo(module);