EventLoggerTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  53. .calledWith(`event.${this.channel}.valid`)
  54. .should.equals(true)
  55. })
  56. })
  57. describe('when there is a duplicate events', function () {
  58. beforeEach(function () {
  59. this.EventLogger.checkEventOrder(
  60. this.channel,
  61. this.id_1,
  62. this.message_1
  63. )
  64. return (this.status = this.EventLogger.checkEventOrder(
  65. this.channel,
  66. this.id_1,
  67. this.message_1
  68. ))
  69. })
  70. it('should return "duplicate" for the same event', function () {
  71. return expect(this.status).to.equal('duplicate')
  72. })
  73. return it('should increment the duplicate event metric', function () {
  74. return this.metrics.inc
  75. .calledWith(`event.${this.channel}.duplicate`)
  76. .should.equals(true)
  77. })
  78. })
  79. describe('when there are out of order events', function () {
  80. beforeEach(function () {
  81. this.EventLogger.checkEventOrder(
  82. this.channel,
  83. this.id_1,
  84. this.message_1
  85. )
  86. this.EventLogger.checkEventOrder(
  87. this.channel,
  88. this.id_2,
  89. this.message_2
  90. )
  91. return (this.status = this.EventLogger.checkEventOrder(
  92. this.channel,
  93. this.id_1,
  94. this.message_1
  95. ))
  96. })
  97. it('should return "out-of-order" for the event', function () {
  98. return expect(this.status).to.equal('out-of-order')
  99. })
  100. return it('should increment the out-of-order event metric', function () {
  101. return this.metrics.inc
  102. .calledWith(`event.${this.channel}.out-of-order`)
  103. .should.equals(true)
  104. })
  105. })
  106. return describe('after MAX_STALE_TIME_IN_MS', function () {
  107. return it('should flush old entries', function () {
  108. let status
  109. this.EventLogger.MAX_EVENTS_BEFORE_CLEAN = 10
  110. this.EventLogger.checkEventOrder(
  111. this.channel,
  112. this.id_1,
  113. this.message_1
  114. )
  115. for (let i = 1; i <= 8; i++) {
  116. status = this.EventLogger.checkEventOrder(
  117. this.channel,
  118. this.id_1,
  119. this.message_1
  120. )
  121. expect(status).to.equal('duplicate')
  122. }
  123. // the next event should flush the old entries aboce
  124. this.EventLogger.MAX_STALE_TIME_IN_MS = 1000
  125. tk.freeze(new Date(this.start + 5 * 1000))
  126. // because we flushed the entries this should not be a duplicate
  127. this.EventLogger.checkEventOrder(
  128. this.channel,
  129. 'other-1',
  130. this.message_2
  131. )
  132. status = this.EventLogger.checkEventOrder(
  133. this.channel,
  134. this.id_1,
  135. this.message_1
  136. )
  137. return expect(status).to.be.undefined
  138. })
  139. })
  140. })
  141. })