2013-08-09 06:04:25 +08:00
|
|
|
"use strict";
|
2013-08-24 18:46:10 +08:00
|
|
|
var should = require('should')
|
|
|
|
, sandbox = require('sandboxed-module');
|
|
|
|
|
|
|
|
|
|
|
|
describe('log4js in a cluster', function() {
|
|
|
|
describe('when in master mode', function() {
|
|
|
|
|
|
|
|
var log4js
|
|
|
|
, clusterOnFork = false
|
|
|
|
, workerCb
|
|
|
|
, events = []
|
|
|
|
, worker = {
|
|
|
|
on: function(evt, cb) {
|
|
|
|
evt.should.eql('message');
|
|
|
|
this.cb = cb;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js',
|
|
|
|
{
|
|
|
|
requires: {
|
|
|
|
'cluster': {
|
|
|
|
isMaster: true,
|
|
|
|
on: function(evt, cb) {
|
|
|
|
evt.should.eql('fork');
|
|
|
|
clusterOnFork = true;
|
|
|
|
cb(worker);
|
|
|
|
}
|
|
|
|
},
|
2013-09-15 12:37:01 +08:00
|
|
|
'./appenders/console': function() {
|
|
|
|
return function() {
|
2013-08-24 18:46:10 +08:00
|
|
|
return function(event) {
|
|
|
|
events.push(event);
|
|
|
|
};
|
2013-09-15 12:37:01 +08:00
|
|
|
};
|
2013-08-24 18:46:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should listen for fork events', function() {
|
2013-08-29 14:56:40 +08:00
|
|
|
clusterOnFork.should.eql(true);
|
2013-08-24 18:46:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should listen for messages from workers', function() {
|
|
|
|
//workerCb was created in a different context to the test
|
|
|
|
//(thanks to sandbox.require), so doesn't pick up the should prototype
|
|
|
|
(typeof worker.cb).should.eql('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should log valid ::log4js-message events', function() {
|
|
|
|
worker.cb({
|
|
|
|
type: '::log4js-message',
|
|
|
|
event: JSON.stringify({
|
|
|
|
startTime: '2010-10-10 18:54:06',
|
|
|
|
category: 'cheese',
|
|
|
|
level: { levelStr: 'DEBUG' },
|
|
|
|
data: [ "blah" ]
|
|
|
|
})
|
|
|
|
});
|
|
|
|
events.should.have.length(1);
|
|
|
|
events[0].data[0].should.eql("blah");
|
|
|
|
events[0].category.should.eql('cheese');
|
|
|
|
//startTime was created in a different context to the test
|
|
|
|
//(thanks to sandbox.require), so instanceof doesn't think
|
|
|
|
//it's a Date.
|
|
|
|
events[0].startTime.constructor.name.should.eql('Date');
|
|
|
|
events[0].level.toString().should.eql('DEBUG');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle invalid ::log4js-message events', function() {
|
|
|
|
worker.cb({
|
|
|
|
type: '::log4js-message',
|
|
|
|
event: "biscuits"
|
|
|
|
});
|
|
|
|
worker.cb({
|
|
|
|
type: '::log4js-message',
|
|
|
|
event: JSON.stringify({
|
|
|
|
startTime: 'whatever'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
events.should.have.length(3);
|
|
|
|
events[1].data[0].should.eql('Unable to parse log:');
|
|
|
|
events[1].data[1].should.eql('biscuits');
|
|
|
|
events[1].category.should.eql('log4js');
|
|
|
|
events[1].level.toString().should.eql('ERROR');
|
|
|
|
|
|
|
|
events[2].data[0].should.eql('Unable to parse log:');
|
|
|
|
events[2].data[1].should.eql(JSON.stringify({ startTime: 'whatever'}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore other events', function() {
|
|
|
|
worker.cb({
|
|
|
|
type: "::blah-blah",
|
|
|
|
event: "blah"
|
|
|
|
});
|
|
|
|
|
|
|
|
events.should.have.length(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when in worker mode', function() {
|
|
|
|
var log4js, events = [];
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
log4js = sandbox.require(
|
|
|
|
'../lib/log4js',
|
|
|
|
{
|
|
|
|
requires: {
|
|
|
|
'cluster': {
|
|
|
|
isMaster: false,
|
|
|
|
on: function() {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
globals: {
|
|
|
|
'process': {
|
|
|
|
'send': function(event) {
|
|
|
|
events.push(event);
|
2013-08-29 06:49:42 +08:00
|
|
|
},
|
|
|
|
'env': {
|
2013-08-24 18:46:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
log4js.getLogger('test').debug("just testing");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit ::log4js-message events', function() {
|
|
|
|
events.should.have.length(1);
|
|
|
|
events[0].type.should.eql('::log4js-message');
|
|
|
|
events[0].event.should.be.a('string');
|
|
|
|
|
|
|
|
var evt = JSON.parse(events[0].event);
|
|
|
|
evt.category.should.eql('test');
|
|
|
|
evt.level.levelStr.should.eql('DEBUG');
|
|
|
|
evt.data[0].should.eql('just testing');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|