EventLoggerTests.js 4.4 KB

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