RedisManagerTests.coffee 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. web:{}
  17. @doc_id = "doc-id-123"
  18. @project_id = "project-id-123"
  19. @batchSize = 100
  20. @callback = sinon.stub()
  21. describe "getOldestRawUpdates", ->
  22. beforeEach ->
  23. @rawUpdates = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
  24. @jsonUpdates = (JSON.stringify(update) for update in @rawUpdates)
  25. @rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonUpdates)
  26. @RedisManager.getOldestRawUpdates @doc_id, @batchSize, @callback
  27. it "should read the updates from redis", ->
  28. @rclient.lrange
  29. .calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @batchSize - 1)
  30. .should.equal true
  31. it "should call the callback with the parsed ops", ->
  32. @callback.calledWith(null, @rawUpdates).should.equal true
  33. describe "deleteOldestRawUpdates", ->
  34. beforeEach ->
  35. @rclient.ltrim = sinon.stub()
  36. @rclient.srem = sinon.stub()
  37. @rclient.exec = sinon.stub().callsArgWith(0)
  38. @RedisManager.deleteOldestRawUpdates @project_id, @doc_id, @batchSize, @callback
  39. it "should delete the updates from redis", ->
  40. @rclient.ltrim
  41. .calledWith("UncompressedHistoryOps:#{@doc_id}", @batchSize, -1)
  42. .should.equal true
  43. it "should delete the doc from the set of docs with history ops", ->
  44. @rclient.srem
  45. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  46. .should.equal true
  47. it "should call the callback ", ->
  48. @callback.called.should.equal true
  49. describe "getDocIdsWithHistoryOps", ->
  50. beforeEach ->
  51. @doc_ids = ["mock-id-1", "mock-id-2"]
  52. @rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
  53. @RedisManager.getDocIdsWithHistoryOps @project_id, @callback
  54. it "should read the doc_ids from redis", ->
  55. @rclient.smembers
  56. .calledWith("DocsWithHistoryOps:#{@project_id}")
  57. .should.equal true
  58. it "should call the callback with the doc_ids", ->
  59. @callback.calledWith(null, @doc_ids).should.equal true