WebsocketLoadBalancerTests.coffee 3.8 KB

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