RealTimeRedisManagerTests.coffee 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RealTimeRedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. describe "RealTimeRedisManager", ->
  8. beforeEach ->
  9. @rclient =
  10. auth: () ->
  11. exec: sinon.stub()
  12. @rclient.multi = () => @rclient
  13. @pubsubClient =
  14. publish: sinon.stub()
  15. @RealTimeRedisManager = SandboxedModule.require modulePath, requires:
  16. "redis-sharelatex": createClient: (config) => if (config.name is 'pubsub') then @pubsubClient else @rclient
  17. "settings-sharelatex":
  18. redis:
  19. documentupdater: @settings =
  20. key_schema:
  21. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  22. pubsub:
  23. name: "pubsub"
  24. "logger-sharelatex": { log: () -> }
  25. "crypto": @crypto = { randomBytes: sinon.stub().withArgs(4).returns(Buffer.from([0x1, 0x2, 0x3, 0x4])) }
  26. "os": @os = {hostname: sinon.stub().returns("somehost")}
  27. @doc_id = "doc-id-123"
  28. @project_id = "project-id-123"
  29. @callback = sinon.stub()
  30. describe "getPendingUpdatesForDoc", ->
  31. beforeEach ->
  32. @rclient.lrange = sinon.stub()
  33. @rclient.ltrim = sinon.stub()
  34. describe "successfully", ->
  35. beforeEach ->
  36. @updates = [
  37. { op: [{ i: "foo", p: 4 }] }
  38. { op: [{ i: "foo", p: 4 }] }
  39. ]
  40. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  41. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  42. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  43. it "should get the pending updates", ->
  44. @rclient.lrange
  45. .calledWith("PendingUpdates:#{@doc_id}", 0, 7)
  46. .should.equal true
  47. it "should delete the pending updates", ->
  48. @rclient.ltrim
  49. .calledWith("PendingUpdates:#{@doc_id}", 8, -1)
  50. .should.equal true
  51. it "should call the callback with the updates", ->
  52. @callback.calledWith(null, @updates).should.equal true
  53. describe "when the JSON doesn't parse", ->
  54. beforeEach ->
  55. @jsonUpdates = [
  56. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  57. "broken json"
  58. ]
  59. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  60. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  61. it "should return an error to the callback", ->
  62. @callback.calledWith(new Error("JSON parse error")).should.equal true
  63. describe "getUpdatesLength", ->
  64. beforeEach ->
  65. @rclient.llen = sinon.stub().yields(null, @length = 3)
  66. @RealTimeRedisManager.getUpdatesLength @doc_id, @callback
  67. it "should look up the length", ->
  68. @rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
  69. it "should return the length", ->
  70. @callback.calledWith(null, @length).should.equal true
  71. describe "sendData", ->
  72. beforeEach ->
  73. @message_id = "doc:somehost:01020304-0"
  74. @RealTimeRedisManager.sendData({op: "thisop"})
  75. it "should send the op with a message id", ->
  76. @pubsubClient.publish.calledWith("applied-ops", JSON.stringify({op:"thisop",_id:@message_id})).should.equal true