RedisManagerTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../../app/js/RedisManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('RedisManager', function () {
  21. beforeEach(function () {
  22. this.RedisManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. 'redis-sharelatex': {
  25. createClient: () => {
  26. return (this.rclient = {
  27. auth: sinon.stub(),
  28. multi: () => this.rclient
  29. })
  30. }
  31. },
  32. 'settings-sharelatex': {
  33. redis: {
  34. history: {
  35. key_schema: {
  36. uncompressedHistoryOps({ doc_id }) {
  37. return `UncompressedHistoryOps:${doc_id}`
  38. },
  39. docsWithHistoryOps({ project_id }) {
  40. return `DocsWithHistoryOps:${project_id}`
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. })
  48. this.doc_id = 'doc-id-123'
  49. this.project_id = 'project-id-123'
  50. this.batchSize = 100
  51. return (this.callback = sinon.stub())
  52. })
  53. describe('getOldestDocUpdates', function () {
  54. beforeEach(function () {
  55. this.rawUpdates = [
  56. { v: 42, op: 'mock-op-42' },
  57. { v: 45, op: 'mock-op-45' }
  58. ]
  59. this.jsonUpdates = Array.from(this.rawUpdates).map((update) =>
  60. JSON.stringify(update)
  61. )
  62. this.rclient.lrange = sinon.stub().callsArgWith(3, null, this.jsonUpdates)
  63. return this.RedisManager.getOldestDocUpdates(
  64. this.doc_id,
  65. this.batchSize,
  66. this.callback
  67. )
  68. })
  69. it('should read the updates from redis', function () {
  70. return this.rclient.lrange
  71. .calledWith(
  72. `UncompressedHistoryOps:${this.doc_id}`,
  73. 0,
  74. this.batchSize - 1
  75. )
  76. .should.equal(true)
  77. })
  78. it('should call the callback with the unparsed ops', function () {
  79. return this.callback.calledWith(null, this.jsonUpdates).should.equal(true)
  80. })
  81. describe('expandDocUpdates', function () {
  82. beforeEach(function () {
  83. return this.RedisManager.expandDocUpdates(
  84. this.jsonUpdates,
  85. this.callback
  86. )
  87. })
  88. return it('should call the callback with the parsed ops', function () {
  89. return this.callback
  90. .calledWith(null, this.rawUpdates)
  91. .should.equal(true)
  92. })
  93. })
  94. return describe('deleteAppliedDocUpdates', function () {
  95. beforeEach(function () {
  96. this.rclient.lrem = sinon.stub()
  97. this.rclient.srem = sinon.stub()
  98. this.rclient.exec = sinon.stub().callsArgWith(0)
  99. return this.RedisManager.deleteAppliedDocUpdates(
  100. this.project_id,
  101. this.doc_id,
  102. this.jsonUpdates,
  103. this.callback
  104. )
  105. })
  106. it('should delete the first update from redis', function () {
  107. return this.rclient.lrem
  108. .calledWith(
  109. `UncompressedHistoryOps:${this.doc_id}`,
  110. 1,
  111. this.jsonUpdates[0]
  112. )
  113. .should.equal(true)
  114. })
  115. it('should delete the second update from redis', function () {
  116. return this.rclient.lrem
  117. .calledWith(
  118. `UncompressedHistoryOps:${this.doc_id}`,
  119. 1,
  120. this.jsonUpdates[1]
  121. )
  122. .should.equal(true)
  123. })
  124. it('should delete the doc from the set of docs with history ops', function () {
  125. return this.rclient.srem
  126. .calledWith(`DocsWithHistoryOps:${this.project_id}`, this.doc_id)
  127. .should.equal(true)
  128. })
  129. return it('should call the callback ', function () {
  130. return this.callback.called.should.equal(true)
  131. })
  132. })
  133. })
  134. return describe('getDocIdsWithHistoryOps', function () {
  135. beforeEach(function () {
  136. this.doc_ids = ['mock-id-1', 'mock-id-2']
  137. this.rclient.smembers = sinon.stub().callsArgWith(1, null, this.doc_ids)
  138. return this.RedisManager.getDocIdsWithHistoryOps(
  139. this.project_id,
  140. this.callback
  141. )
  142. })
  143. it('should read the doc_ids from redis', function () {
  144. return this.rclient.smembers
  145. .calledWith(`DocsWithHistoryOps:${this.project_id}`)
  146. .should.equal(true)
  147. })
  148. return it('should call the callback with the doc_ids', function () {
  149. return this.callback.calledWith(null, this.doc_ids).should.equal(true)
  150. })
  151. })
  152. })