BlobManagerTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, this.blobHash)
  51. this.BlobManager.createBlobsForUpdates(
  52. this.project_id,
  53. this.historyId,
  54. this.updates,
  55. this.extendLock,
  56. (error, updatesWithBlobs) => {
  57. this.callback(error, updatesWithBlobs)
  58. done()
  59. }
  60. )
  61. })
  62. it('should create blobs', function () {
  63. this.HistoryStoreManager.createBlobForUpdate
  64. .calledWith(this.project_id, this.historyId, this.updates[0])
  65. .should.equal(true)
  66. })
  67. it('should extend the lock', function () {
  68. this.extendLock.called.should.equal(true)
  69. })
  70. it('should call the callback with the updates', function () {
  71. const updatesWithBlobs = this.updates.map(update => ({
  72. update,
  73. blobHash: this.blobHash,
  74. }))
  75. this.callback.calledWith(null, updatesWithBlobs).should.equal(true)
  76. })
  77. })
  78. describe('when there are blobs to create and there is a single network error', function () {
  79. beforeEach(function (done) {
  80. this.UpdateTranslator.isAddUpdate.returns(true)
  81. this.blobHash = 'test hash'
  82. this.HistoryStoreManager.createBlobForUpdate
  83. .onFirstCall()
  84. .yields(new Error('random failure'))
  85. this.HistoryStoreManager.createBlobForUpdate.yields(null, this.blobHash)
  86. this.BlobManager.createBlobsForUpdates(
  87. this.project_id,
  88. this.historyId,
  89. this.updates,
  90. this.extendLock,
  91. (error, updatesWithBlobs) => {
  92. this.callback(error, updatesWithBlobs)
  93. done()
  94. }
  95. )
  96. })
  97. it('should create blobs', function () {
  98. this.HistoryStoreManager.createBlobForUpdate
  99. .calledWith(this.project_id, this.historyId, this.updates[0])
  100. .should.equal(true)
  101. })
  102. it('should extend the lock', function () {
  103. this.extendLock.called.should.equal(true)
  104. })
  105. it('should call the callback with the updates', function () {
  106. const updatesWithBlobs = this.updates.map(update => ({
  107. update,
  108. blobHash: this.blobHash,
  109. }))
  110. this.callback.calledWith(null, updatesWithBlobs).should.equal(true)
  111. })
  112. })
  113. describe('when there are blobs to create and there are multiple network errors', function () {
  114. beforeEach(function (done) {
  115. this.UpdateTranslator.isAddUpdate.returns(true)
  116. this.blobHash = 'test hash'
  117. this.error = new Error('random failure')
  118. this.HistoryStoreManager.createBlobForUpdate.yields(this.error)
  119. this.BlobManager.createBlobsForUpdates(
  120. this.project_id,
  121. this.historyId,
  122. this.updates,
  123. this.extendLock,
  124. (error, updatesWithBlobs) => {
  125. this.callback(error, updatesWithBlobs)
  126. done()
  127. }
  128. )
  129. })
  130. it('should try to create blobs', function () {
  131. this.HistoryStoreManager.createBlobForUpdate
  132. .calledWith(this.project_id, this.historyId, this.updates[0])
  133. .should.equal(true)
  134. })
  135. it('should call the callback with an error', function () {
  136. this.callback.calledWith(this.error).should.equal(true)
  137. })
  138. })
  139. })
  140. })