WebsocketLoadBalancerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/WebsocketLoadBalancer'
  17. )
  18. describe('WebsocketLoadBalancer', function () {
  19. beforeEach(function () {
  20. this.rclient = {}
  21. this.RoomEvents = { on: sinon.stub() }
  22. this.WebsocketLoadBalancer = SandboxedModule.require(modulePath, {
  23. requires: {
  24. './RedisClientManager': {
  25. createClientList: () => [],
  26. },
  27. './SafeJsonParse': (this.SafeJsonParse = {
  28. parse: (data, cb) => cb(null, JSON.parse(data)),
  29. }),
  30. './EventLogger': { checkEventOrder: sinon.stub() },
  31. './HealthCheckManager': { check: sinon.stub() },
  32. './RoomManager': (this.RoomManager = {
  33. eventSource: sinon.stub().returns(this.RoomEvents),
  34. }),
  35. './ChannelManager': (this.ChannelManager = { publish: sinon.stub() }),
  36. './ConnectedUsersManager': (this.ConnectedUsersManager = {
  37. refreshClient: sinon.stub(),
  38. }),
  39. },
  40. })
  41. this.io = {}
  42. this.WebsocketLoadBalancer.rclientPubList = [{ publish: sinon.stub() }]
  43. this.WebsocketLoadBalancer.rclientSubList = [
  44. {
  45. subscribe: sinon.stub(),
  46. on: sinon.stub(),
  47. },
  48. ]
  49. this.room_id = 'room-id'
  50. this.message = 'otUpdateApplied'
  51. return (this.payload = ['argument one', 42])
  52. })
  53. describe('emitToRoom', function () {
  54. beforeEach(function () {
  55. return this.WebsocketLoadBalancer.emitToRoom(
  56. this.room_id,
  57. this.message,
  58. ...Array.from(this.payload)
  59. )
  60. })
  61. return it('should publish the message to redis', function () {
  62. return this.ChannelManager.publish
  63. .calledWith(
  64. this.WebsocketLoadBalancer.rclientPubList[0],
  65. 'editor-events',
  66. this.room_id,
  67. JSON.stringify({
  68. room_id: this.room_id,
  69. message: this.message,
  70. payload: this.payload,
  71. })
  72. )
  73. .should.equal(true)
  74. })
  75. })
  76. describe('emitToAll', function () {
  77. beforeEach(function () {
  78. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  79. return this.WebsocketLoadBalancer.emitToAll(
  80. this.message,
  81. ...Array.from(this.payload)
  82. )
  83. })
  84. return it("should emit to the room 'all'", function () {
  85. return this.WebsocketLoadBalancer.emitToRoom
  86. .calledWith('all', this.message, ...Array.from(this.payload))
  87. .should.equal(true)
  88. })
  89. })
  90. describe('listenForEditorEvents', function () {
  91. beforeEach(function () {
  92. this.WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  93. return this.WebsocketLoadBalancer.listenForEditorEvents()
  94. })
  95. it('should subscribe to the editor-events channel', function () {
  96. return this.WebsocketLoadBalancer.rclientSubList[0].subscribe
  97. .calledWith('editor-events')
  98. .should.equal(true)
  99. })
  100. return it('should process the events with _processEditorEvent', function () {
  101. return this.WebsocketLoadBalancer.rclientSubList[0].on
  102. .calledWith('message', sinon.match.func)
  103. .should.equal(true)
  104. })
  105. })
  106. return describe('_processEditorEvent', function () {
  107. describe('with bad JSON', function () {
  108. beforeEach(function () {
  109. this.isRestrictedUser = false
  110. this.SafeJsonParse.parse = sinon
  111. .stub()
  112. .callsArgWith(1, new Error('oops'))
  113. return this.WebsocketLoadBalancer._processEditorEvent(
  114. this.io,
  115. 'editor-events',
  116. 'blah'
  117. )
  118. })
  119. return it('should log an error', function () {
  120. return this.logger.error.called.should.equal(true)
  121. })
  122. })
  123. describe('with a designated room', function () {
  124. beforeEach(function () {
  125. this.io.sockets = {
  126. clients: sinon.stub().returns([
  127. {
  128. id: 'client-id-1',
  129. emit: (this.emit1 = sinon.stub()),
  130. ol_context: {},
  131. },
  132. {
  133. id: 'client-id-2',
  134. emit: (this.emit2 = sinon.stub()),
  135. ol_context: {},
  136. },
  137. {
  138. id: 'client-id-1',
  139. emit: (this.emit3 = sinon.stub()),
  140. ol_context: {},
  141. }, // duplicate client
  142. ]),
  143. }
  144. const data = JSON.stringify({
  145. room_id: this.room_id,
  146. message: this.message,
  147. payload: this.payload,
  148. })
  149. return this.WebsocketLoadBalancer._processEditorEvent(
  150. this.io,
  151. 'editor-events',
  152. data
  153. )
  154. })
  155. return it('should send the message to all (unique) clients in the room', function () {
  156. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  157. this.emit1
  158. .calledWith(this.message, ...Array.from(this.payload))
  159. .should.equal(true)
  160. this.emit2
  161. .calledWith(this.message, ...Array.from(this.payload))
  162. .should.equal(true)
  163. return this.emit3.called.should.equal(false)
  164. })
  165. }) // duplicate client should be ignored
  166. describe('with a designated room, and restricted clients, not restricted message', function () {
  167. beforeEach(function () {
  168. this.io.sockets = {
  169. clients: sinon.stub().returns([
  170. {
  171. id: 'client-id-1',
  172. emit: (this.emit1 = sinon.stub()),
  173. ol_context: {},
  174. },
  175. {
  176. id: 'client-id-2',
  177. emit: (this.emit2 = sinon.stub()),
  178. ol_context: {},
  179. },
  180. {
  181. id: 'client-id-1',
  182. emit: (this.emit3 = sinon.stub()),
  183. ol_context: {},
  184. }, // duplicate client
  185. {
  186. id: 'client-id-4',
  187. emit: (this.emit4 = sinon.stub()),
  188. ol_context: { is_restricted_user: true },
  189. },
  190. ]),
  191. }
  192. const data = JSON.stringify({
  193. room_id: this.room_id,
  194. message: this.message,
  195. payload: this.payload,
  196. })
  197. return this.WebsocketLoadBalancer._processEditorEvent(
  198. this.io,
  199. 'editor-events',
  200. data
  201. )
  202. })
  203. return it('should send the message to all (unique) clients in the room', function () {
  204. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  205. this.emit1
  206. .calledWith(this.message, ...Array.from(this.payload))
  207. .should.equal(true)
  208. this.emit2
  209. .calledWith(this.message, ...Array.from(this.payload))
  210. .should.equal(true)
  211. this.emit3.called.should.equal(false) // duplicate client should be ignored
  212. return this.emit4.called.should.equal(true)
  213. })
  214. }) // restricted client, but should be called
  215. describe('with a designated room, and restricted clients, restricted message', function () {
  216. beforeEach(function () {
  217. this.io.sockets = {
  218. clients: sinon.stub().returns([
  219. {
  220. id: 'client-id-1',
  221. emit: (this.emit1 = sinon.stub()),
  222. ol_context: {},
  223. },
  224. {
  225. id: 'client-id-2',
  226. emit: (this.emit2 = sinon.stub()),
  227. ol_context: {},
  228. },
  229. {
  230. id: 'client-id-1',
  231. emit: (this.emit3 = sinon.stub()),
  232. ol_context: {},
  233. }, // duplicate client
  234. {
  235. id: 'client-id-4',
  236. emit: (this.emit4 = sinon.stub()),
  237. ol_context: { is_restricted_user: true },
  238. },
  239. ]),
  240. }
  241. const data = JSON.stringify({
  242. room_id: this.room_id,
  243. message: (this.restrictedMessage = 'new-comment'),
  244. payload: this.payload,
  245. })
  246. return this.WebsocketLoadBalancer._processEditorEvent(
  247. this.io,
  248. 'editor-events',
  249. data
  250. )
  251. })
  252. return it('should send the message to all (unique) clients in the room, who are not restricted', function () {
  253. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  254. this.emit1
  255. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  256. .should.equal(true)
  257. this.emit2
  258. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  259. .should.equal(true)
  260. this.emit3.called.should.equal(false) // duplicate client should be ignored
  261. return this.emit4.called.should.equal(false)
  262. })
  263. }) // restricted client, should not be called
  264. return describe('when emitting to all', function () {
  265. beforeEach(function () {
  266. this.io.sockets = { emit: (this.emit = sinon.stub()) }
  267. const data = JSON.stringify({
  268. room_id: 'all',
  269. message: this.message,
  270. payload: this.payload,
  271. })
  272. return this.WebsocketLoadBalancer._processEditorEvent(
  273. this.io,
  274. 'editor-events',
  275. data
  276. )
  277. })
  278. return it('should send the message to all clients', function () {
  279. return this.emit
  280. .calledWith(this.message, ...Array.from(this.payload))
  281. .should.equal(true)
  282. })
  283. })
  284. })
  285. })