DispatchManagerTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. let Timer
  23. this.timeout(3000)
  24. this.DispatchManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './UpdateManager': (this.UpdateManager = {}),
  27. 'logger-sharelatex': (this.logger = {
  28. log: sinon.stub(),
  29. error: sinon.stub(),
  30. warn: sinon.stub()
  31. }),
  32. 'settings-sharelatex': (this.settings = {
  33. redis: {
  34. documentupdater: {}
  35. }
  36. }),
  37. 'redis-sharelatex': (this.redis = {}),
  38. './RateLimitManager': {},
  39. './Errors': Errors,
  40. './Metrics': (this.Metrics = {
  41. Timer: (Timer = (function () {
  42. Timer = class Timer {
  43. static initClass() {
  44. this.prototype.done = sinon.stub()
  45. }
  46. }
  47. Timer.initClass()
  48. return Timer
  49. })())
  50. })
  51. }
  52. })
  53. this.callback = sinon.stub()
  54. return (this.RateLimiter = {
  55. run(task, cb) {
  56. return task(cb)
  57. }
  58. })
  59. }) // run task without rate limit
  60. return describe('each worker', function () {
  61. beforeEach(function () {
  62. this.client = { auth: sinon.stub() }
  63. this.redis.createClient = sinon.stub().returns(this.client)
  64. return (this.worker = this.DispatchManager.createDispatcher(
  65. this.RateLimiter
  66. ))
  67. })
  68. it('should create a new redis client', function () {
  69. return this.redis.createClient.called.should.equal(true)
  70. })
  71. describe('_waitForUpdateThenDispatchWorker', function () {
  72. beforeEach(function () {
  73. this.project_id = 'project-id-123'
  74. this.doc_id = 'doc-id-123'
  75. this.doc_key = `${this.project_id}:${this.doc_id}`
  76. return (this.client.blpop = sinon
  77. .stub()
  78. .callsArgWith(2, null, ['pending-updates-list', this.doc_key]))
  79. })
  80. describe('in the normal case', function () {
  81. beforeEach(function () {
  82. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  83. .stub()
  84. .callsArg(2)
  85. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  86. })
  87. it('should call redis with BLPOP', function () {
  88. return this.client.blpop
  89. .calledWith('pending-updates-list', 0)
  90. .should.equal(true)
  91. })
  92. it('should call processOutstandingUpdatesWithLock', function () {
  93. return this.UpdateManager.processOutstandingUpdatesWithLock
  94. .calledWith(this.project_id, this.doc_id)
  95. .should.equal(true)
  96. })
  97. it('should not log any errors', function () {
  98. this.logger.error.called.should.equal(false)
  99. return this.logger.warn.called.should.equal(false)
  100. })
  101. return it('should call the callback', function () {
  102. return this.callback.called.should.equal(true)
  103. })
  104. })
  105. describe('with an error', function () {
  106. beforeEach(function () {
  107. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  108. .stub()
  109. .callsArgWith(2, new Error('a generic error'))
  110. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  111. })
  112. it('should log an error', function () {
  113. return this.logger.error.called.should.equal(true)
  114. })
  115. return it('should call the callback', function () {
  116. return this.callback.called.should.equal(true)
  117. })
  118. })
  119. return describe("with a 'Delete component' error", function () {
  120. beforeEach(function () {
  121. this.UpdateManager.processOutstandingUpdatesWithLock = sinon
  122. .stub()
  123. .callsArgWith(2, new Errors.DeleteMismatchError())
  124. return this.worker._waitForUpdateThenDispatchWorker(this.callback)
  125. })
  126. it('should log a warning', function () {
  127. return this.logger.warn.called.should.equal(true)
  128. })
  129. return it('should call the callback', function () {
  130. return this.callback.called.should.equal(true)
  131. })
  132. })
  133. })
  134. return describe('run', function () {
  135. return it('should call _waitForUpdateThenDispatchWorker until shutting down', function (done) {
  136. let callCount = 0
  137. this.worker._waitForUpdateThenDispatchWorker = (callback) => {
  138. if (callback == null) {
  139. callback = function (error) {}
  140. }
  141. callCount++
  142. if (callCount === 3) {
  143. this.settings.shuttingDown = true
  144. }
  145. return setTimeout(() => callback(), 10)
  146. }
  147. sinon.spy(this.worker, '_waitForUpdateThenDispatchWorker')
  148. this.worker.run()
  149. var checkStatus = () => {
  150. if (!this.settings.shuttingDown) {
  151. // retry until shutdown
  152. setTimeout(checkStatus, 100)
  153. } else {
  154. this.worker._waitForUpdateThenDispatchWorker.callCount.should.equal(
  155. 3
  156. )
  157. return done()
  158. }
  159. }
  160. return checkStatus()
  161. })
  162. })
  163. })
  164. })