hookioAppender-test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var vows = require('vows')
  3. , assert = require('assert')
  4. , sandbox = require('sandboxed-module');
  5. function fancyResultingHookioAppender(hookNotReady) {
  6. var emitHook = !hookNotReady
  7. , result = { ons: {}, emissions: {}, logged: [], configs: [] };
  8. var fakeLog4Js = {
  9. appenderMakers: {}
  10. };
  11. fakeLog4Js.loadAppender = function (appender) {
  12. fakeLog4Js.appenderMakers[appender] = function (config) {
  13. result.actualLoggerConfig = config;
  14. return function log(logEvent) {
  15. result.logged.push(logEvent);
  16. };
  17. };
  18. };
  19. var fakeHookIo = { Hook: function(config) { result.configs.push(config); } };
  20. fakeHookIo.Hook.prototype.start = function () {
  21. result.startCalled = true;
  22. };
  23. fakeHookIo.Hook.prototype.on = function (eventName, functionToExec) {
  24. result.ons[eventName] = { functionToExec: functionToExec };
  25. if (emitHook && eventName === 'hook::ready') {
  26. functionToExec();
  27. }
  28. };
  29. fakeHookIo.Hook.prototype.emit = function (eventName, data) {
  30. result.emissions[eventName] = result.emissions[eventName] || [];
  31. result.emissions[eventName].push({data: data});
  32. var on = '*::' + eventName;
  33. if (eventName !== 'hook::ready' && result.ons[on]) {
  34. result.ons[on].callingCount =
  35. result.ons[on].callingCount ? result.ons[on].callingCount += 1 : 1;
  36. result.ons[on].functionToExec(data);
  37. }
  38. };
  39. return { theResult: result,
  40. theModule: sandbox.require('../lib/appenders/hookio', {
  41. requires: {
  42. '../log4js': fakeLog4Js,
  43. 'hook.io': fakeHookIo
  44. }
  45. })
  46. };
  47. }
  48. vows.describe('log4js hookioAppender').addBatch({
  49. 'master': {
  50. topic: function() {
  51. var fancy = fancyResultingHookioAppender();
  52. var logger = fancy.theModule.configure(
  53. {
  54. name: 'ohno',
  55. mode: 'master',
  56. 'hook-port': 5001,
  57. appender: { type: 'file' }
  58. }
  59. );
  60. logger(
  61. {
  62. level: { levelStr: 'INFO' },
  63. data: "ALRIGHTY THEN",
  64. startTime: '2011-10-27T03:53:16.031Z'
  65. }
  66. );
  67. logger(
  68. {
  69. level: { levelStr: 'DEBUG' },
  70. data: "OH WOW",
  71. startTime: '2011-10-27T04:53:16.031Z'
  72. }
  73. );
  74. return fancy.theResult;
  75. },
  76. 'should write to the actual appender': function (result) {
  77. assert.isTrue(result.startCalled);
  78. assert.equal(result.configs.length, 1);
  79. assert.equal(result.configs[0]['hook-port'], 5001);
  80. assert.equal(result.logged.length, 2);
  81. assert.equal(result.emissions['ohno::log'].length, 2);
  82. assert.equal(result.ons['*::ohno::log'].callingCount, 2);
  83. },
  84. 'data written should be formatted correctly': function (result) {
  85. assert.equal(result.logged[0].level.toString(), 'INFO');
  86. assert.equal(result.logged[0].data, 'ALRIGHTY THEN');
  87. assert.isTrue(typeof(result.logged[0].startTime) === 'object');
  88. assert.equal(result.logged[1].level.toString(), 'DEBUG');
  89. assert.equal(result.logged[1].data, 'OH WOW');
  90. assert.isTrue(typeof(result.logged[1].startTime) === 'object');
  91. },
  92. 'the actual logger should get the right config': function (result) {
  93. assert.equal(result.actualLoggerConfig.type, 'file');
  94. }
  95. },
  96. 'worker': {
  97. 'should emit logging events to the master': {
  98. topic: function() {
  99. var fancy = fancyResultingHookioAppender();
  100. var logger = fancy.theModule.configure({
  101. name: 'ohno',
  102. mode: 'worker',
  103. appender: { type: 'file' }
  104. });
  105. logger({
  106. level: { levelStr: 'INFO' },
  107. data: "ALRIGHTY THEN",
  108. startTime: '2011-10-27T03:53:16.031Z'
  109. });
  110. logger({
  111. level: { levelStr: 'DEBUG' },
  112. data: "OH WOW",
  113. startTime: '2011-10-27T04:53:16.031Z'
  114. });
  115. return fancy.theResult;
  116. },
  117. 'should not write to the actual appender': function (result) {
  118. assert.isTrue(result.startCalled);
  119. assert.equal(result.logged.length, 0);
  120. assert.equal(result.emissions['ohno::log'].length, 2);
  121. assert.isUndefined(result.ons['*::ohno::log']);
  122. }
  123. }
  124. },
  125. 'when hook not ready': {
  126. topic: function() {
  127. var fancy = fancyResultingHookioAppender(true)
  128. , logger = fancy.theModule.configure({
  129. name: 'ohno',
  130. mode: 'worker'
  131. });
  132. logger({
  133. level: { levelStr: 'INFO' },
  134. data: "something",
  135. startTime: '2011-10-27T03:45:12.031Z'
  136. });
  137. return fancy;
  138. },
  139. 'should buffer the log events': function(fancy) {
  140. assert.isUndefined(fancy.theResult.emissions['ohno::log']);
  141. },
  142. },
  143. 'when hook ready': {
  144. topic: function() {
  145. var fancy = fancyResultingHookioAppender(true)
  146. , logger = fancy.theModule.configure({
  147. name: 'ohno',
  148. mode: 'worker'
  149. });
  150. logger({
  151. level: { levelStr: 'INFO' },
  152. data: "something",
  153. startTime: '2011-10-27T03:45:12.031Z'
  154. });
  155. fancy.theResult.ons['hook::ready'].functionToExec();
  156. return fancy;
  157. },
  158. 'should emit the buffered events': function(fancy) {
  159. assert.equal(fancy.theResult.emissions['ohno::log'].length, 1);
  160. }
  161. }
  162. }).exportTo(module);