WebsocketLoadBalancerTests.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. @WebsocketLoadBalancer = SandboxedModule.require modulePath, requires:
  8. "redis-sharelatex":
  9. createClient: () ->
  10. auth:->
  11. "logger-sharelatex": { log: sinon.stub(), err: sinon.stub() }
  12. @io = {}
  13. @WebsocketLoadBalancer.rclientPub = publish: sinon.stub()
  14. @WebsocketLoadBalancer.rclientSub =
  15. subscribe: sinon.stub()
  16. on: sinon.stub()
  17. @room_id = "room-id"
  18. @message = "message-to-editor"
  19. @payload = ["argument one", 42]
  20. describe "emitToRoom", ->
  21. beforeEach ->
  22. @WebsocketLoadBalancer.emitToRoom(@room_id, @message, @payload...)
  23. it "should publish the message to redis", ->
  24. @WebsocketLoadBalancer.rclientPub.publish
  25. .calledWith("editor-events", JSON.stringify(
  26. room_id: @room_id,
  27. message: @message
  28. payload: @payload
  29. ))
  30. .should.equal true
  31. describe "emitToAll", ->
  32. beforeEach ->
  33. @WebsocketLoadBalancer.emitToRoom = sinon.stub()
  34. @WebsocketLoadBalancer.emitToAll @message, @payload...
  35. it "should emit to the room 'all'", ->
  36. @WebsocketLoadBalancer.emitToRoom
  37. .calledWith("all", @message, @payload...)
  38. .should.equal true
  39. describe "listenForEditorEvents", ->
  40. beforeEach ->
  41. @WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  42. @WebsocketLoadBalancer.listenForEditorEvents()
  43. it "should subscribe to the editor-events channel", ->
  44. @WebsocketLoadBalancer.rclientSub.subscribe
  45. .calledWith("editor-events")
  46. .should.equal true
  47. it "should process the events with _processEditorEvent", ->
  48. @WebsocketLoadBalancer.rclientSub.on
  49. .calledWith("message", sinon.match.func)
  50. .should.equal true
  51. describe "_processEditorEvent", ->
  52. describe "with a designated room", ->
  53. beforeEach ->
  54. @io.sockets =
  55. in: sinon.stub().returns(emit: @emit = sinon.stub())
  56. data = JSON.stringify
  57. room_id: @room_id
  58. message: @message
  59. payload: @payload
  60. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  61. it "should send the message to all clients in the room", ->
  62. @io.sockets.in
  63. .calledWith(@room_id)
  64. .should.equal true
  65. @emit.calledWith(@message, @payload...).should.equal true
  66. describe "when emitting to all", ->
  67. beforeEach ->
  68. @io.sockets =
  69. emit: @emit = sinon.stub()
  70. data = JSON.stringify
  71. room_id: "all"
  72. message: @message
  73. payload: @payload
  74. @WebsocketLoadBalancer._processEditorEvent(@io, "editor-events", data)
  75. it "should send the message to all clients", ->
  76. @emit.calledWith(@message, @payload...).should.equal true