RedisManagerTests.coffee 2.2 KB

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