BlobManagerTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import sinon from 'sinon'
  2. import { strict as esmock } from 'esmock'
  3. const MODULE_PATH = '../../../../app/js/BlobManager.js'
  4. describe('BlobManager', function () {
  5. beforeEach(async function () {
  6. this.callback = sinon.stub()
  7. this.extendLock = sinon.stub().yields()
  8. this.project_id = 'project-1'
  9. this.historyId = 12345
  10. this.HistoryStoreManager = {
  11. createBlobForUpdate: sinon.stub(),
  12. }
  13. this.UpdateTranslator = {
  14. isAddUpdate: sinon.stub().returns(false),
  15. }
  16. this.BlobManager = await esmock(MODULE_PATH, {
  17. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  18. '../../../../app/js/UpdateTranslator.js': this.UpdateTranslator,
  19. })
  20. this.updates = ['update-1', 'update-2']
  21. })
  22. describe('createBlobsForUpdates', function () {
  23. describe('when there are no blobs to create', function () {
  24. beforeEach(function (done) {
  25. this.BlobManager.createBlobsForUpdates(
  26. this.project_id,
  27. this.historyId,
  28. this.updates,
  29. this.extendLock,
  30. (error, updatesWithBlobs) => {
  31. this.callback(error, updatesWithBlobs)
  32. done()
  33. }
  34. )
  35. })
  36. it('should not create any blobs', function () {
  37. this.HistoryStoreManager.createBlobForUpdate.called.should.equal(false)
  38. })
  39. it('should call the callback with the updates', function () {
  40. const updatesWithBlobs = this.updates.map(update => ({
  41. update,
  42. }))
  43. this.callback.calledWith(null, updatesWithBlobs).should.equal(true)
  44. })
  45. })
  46. describe('when there are blobs to create', function () {
  47. beforeEach(function (done) {
  48. this.UpdateTranslator.isAddUpdate.returns(true)
  49. this.blobHash = 'test hash'
  50. this.HistoryStoreManager.createBlobForUpdate.yields(null, {
  51. file: this.blobHash,
  52. })
  53. this.BlobManager.createBlobsForUpdates(
  54. this.project_id,
  55. this.historyId,
  56. this.updates,
  57. this.extendLock,
  58. (error, updatesWithBlobs) => {
  59. this.callback(error, updatesWithBlobs)
  60. done()
  61. }
  62. )
  63. })
  64. it('should create blobs', function () {
  65. this.HistoryStoreManager.createBlobForUpdate
  66. .calledWith(this.project_id, this.historyId, this.updates[0])
  67. .should.equal(true)
  68. })
  69. it('should extend the lock', function () {
  70. this.extendLock.called.should.equal(true)
  71. })
  72. it('should call the callback with the updates', function () {
  73. const updatesWithBlobs = this.updates.map(update => ({
  74. update,
  75. blobHashes: { file: this.blobHash },
  76. }))
  77. this.callback.calledWith(null, updatesWithBlobs).should.equal(true)
  78. })
  79. })
  80. describe('when there are blobs to create and there is a single network error', function () {
  81. beforeEach(function (done) {
  82. this.UpdateTranslator.isAddUpdate.returns(true)
  83. this.blobHash = 'test hash'
  84. this.HistoryStoreManager.createBlobForUpdate
  85. .onFirstCall()
  86. .yields(new Error('random failure'))
  87. this.HistoryStoreManager.createBlobForUpdate.yields(null, {
  88. file: this.blobHash,
  89. })
  90. this.BlobManager.createBlobsForUpdates(
  91. this.project_id,
  92. this.historyId,
  93. this.updates,
  94. this.extendLock,
  95. (error, updatesWithBlobs) => {
  96. this.callback(error, updatesWithBlobs)
  97. done()
  98. }
  99. )
  100. })
  101. it('should create blobs', function () {
  102. this.HistoryStoreManager.createBlobForUpdate
  103. .calledWith(this.project_id, this.historyId, this.updates[0])
  104. .should.equal(true)
  105. })
  106. it('should extend the lock', function () {
  107. this.extendLock.called.should.equal(true)
  108. })
  109. it('should call the callback with the updates', function () {
  110. const updatesWithBlobs = this.updates.map(update => ({
  111. update,
  112. blobHashes: { file: this.blobHash },
  113. }))
  114. this.callback.calledWith(null, updatesWithBlobs).should.equal(true)
  115. })
  116. })
  117. describe('when there are blobs to create and there are multiple network errors', function () {
  118. beforeEach(function (done) {
  119. this.UpdateTranslator.isAddUpdate.returns(true)
  120. this.blobHash = 'test hash'
  121. this.error = new Error('random failure')
  122. this.HistoryStoreManager.createBlobForUpdate.yields(this.error)
  123. this.BlobManager.createBlobsForUpdates(
  124. this.project_id,
  125. this.historyId,
  126. this.updates,
  127. this.extendLock,
  128. (error, updatesWithBlobs) => {
  129. this.callback(error, updatesWithBlobs)
  130. done()
  131. }
  132. )
  133. })
  134. it('should try to create blobs', function () {
  135. this.HistoryStoreManager.createBlobForUpdate
  136. .calledWith(this.project_id, this.historyId, this.updates[0])
  137. .should.equal(true)
  138. })
  139. it('should call the callback with an error', function () {
  140. this.callback.calledWith(this.error).should.equal(true)
  141. })
  142. })
  143. })
  144. })