flushProjectTests.js 4.3 KB

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