RealTimeRedisManagerTests.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "./Metrics": @metrics = { summary: sinon.stub()}
  28. @doc_id = "doc-id-123"
  29. @project_id = "project-id-123"
  30. @callback = sinon.stub()
  31. describe "getPendingUpdatesForDoc", ->
  32. beforeEach ->
  33. @rclient.lrange = sinon.stub()
  34. @rclient.ltrim = sinon.stub()
  35. describe "successfully", ->
  36. beforeEach ->
  37. @updates = [
  38. { op: [{ i: "foo", p: 4 }] }
  39. { op: [{ i: "foo", p: 4 }] }
  40. ]
  41. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  42. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  43. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  44. it "should get the pending updates", ->
  45. @rclient.lrange
  46. .calledWith("PendingUpdates:#{@doc_id}", 0, 7)
  47. .should.equal true
  48. it "should delete the pending updates", ->
  49. @rclient.ltrim
  50. .calledWith("PendingUpdates:#{@doc_id}", 8, -1)
  51. .should.equal true
  52. it "should call the callback with the updates", ->
  53. @callback.calledWith(null, @updates).should.equal true
  54. describe "when the JSON doesn't parse", ->
  55. beforeEach ->
  56. @jsonUpdates = [
  57. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  58. "broken json"
  59. ]
  60. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  61. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  62. it "should return an error to the callback", ->
  63. @callback.calledWith(new Error("JSON parse error")).should.equal true
  64. describe "getUpdatesLength", ->
  65. beforeEach ->
  66. @rclient.llen = sinon.stub().yields(null, @length = 3)
  67. @RealTimeRedisManager.getUpdatesLength @doc_id, @callback
  68. it "should look up the length", ->
  69. @rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
  70. it "should return the length", ->
  71. @callback.calledWith(null, @length).should.equal true
  72. describe "sendData", ->
  73. beforeEach ->
  74. @message_id = "doc:somehost:01020304-0"
  75. @RealTimeRedisManager.sendData({op: "thisop"})
  76. it "should send the op with a message id", ->
  77. @pubsubClient.publish.calledWith("applied-ops", JSON.stringify({op:"thisop",_id:@message_id})).should.equal true
  78. it "should track the payload size", ->
  79. @metrics.summary.calledWith("redis.publish.applied-ops", JSON.stringify({op:"thisop",_id:@message_id}).length).should.equal true