flushProjectTests.js 4.5 KB

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