WebsocketLoadBalancerTests.coffee 2.6 KB

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