|
| 1 | +var vows = require('vows'); |
| 2 | +var assert = require('assert'); |
| 3 | +var sandbox = require('sandboxed-module'); |
| 4 | + |
| 5 | +function fancyResultingHookioAppender(opts) { |
| 6 | + var result = { ons: {}, emissions: {}, logged: [] }; |
| 7 | + var fakeLog4Js = { |
| 8 | + appenderMakers: { file: function (config) { |
| 9 | + return function log(logEvent) { |
| 10 | + result.logged.push(logEvent); |
| 11 | + } |
| 12 | + }} |
| 13 | + }; |
| 14 | + var fakeHookIo = { Hook: function() { } }; |
| 15 | + fakeHookIo.Hook.prototype.start = function () { |
| 16 | + result.startCalled = true; |
| 17 | + }; |
| 18 | + fakeHookIo.Hook.prototype.on = function (eventName, functionToExec) { |
| 19 | + result.ons[eventName] = { functionToExec: functionToExec }; |
| 20 | + if (eventName === 'hook::ready') { |
| 21 | + functionToExec(); |
| 22 | + } |
| 23 | + }; |
| 24 | + fakeHookIo.Hook.prototype.emit = function (eventName, data) { |
| 25 | + result.emissions[eventName] = result.emissions[eventName] || []; |
| 26 | + result.emissions[eventName].push({data: data}); |
| 27 | + var on = '*::' + eventName; |
| 28 | + if (eventName !== 'hook::ready' && result.ons[on]) { |
| 29 | + result.ons[on].callingCount = result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1; |
| 30 | + result.ons[on].functionToExec(data); |
| 31 | + } |
| 32 | + }; |
| 33 | + return { theResult: result, |
| 34 | + theModule: sandbox.require('../lib/appenders/hookio', { |
| 35 | + requires: { |
| 36 | + '../log4js': fakeLog4Js, |
| 37 | + 'hook.io': fakeHookIo |
| 38 | + } |
| 39 | + }) |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +vows.describe('log4js hookioAppender').addBatch({ |
| 45 | + 'master': { |
| 46 | + topic: function() { |
| 47 | + var fancy = fancyResultingHookioAppender(); |
| 48 | + var logger = fancy.theModule.configure({ name: 'ohno', mode: 'master', appender: { type: 'file' } }); |
| 49 | + logger({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' }); |
| 50 | + logger({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'}); |
| 51 | + return fancy.theResult; |
| 52 | + }, |
| 53 | + |
| 54 | + 'should write to the actual appender': function (result) { |
| 55 | + assert.isTrue(result.startCalled); |
| 56 | + assert.equal(result.logged.length, 2); |
| 57 | + assert.equal(result.emissions['ohno::log'].length, 2); |
| 58 | + assert.equal(result.ons['*::ohno::log'].callingCount, 2); |
| 59 | + }, |
| 60 | + |
| 61 | + 'data written should be formatted correctly': function (result) { |
| 62 | + assert.equal(result.logged[0].level.toString(), 'INFO'); |
| 63 | + assert.equal(result.logged[0].data, 'ALRIGHTY THEN'); |
| 64 | + assert.isTrue(typeof(result.logged[0].startTime) === 'object'); |
| 65 | + assert.equal(result.logged[1].level.toString(), 'DEBUG'); |
| 66 | + assert.equal(result.logged[1].data, 'OH WOW'); |
| 67 | + assert.isTrue(typeof(result.logged[1].startTime) === 'object'); |
| 68 | + } |
| 69 | + }, |
| 70 | + 'worker': { |
| 71 | + 'should emit logging events to the master': { |
| 72 | + topic: function() { |
| 73 | + var fancy = fancyResultingHookioAppender(); |
| 74 | + var logger = fancy.theModule.configure({ name: 'ohno', mode: 'worker', appender: { type: 'file' } }); |
| 75 | + logger({ level: { levelStr: 'INFO' }, data: "ALRIGHTY THEN", startTime: '2011-10-27T03:53:16.031Z' }); |
| 76 | + logger({ level: { levelStr: 'DEBUG' }, data: "OH WOW", startTime: '2011-10-27T04:53:16.031Z'}); |
| 77 | + return fancy.theResult; |
| 78 | + }, |
| 79 | + |
| 80 | + 'should not write to the actual appender': function (result) { |
| 81 | + assert.isTrue(result.startCalled); |
| 82 | + assert.equal(result.logged.length, 0); |
| 83 | + assert.equal(result.emissions['ohno::log'].length, 2); |
| 84 | + assert.isUndefined(result.ons['*::ohno::log']); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +}).exportTo(module); |
0 commit comments