EventLoggerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. require('chai').should()
  12. const { expect } = require('chai')
  13. const SandboxedModule = require('sandboxed-module')
  14. const modulePath = '../../../app/js/EventLogger'
  15. const sinon = require('sinon')
  16. const tk = require('timekeeper')
  17. describe('EventLogger', function () {
  18. beforeEach(function () {
  19. this.start = Date.now()
  20. tk.freeze(new Date(this.start))
  21. this.EventLogger = SandboxedModule.require(modulePath, {
  22. requires: {
  23. 'logger-sharelatex': (this.logger = {
  24. error: sinon.stub(),
  25. warn: sinon.stub()
  26. }),
  27. 'metrics-sharelatex': (this.metrics = { inc: sinon.stub() })
  28. }
  29. })
  30. this.channel = 'applied-ops'
  31. this.id_1 = 'random-hostname:abc-1'
  32. this.message_1 = 'message-1'
  33. this.id_2 = 'random-hostname:abc-2'
  34. return (this.message_2 = 'message-2')
  35. })
  36. afterEach(function () {
  37. return tk.reset()
  38. })
  39. return describe('checkEventOrder', function () {
  40. describe('when the events are in order', function () {
  41. beforeEach(function () {
  42. this.EventLogger.checkEventOrder(
  43. this.channel,
  44. this.id_1,
  45. this.message_1
  46. )
  47. return (this.status = this.EventLogger.checkEventOrder(
  48. this.channel,
  49. this.id_2,
  50. this.message_2
  51. ))
  52. })
  53. it('should accept events in order', function () {
  54. return expect(this.status).to.be.undefined
  55. })
  56. return it('should increment the valid event metric', function () {
  57. return this.metrics.inc.calledWith(`event.${this.channel}.valid`, 1)
  58. .should.equal.true
  59. })
  60. })
  61. describe('when there is a duplicate events', function () {
  62. beforeEach(function () {
  63. this.EventLogger.checkEventOrder(
  64. this.channel,
  65. this.id_1,
  66. this.message_1
  67. )
  68. return (this.status = this.EventLogger.checkEventOrder(
  69. this.channel,
  70. this.id_1,
  71. this.message_1
  72. ))
  73. })
  74. it('should return "duplicate" for the same event', function () {
  75. return expect(this.status).to.equal('duplicate')
  76. })
  77. return it('should increment the duplicate event metric', function () {
  78. return this.metrics.inc.calledWith(`event.${this.channel}.duplicate`, 1)
  79. .should.equal.true
  80. })
  81. })
  82. describe('when there are out of order events', function () {
  83. beforeEach(function () {
  84. this.EventLogger.checkEventOrder(
  85. this.channel,
  86. this.id_1,
  87. this.message_1
  88. )
  89. this.EventLogger.checkEventOrder(
  90. this.channel,
  91. this.id_2,
  92. this.message_2
  93. )
  94. return (this.status = this.EventLogger.checkEventOrder(
  95. this.channel,
  96. this.id_1,
  97. this.message_1
  98. ))
  99. })
  100. it('should return "out-of-order" for the event', function () {
  101. return expect(this.status).to.equal('out-of-order')
  102. })
  103. return it('should increment the out-of-order event metric', function () {
  104. return this.metrics.inc.calledWith(
  105. `event.${this.channel}.out-of-order`,
  106. 1
  107. ).should.equal.true
  108. })
  109. })
  110. return describe('after MAX_STALE_TIME_IN_MS', function () {
  111. return it('should flush old entries', function () {
  112. let status
  113. this.EventLogger.MAX_EVENTS_BEFORE_CLEAN = 10
  114. this.EventLogger.checkEventOrder(
  115. this.channel,
  116. this.id_1,
  117. this.message_1
  118. )
  119. for (let i = 1; i <= 8; i++) {
  120. status = this.EventLogger.checkEventOrder(
  121. this.channel,
  122. this.id_1,
  123. this.message_1
  124. )
  125. expect(status).to.equal('duplicate')
  126. }
  127. // the next event should flush the old entries aboce
  128. this.EventLogger.MAX_STALE_TIME_IN_MS = 1000
  129. tk.freeze(new Date(this.start + 5 * 1000))
  130. // because we flushed the entries this should not be a duplicate
  131. this.EventLogger.checkEventOrder(
  132. this.channel,
  133. 'other-1',
  134. this.message_2
  135. )
  136. status = this.EventLogger.checkEventOrder(
  137. this.channel,
  138. this.id_1,
  139. this.message_1
  140. )
  141. return expect(status).to.be.undefined
  142. })
  143. })
  144. })
  145. })