WebsocketLoadBalancerTests.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/WebsocketLoadBalancer'
  5. describe "WebsocketLoadBalancer", ->
  6. beforeEach ->
  7. @rclient = {}
  8. @WebsocketLoadBalancer = SandboxedModule.require modulePath, requires:
  9. "redis-sharelatex":
  10. createClient: () => @rclient
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. "./SafeJsonParse": @SafeJsonParse =
  13. parse: (data, cb) => cb null, JSON.parse(data)
  14. "./EventLogger": {checkEventOrder: sinon.stub()}
  15. "./HealthCheckManager": {check: sinon.stub()}
  16. @io = {}
  17. @WebsocketLoadBalancer.rclientPub = publish: sinon.stub()
  18. @WebsocketLoadBalancer.rclientSub =
  19. subscribe: sinon.stub()
  20. on: sinon.stub()
  21. @room_id = "room-id"
  22. @message = "message-to-editor"
  23. @payload = ["argument one", 42]
  24. describe "emitToRoom", ->
  25. beforeEach ->
  26. @WebsocketLoadBalancer.emitToRoom(@room_id, @message, @payload...)
  27. it "should publish the message to redis", ->
  28. @WebsocketLoadBalancer.rclientPub.publish
  29. .calledWith("editor-events", JSON.stringify(
  30. room_id: @room_id,
  31. message: @message
  32. payload: @payload
  33. ))
  34. .should.equal true
  35. describe "emitToAll", ->
  36. beforeEach ->
  37. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  38. @WebsocketLoadBalancer.emitToAll @message, @payload...
  39. it "should emit to the room 'all'", ->
  40. @WebsocketLoadBalancer.emitToRoom
  41. .calledWith("all", @message, @payload...)
  42. .should.equal true
  43. describe "listenForEditorEvents", ->
  44. beforeEach ->
  45. @WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  46. @WebsocketLoadBalancer.listenForEditorEvents()
  47. it "should subscribe to the editor-events channel", ->
  48. @WebsocketLoadBalancer.rclientSub.subscribe
  49. .calledWith("editor-events")
  50. .should.equal true
  51. it "should process the events with _processEditorEvent", ->
  52. @WebsocketLoadBalancer.rclientSub.on
  53. .calledWith("message", sinon.match.func)
  54. .should.equal true
  55. describe "_processEditorEvent", ->
  56. describe "with bad JSON", ->
  57. beforeEach ->
  58. @SafeJsonParse.parse = sinon.stub().callsArgWith 1, new Error("oops")
  59. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", "blah")
  60. it "should log an error", ->
  61. @logger.error.called.should.equal true
  62. describe "with a designated room", ->
  63. beforeEach ->
  64. @io.sockets =
  65. clients: sinon.stub().returns([
  66. {id: 'client-id-1', emit: @emit1 = sinon.stub()}
  67. {id: 'client-id-2', emit: @emit2 = sinon.stub()}
  68. {id: 'client-id-1', emit: @emit3 = sinon.stub()} # duplicate client
  69. ])
  70. data = JSON.stringify
  71. room_id: @room_id
  72. message: @message
  73. payload: @payload
  74. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  75. it "should send the message to all (unique) clients in the room", ->
  76. @io.sockets.clients
  77. .calledWith(@room_id)
  78. .should.equal true
  79. @emit1.calledWith(@message, @payload...).should.equal true
  80. @emit2.calledWith(@message, @payload...).should.equal true
  81. @emit3.called.should.equal false # duplicate client should be ignored
  82. describe "when emitting to all", ->
  83. beforeEach ->
  84. @io.sockets =
  85. emit: @emit = sinon.stub()
  86. data = JSON.stringify
  87. room_id: "all"
  88. message: @message
  89. payload: @payload
  90. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  91. it "should send the message to all clients", ->
  92. @emit.calledWith(@message, @payload...).should.equal true