WebsocketLoadBalancerTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @io = {}
  15. @WebsocketLoadBalancer.rclientPub = publish: sinon.stub()
  16. @WebsocketLoadBalancer.rclientSub =
  17. subscribe: sinon.stub()
  18. on: sinon.stub()
  19. @room_id = "room-id"
  20. @message = "message-to-editor"
  21. @payload = ["argument one", 42]
  22. describe "emitToRoom", ->
  23. beforeEach ->
  24. @WebsocketLoadBalancer.emitToRoom(@room_id, @message, @payload...)
  25. it "should publish the message to redis", ->
  26. @WebsocketLoadBalancer.rclientPub.publish
  27. .calledWith("editor-events", JSON.stringify(
  28. room_id: @room_id,
  29. message: @message
  30. payload: @payload
  31. ))
  32. .should.equal true
  33. describe "emitToAll", ->
  34. beforeEach ->
  35. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  36. @WebsocketLoadBalancer.emitToAll @message, @payload...
  37. it "should emit to the room 'all'", ->
  38. @WebsocketLoadBalancer.emitToRoom
  39. .calledWith("all", @message, @payload...)
  40. .should.equal true
  41. describe "listenForEditorEvents", ->
  42. beforeEach ->
  43. @WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  44. @WebsocketLoadBalancer.listenForEditorEvents()
  45. it "should subscribe to the editor-events channel", ->
  46. @WebsocketLoadBalancer.rclientSub.subscribe
  47. .calledWith("editor-events")
  48. .should.equal true
  49. it "should process the events with _processEditorEvent", ->
  50. @WebsocketLoadBalancer.rclientSub.on
  51. .calledWith("message", sinon.match.func)
  52. .should.equal true
  53. describe "_processEditorEvent", ->
  54. describe "with bad JSON", ->
  55. beforeEach ->
  56. @SafeJsonParse.parse = sinon.stub().callsArgWith 1, new Error("oops")
  57. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", "blah")
  58. it "should log an error", ->
  59. @logger.error.called.should.equal true
  60. describe "with a designated room", ->
  61. beforeEach ->
  62. @io.sockets =
  63. in: sinon.stub().returns(emit: @emit = sinon.stub())
  64. data = JSON.stringify
  65. room_id: @room_id
  66. message: @message
  67. payload: @payload
  68. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  69. it "should send the message to all clients in the room", ->
  70. @io.sockets.in
  71. .calledWith(@room_id)
  72. .should.equal true
  73. @emit.calledWith(@message, @payload...).should.equal true
  74. describe "when emitting to all", ->
  75. beforeEach ->
  76. @io.sockets =
  77. emit: @emit = sinon.stub()
  78. data = JSON.stringify
  79. room_id: "all"
  80. message: @message
  81. payload: @payload
  82. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  83. it "should send the message to all clients", ->
  84. @emit.calledWith(@message, @payload...).should.equal true