2013-08-09 06:04:25 +08:00
|
|
|
"use strict";
|
|
|
|
var assert = require('assert');
|
|
|
|
var vows = require('vows');
|
|
|
|
var layouts = require('../lib/layouts');
|
|
|
|
var sandbox = require('sandboxed-module');
|
|
|
|
var LoggingEvent = require('../lib/logger').LoggingEvent;
|
|
|
|
var cluster = require('cluster');
|
|
|
|
|
|
|
|
vows.describe('log4js cluster appender').addBatch({
|
|
|
|
'when in master mode': {
|
|
|
|
topic: function() {
|
|
|
|
|
|
|
|
var registeredClusterEvents = [];
|
|
|
|
var loggingEvents = [];
|
|
|
|
|
|
|
|
// Fake cluster module, so no cluster listeners be really added
|
|
|
|
var fakeCluster = {
|
|
|
|
|
|
|
|
on: function(event, callback) {
|
|
|
|
registeredClusterEvents.push(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
isMaster: true,
|
|
|
|
isWorker: false,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var fakeActualAppender = function(loggingEvent) {
|
|
|
|
loggingEvents.push(loggingEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load appender and fake modules in it
|
|
|
|
var appenderModule = sandbox.require('../lib/appenders/clustered', {
|
|
|
|
requires: {
|
|
|
|
'cluster': fakeCluster,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var masterAppender = appenderModule.appender({
|
2014-05-20 19:42:29 +08:00
|
|
|
actualAppenders: [fakeActualAppender, fakeActualAppender, fakeActualAppender],
|
|
|
|
appenders: [{}, {category: "test"}, {category: "wovs"}]
|
2013-08-09 06:04:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Actual test - log message using masterAppender
|
|
|
|
masterAppender(new LoggingEvent('wovs', 'Info', ['masterAppender test']));
|
|
|
|
|
|
|
|
var returnValue = {
|
|
|
|
registeredClusterEvents: registeredClusterEvents,
|
|
|
|
loggingEvents: loggingEvents,
|
|
|
|
};
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
"should register 'fork' event listener on 'cluster'": function(topic) {
|
|
|
|
assert.equal(topic.registeredClusterEvents[0], 'fork');
|
|
|
|
},
|
|
|
|
|
|
|
|
"should log using actual appender": function(topic) {
|
2014-05-20 19:42:29 +08:00
|
|
|
assert.equal(topic.loggingEvents.length, 2)
|
2013-08-09 06:04:25 +08:00
|
|
|
assert.equal(topic.loggingEvents[0].data[0], 'masterAppender test');
|
2014-05-20 19:42:29 +08:00
|
|
|
assert.equal(topic.loggingEvents[1].data[0], 'masterAppender test');
|
2013-08-09 06:04:25 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
'when in worker mode': {
|
|
|
|
|
|
|
|
topic: function() {
|
|
|
|
|
|
|
|
var registeredProcessEvents = [];
|
|
|
|
|
|
|
|
// Fake cluster module, to fake we're inside a worker process
|
|
|
|
var fakeCluster = {
|
|
|
|
|
|
|
|
isMaster: false,
|
|
|
|
isWorker: true,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var fakeProcess = {
|
|
|
|
|
|
|
|
send: function(data) {
|
|
|
|
registeredProcessEvents.push(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load appender and fake modules in it
|
|
|
|
var appenderModule = sandbox.require('../lib/appenders/clustered', {
|
|
|
|
requires: {
|
|
|
|
'cluster': fakeCluster,
|
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
'process': fakeProcess,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var workerAppender = appenderModule.appender();
|
|
|
|
|
|
|
|
// Actual test - log message using masterAppender
|
|
|
|
workerAppender(new LoggingEvent('wovs', 'Info', ['workerAppender test']));
|
2013-09-27 02:55:20 +08:00
|
|
|
workerAppender(new LoggingEvent('wovs', 'Info', [new Error('Error test')]));
|
2013-08-09 06:04:25 +08:00
|
|
|
|
|
|
|
var returnValue = {
|
|
|
|
registeredProcessEvents: registeredProcessEvents,
|
|
|
|
};
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"worker appender should call process.send" : function(topic) {
|
|
|
|
assert.equal(topic.registeredProcessEvents[0].type, '::log-message');
|
|
|
|
assert.equal(JSON.parse(topic.registeredProcessEvents[0].event).data[0], "workerAppender test");
|
2013-09-27 02:55:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
"worker should serialize an Error correctly" : function(topic) {
|
|
|
|
assert.equal(topic.registeredProcessEvents[1].type, '::log-message');
|
2013-09-27 21:45:10 +08:00
|
|
|
assert(JSON.parse(topic.registeredProcessEvents[1].event).data[0].stack);
|
|
|
|
var actual = JSON.parse(topic.registeredProcessEvents[1].event).data[0].stack;
|
|
|
|
var expectedRegex = /^Error: Error test/;
|
|
|
|
assert(actual.match(expectedRegex), "Expected: \n\n " + actual + "\n\n to match " + expectedRegex);
|
2013-08-09 06:04:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).exportTo(module);
|