DispatchManagerTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const modulePath = '../../../../app/js/DispatchManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. const Errors = require('../../../../app/js/Errors.js')
  20. describe('DispatchManager', function () {
  21. beforeEach(function () {
  22. this.timeout(3000)
  23. this.DispatchManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './UpdateManager': (this.UpdateManager = {}),
  26. 'logger-sharelatex': (this.logger = {
  27. log: sinon.stub(),
  28. error: sinon.stub(),
  29. warn: sinon.stub()
  30. }),
  31. 'settings-sharelatex': (this.settings = {
  32. redis: {
  33. documentupdater: {}
  34. }
  35. }),
  36. 'redis-sharelatex': (this.redis = {}),
  37. './RateLimitManager': {},
  38. './Errors': Errors,
  39. './Metrics': {
  40. Timer() {
  41. return { done() {} }
  42. }
  43. }
  44. }
  45. })
  46. this.callback = sinon.stub()
  47. return (this.RateLimiter = {
  48. run(task, cb) {
  49. return task(cb)
  50. }
  51. })
  52. }) // run task without rate limit
  53. return describe('each worker', function () {
  54. beforeEach(function () {
  55. this.client = { auth: sinon.stub() }
  56. this.redis.createClient = sinon.stub().returns(this.client)
  57. return (this.worker = this.DispatchManager.createDispatcher(
  58. this.RateLimiter
  59. ))
  60. })
  61. it('should create a new redis client', function () {
  62. return this.redis.createClient.called.should.equal(true)
  63. })
  64. describe('_waitForUpdateThenDispatchWorker', function () {
  65. beforeEach(function () {
  66. this.project_id = 'project-id-123'
  67. this.doc_id = 'doc-id-123'
  68. this.doc_key = `${this.project_id}:${this.doc_id}`
  69. return (this.client.blpop = sinon
  70. .stub()
  71. .callsArgWith(2, null, ['pending-updates-list', this.doc_key]))
  72. })
  73. describe('in the normal case', function () {
  74. beforeEach(function () {
  75. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  76. .stub()
  77. .callsArg(2)
  78. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  79. })
  80. it('should call redis with BLPOP', function () {
  81. return this.client.blpop
  82. .calledWith('pending-updates-list', 0)
  83. .should.equal(true)
  84. })
  85. it('should call processOutstandingUpdatesWithLock', function () {
  86. return this.UpdateManager.processOutstandingUpdatesWithLock
  87. .calledWith(this.project_id, this.doc_id)
  88. .should.equal(true)
  89. })
  90. it('should not log any errors', function () {
  91. this.logger.error.called.should.equal(false)
  92. return this.logger.warn.called.should.equal(false)
  93. })
  94. return it('should call the callback', function () {
  95. return this.callback.called.should.equal(true)
  96. })
  97. })
  98. describe('with an error', function () {
  99. beforeEach(function () {
  100. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  101. .stub()
  102. .callsArgWith(2, new Error('a generic error'))
  103. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  104. })
  105. it('should log an error', function () {
  106. return this.logger.error.called.should.equal(true)
  107. })
  108. return it('should call the callback', function () {
  109. return this.callback.called.should.equal(true)
  110. })
  111. })
  112. return describe("with a 'Delete component' error", function () {
  113. beforeEach(function () {
  114. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  115. .stub()
  116. .callsArgWith(2, new Errors.DeleteMismatchError())
  117. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  118. })
  119. it('should log a warning', function () {
  120. return this.logger.warn.called.should.equal(true)
  121. })
  122. return it('should call the callback', function () {
  123. return this.callback.called.should.equal(true)
  124. })
  125. })
  126. })
  127. return describe('run', function () {
  128. return it('should call _waitForUpdateThenDispatchWorker until shutting down', function (done) {
  129. let callCount = 0
  130. this.worker._waitForUpdateThenDispatchWorker = (callback) => {
  131. if (callback == null) {
  132. callback = function (error) {}
  133. }
  134. callCount++
  135. if (callCount === 3) {
  136. this.settings.shuttingDown = true
  137. }
  138. return setTimeout(() => callback(), 10)
  139. }
  140. sinon.spy(this.worker, '_waitForUpdateThenDispatchWorker')
  141. this.worker.run()
  142. var checkStatus = () => {
  143. if (!this.settings.shuttingDown) {
  144. // retry until shutdown
  145. setTimeout(checkStatus, 100)
  146. } else {
  147. this.worker._waitForUpdateThenDispatchWorker.callCount.should.equal(
  148. 3
  149. )
  150. return done()
  151. }
  152. }
  153. return checkStatus()
  154. })
  155. })
  156. })
  157. })