flushProjectTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS206: Consider reworking classes to avoid initClass
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. const sinon = require('sinon')
  18. const chai = require('chai')
  19. const should = chai.should()
  20. const modulePath = '../../../../app/js/ProjectManager.js'
  21. const SandboxedModule = require('sandboxed-module')
  22. describe('ProjectManager - flushProject', function () {
  23. beforeEach(function () {
  24. let Timer
  25. this.ProjectManager = SandboxedModule.require(modulePath, {
  26. requires: {
  27. './RedisManager': (this.RedisManager = {}),
  28. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  29. './DocumentManager': (this.DocumentManager = {}),
  30. 'logger-sharelatex': (this.logger = {
  31. log: sinon.stub(),
  32. error: sinon.stub()
  33. }),
  34. './HistoryManager': (this.HistoryManager = {}),
  35. './Metrics': (this.Metrics = {
  36. Timer: (Timer = (function () {
  37. Timer = class Timer {
  38. static initClass() {
  39. this.prototype.done = sinon.stub()
  40. }
  41. }
  42. Timer.initClass()
  43. return Timer
  44. })())
  45. })
  46. }
  47. })
  48. this.project_id = 'project-id-123'
  49. return (this.callback = sinon.stub())
  50. })
  51. describe('successfully', function () {
  52. beforeEach(function (done) {
  53. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  54. this.RedisManager.getDocIdsInProject = sinon
  55. .stub()
  56. .callsArgWith(1, null, this.doc_ids)
  57. this.DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArg(2)
  58. return this.ProjectManager.flushProjectWithLocks(
  59. this.project_id,
  60. (error) => {
  61. this.callback(error)
  62. return done()
  63. }
  64. )
  65. })
  66. it('should get the doc ids in the project', function () {
  67. return this.RedisManager.getDocIdsInProject
  68. .calledWith(this.project_id)
  69. .should.equal(true)
  70. })
  71. it('should flush each doc in the project', function () {
  72. return Array.from(this.doc_ids).map((doc_id) =>
  73. this.DocumentManager.flushDocIfLoadedWithLock
  74. .calledWith(this.project_id, doc_id)
  75. .should.equal(true)
  76. )
  77. })
  78. it('should call the callback without error', function () {
  79. return this.callback.calledWith(null).should.equal(true)
  80. })
  81. return it('should time the execution', function () {
  82. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  83. })
  84. })
  85. return describe('when a doc errors', function () {
  86. beforeEach(function (done) {
  87. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  88. this.RedisManager.getDocIdsInProject = sinon
  89. .stub()
  90. .callsArgWith(1, null, this.doc_ids)
  91. this.DocumentManager.flushDocIfLoadedWithLock = sinon.spy(
  92. (project_id, doc_id, callback) => {
  93. if (callback == null) {
  94. callback = function (error) {}
  95. }
  96. if (doc_id === 'doc-id-1') {
  97. return callback(
  98. (this.error = new Error('oops, something went wrong'))
  99. )
  100. } else {
  101. return callback()
  102. }
  103. }
  104. )
  105. return this.ProjectManager.flushProjectWithLocks(
  106. this.project_id,
  107. (error) => {
  108. this.callback(error)
  109. return done()
  110. }
  111. )
  112. })
  113. it('should still flush each doc in the project', function () {
  114. return Array.from(this.doc_ids).map((doc_id) =>
  115. this.DocumentManager.flushDocIfLoadedWithLock
  116. .calledWith(this.project_id, doc_id)
  117. .should.equal(true)
  118. )
  119. })
  120. it('should record the error', function () {
  121. return this.logger.error
  122. .calledWith(
  123. { err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
  124. 'error flushing doc'
  125. )
  126. .should.equal(true)
  127. })
  128. it('should call the callback with an error', function () {
  129. return this.callback
  130. .calledWith(sinon.match.instanceOf(Error))
  131. .should.equal(true)
  132. })
  133. return it('should time the execution', function () {
  134. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  135. })
  136. })
  137. })