RedisManagerTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/RedisManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "RedisManager", ->
  8. beforeEach ->
  9. @RedisManager = SandboxedModule.require modulePath, requires:
  10. "redis-sharelatex" :
  11. createClient: () => @rclient =
  12. auth: sinon.stub()
  13. multi: () => @rclient
  14. "settings-sharelatex":
  15. redis:
  16. history:
  17. key_schema:
  18. uncompressedHistoryOps: ({doc_id}) -> "UncompressedHistoryOps:#{doc_id}"
  19. docsWithHistoryOps: ({project_id}) -> "DocsWithHistoryOps:#{project_id}"
  20. @doc_id = "doc-id-123"
  21. @project_id = "project-id-123"
  22. @batchSize = 100
  23. @callback = sinon.stub()
  24. describe "getOldestDocUpdates", ->
  25. beforeEach ->
  26. @rawUpdates = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
  27. @jsonUpdates = (JSON.stringify(update) for update in @rawUpdates)
  28. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonUpdates)
  29. @RedisManager.getOldestDocUpdates @doc_id, @batchSize, @callback
  30. it "should read the updates from redis", ->
  31. @rclient.lrange
  32. .calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @batchSize - 1)
  33. .should.equal true
  34. it "should call the callback with the unparsed ops", ->
  35. @callback.calledWith(null, @jsonUpdates).should.equal true
  36. describe "expandDocUpdates", ->
  37. beforeEach ->
  38. @RedisManager.expandDocUpdates @jsonUpdates, @callback
  39. it "should call the callback with the parsed ops", ->
  40. @callback.calledWith(null, @rawUpdates).should.equal true
  41. describe "deleteAppliedDocUpdates", ->
  42. beforeEach ->
  43. @rclient.lrem = sinon.stub()
  44. @rclient.srem = sinon.stub()
  45. @rclient.exec = sinon.stub().callsArgWith(0)
  46. @RedisManager.deleteAppliedDocUpdates @project_id, @doc_id, @jsonUpdates, @callback
  47. it "should delete the first update from redis", ->
  48. @rclient.lrem
  49. .calledWith("UncompressedHistoryOps:#{@doc_id}", 1, @jsonUpdates[0])
  50. .should.equal true
  51. it "should delete the second update from redis", ->
  52. @rclient.lrem
  53. .calledWith("UncompressedHistoryOps:#{@doc_id}", 1, @jsonUpdates[1])
  54. .should.equal true
  55. it "should delete the doc from the set of docs with history ops", ->
  56. @rclient.srem
  57. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  58. .should.equal true
  59. it "should call the callback ", ->
  60. @callback.called.should.equal true
  61. describe "getDocIdsWithHistoryOps", ->
  62. beforeEach ->
  63. @doc_ids = ["mock-id-1", "mock-id-2"]
  64. @rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
  65. @RedisManager.getDocIdsWithHistoryOps @project_id, @callback
  66. it "should read the doc_ids from redis", ->
  67. @rclient.smembers
  68. .calledWith("DocsWithHistoryOps:#{@project_id}")
  69. .should.equal true
  70. it "should call the callback with the doc_ids", ->
  71. @callback.calledWith(null, @doc_ids).should.equal true