ChannelManagerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  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 chai = require('chai')
  13. const should = chai.should()
  14. const { expect } = chai
  15. const sinon = require('sinon')
  16. const modulePath = '../../../app/js/ChannelManager.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('ChannelManager', function () {
  19. beforeEach(function () {
  20. this.rclient = {}
  21. this.other_rclient = {}
  22. return (this.ChannelManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. 'settings-sharelatex': (this.settings = {}),
  25. '@overleaf/metrics': (this.metrics = {
  26. inc: sinon.stub(),
  27. summary: sinon.stub()
  28. }),
  29. 'logger-sharelatex': (this.logger = {
  30. log: sinon.stub(),
  31. warn: sinon.stub(),
  32. error: sinon.stub()
  33. })
  34. }
  35. }))
  36. })
  37. describe('subscribe', function () {
  38. describe('when there is no existing subscription for this redis client', function () {
  39. beforeEach(function (done) {
  40. this.rclient.subscribe = sinon.stub().resolves()
  41. this.ChannelManager.subscribe(
  42. this.rclient,
  43. 'applied-ops',
  44. '1234567890abcdef'
  45. )
  46. return setTimeout(done)
  47. })
  48. return it('should subscribe to the redis channel', function () {
  49. return this.rclient.subscribe
  50. .calledWithExactly('applied-ops:1234567890abcdef')
  51. .should.equal(true)
  52. })
  53. })
  54. describe('when there is an existing subscription for this redis client', function () {
  55. beforeEach(function (done) {
  56. this.rclient.subscribe = sinon.stub().resolves()
  57. this.ChannelManager.subscribe(
  58. this.rclient,
  59. 'applied-ops',
  60. '1234567890abcdef'
  61. )
  62. this.ChannelManager.subscribe(
  63. this.rclient,
  64. 'applied-ops',
  65. '1234567890abcdef'
  66. )
  67. return setTimeout(done)
  68. })
  69. return it('should subscribe to the redis channel again', function () {
  70. return this.rclient.subscribe.callCount.should.equal(2)
  71. })
  72. })
  73. describe('when subscribe errors', function () {
  74. beforeEach(function (done) {
  75. this.rclient.subscribe = sinon
  76. .stub()
  77. .onFirstCall()
  78. .rejects(new Error('some redis error'))
  79. .onSecondCall()
  80. .resolves()
  81. const p = this.ChannelManager.subscribe(
  82. this.rclient,
  83. 'applied-ops',
  84. '1234567890abcdef'
  85. )
  86. p.then(() => done(new Error('should not subscribe but fail'))).catch(
  87. (err) => {
  88. err.message.should.equal('failed to subscribe to channel')
  89. err.cause.message.should.equal('some redis error')
  90. this.ChannelManager.getClientMapEntry(this.rclient)
  91. .has('applied-ops:1234567890abcdef')
  92. .should.equal(false)
  93. this.ChannelManager.subscribe(
  94. this.rclient,
  95. 'applied-ops',
  96. '1234567890abcdef'
  97. )
  98. // subscribe is wrapped in Promise, delay other assertions
  99. return setTimeout(done)
  100. }
  101. )
  102. return null
  103. })
  104. it('should have recorded the error', function () {
  105. return expect(
  106. this.metrics.inc.calledWithExactly('subscribe.failed.applied-ops')
  107. ).to.equal(true)
  108. })
  109. it('should subscribe again', function () {
  110. return this.rclient.subscribe.callCount.should.equal(2)
  111. })
  112. return it('should cleanup', function () {
  113. return this.ChannelManager.getClientMapEntry(this.rclient)
  114. .has('applied-ops:1234567890abcdef')
  115. .should.equal(false)
  116. })
  117. })
  118. describe('when subscribe errors and the clientChannelMap entry was replaced', function () {
  119. beforeEach(function (done) {
  120. this.rclient.subscribe = sinon
  121. .stub()
  122. .onFirstCall()
  123. .rejects(new Error('some redis error'))
  124. .onSecondCall()
  125. .resolves()
  126. this.first = this.ChannelManager.subscribe(
  127. this.rclient,
  128. 'applied-ops',
  129. '1234567890abcdef'
  130. )
  131. // ignore error
  132. this.first.catch(() => {})
  133. expect(
  134. this.ChannelManager.getClientMapEntry(this.rclient).get(
  135. 'applied-ops:1234567890abcdef'
  136. )
  137. ).to.equal(this.first)
  138. this.rclient.unsubscribe = sinon.stub().resolves()
  139. this.ChannelManager.unsubscribe(
  140. this.rclient,
  141. 'applied-ops',
  142. '1234567890abcdef'
  143. )
  144. this.second = this.ChannelManager.subscribe(
  145. this.rclient,
  146. 'applied-ops',
  147. '1234567890abcdef'
  148. )
  149. // should get replaced immediately
  150. expect(
  151. this.ChannelManager.getClientMapEntry(this.rclient).get(
  152. 'applied-ops:1234567890abcdef'
  153. )
  154. ).to.equal(this.second)
  155. // let the first subscribe error -> unsubscribe -> subscribe
  156. return setTimeout(done)
  157. })
  158. return it('should cleanup the second subscribePromise', function () {
  159. return expect(
  160. this.ChannelManager.getClientMapEntry(this.rclient).has(
  161. 'applied-ops:1234567890abcdef'
  162. )
  163. ).to.equal(false)
  164. })
  165. })
  166. return describe('when there is an existing subscription for another redis client but not this one', function () {
  167. beforeEach(function (done) {
  168. this.other_rclient.subscribe = sinon.stub().resolves()
  169. this.ChannelManager.subscribe(
  170. this.other_rclient,
  171. 'applied-ops',
  172. '1234567890abcdef'
  173. )
  174. this.rclient.subscribe = sinon.stub().resolves() // discard the original stub
  175. this.ChannelManager.subscribe(
  176. this.rclient,
  177. 'applied-ops',
  178. '1234567890abcdef'
  179. )
  180. return setTimeout(done)
  181. })
  182. return it('should subscribe to the redis channel on this redis client', function () {
  183. return this.rclient.subscribe
  184. .calledWithExactly('applied-ops:1234567890abcdef')
  185. .should.equal(true)
  186. })
  187. })
  188. })
  189. describe('unsubscribe', function () {
  190. describe('when there is no existing subscription for this redis client', function () {
  191. beforeEach(function (done) {
  192. this.rclient.unsubscribe = sinon.stub().resolves()
  193. this.ChannelManager.unsubscribe(
  194. this.rclient,
  195. 'applied-ops',
  196. '1234567890abcdef'
  197. )
  198. return setTimeout(done)
  199. })
  200. return it('should unsubscribe from the redis channel', function () {
  201. return this.rclient.unsubscribe.called.should.equal(true)
  202. })
  203. })
  204. describe('when there is an existing subscription for this another redis client but not this one', function () {
  205. beforeEach(function (done) {
  206. this.other_rclient.subscribe = sinon.stub().resolves()
  207. this.rclient.unsubscribe = sinon.stub().resolves()
  208. this.ChannelManager.subscribe(
  209. this.other_rclient,
  210. 'applied-ops',
  211. '1234567890abcdef'
  212. )
  213. this.ChannelManager.unsubscribe(
  214. this.rclient,
  215. 'applied-ops',
  216. '1234567890abcdef'
  217. )
  218. return setTimeout(done)
  219. })
  220. return it('should still unsubscribe from the redis channel on this client', function () {
  221. return this.rclient.unsubscribe.called.should.equal(true)
  222. })
  223. })
  224. describe('when unsubscribe errors and completes', function () {
  225. beforeEach(function (done) {
  226. this.rclient.subscribe = sinon.stub().resolves()
  227. this.ChannelManager.subscribe(
  228. this.rclient,
  229. 'applied-ops',
  230. '1234567890abcdef'
  231. )
  232. this.rclient.unsubscribe = sinon
  233. .stub()
  234. .rejects(new Error('some redis error'))
  235. this.ChannelManager.unsubscribe(
  236. this.rclient,
  237. 'applied-ops',
  238. '1234567890abcdef'
  239. )
  240. setTimeout(done)
  241. return null
  242. })
  243. it('should have cleaned up', function () {
  244. return this.ChannelManager.getClientMapEntry(this.rclient)
  245. .has('applied-ops:1234567890abcdef')
  246. .should.equal(false)
  247. })
  248. return it('should not error out when subscribing again', function (done) {
  249. const p = this.ChannelManager.subscribe(
  250. this.rclient,
  251. 'applied-ops',
  252. '1234567890abcdef'
  253. )
  254. p.then(() => done()).catch(done)
  255. return null
  256. })
  257. })
  258. describe('when unsubscribe errors and another client subscribes at the same time', function () {
  259. beforeEach(function (done) {
  260. this.rclient.subscribe = sinon.stub().resolves()
  261. this.ChannelManager.subscribe(
  262. this.rclient,
  263. 'applied-ops',
  264. '1234567890abcdef'
  265. )
  266. let rejectSubscribe
  267. this.rclient.unsubscribe = () =>
  268. new Promise((resolve, reject) => (rejectSubscribe = reject))
  269. this.ChannelManager.unsubscribe(
  270. this.rclient,
  271. 'applied-ops',
  272. '1234567890abcdef'
  273. )
  274. setTimeout(() => {
  275. // delay, actualUnsubscribe should not see the new subscribe request
  276. this.ChannelManager.subscribe(
  277. this.rclient,
  278. 'applied-ops',
  279. '1234567890abcdef'
  280. )
  281. .then(() => setTimeout(done))
  282. .catch(done)
  283. return setTimeout(() =>
  284. // delay, rejectSubscribe is not defined immediately
  285. rejectSubscribe(new Error('redis error'))
  286. )
  287. })
  288. return null
  289. })
  290. it('should have recorded the error', function () {
  291. return expect(
  292. this.metrics.inc.calledWithExactly('unsubscribe.failed.applied-ops')
  293. ).to.equal(true)
  294. })
  295. it('should have subscribed', function () {
  296. return this.rclient.subscribe.called.should.equal(true)
  297. })
  298. return it('should have discarded the finished Promise', function () {
  299. return this.ChannelManager.getClientMapEntry(this.rclient)
  300. .has('applied-ops:1234567890abcdef')
  301. .should.equal(false)
  302. })
  303. })
  304. return describe('when there is an existing subscription for this redis client', function () {
  305. beforeEach(function (done) {
  306. this.rclient.subscribe = sinon.stub().resolves()
  307. this.rclient.unsubscribe = sinon.stub().resolves()
  308. this.ChannelManager.subscribe(
  309. this.rclient,
  310. 'applied-ops',
  311. '1234567890abcdef'
  312. )
  313. this.ChannelManager.unsubscribe(
  314. this.rclient,
  315. 'applied-ops',
  316. '1234567890abcdef'
  317. )
  318. return setTimeout(done)
  319. })
  320. return it('should unsubscribe from the redis channel', function () {
  321. return this.rclient.unsubscribe
  322. .calledWithExactly('applied-ops:1234567890abcdef')
  323. .should.equal(true)
  324. })
  325. })
  326. })
  327. return describe('publish', function () {
  328. describe("when the channel is 'all'", function () {
  329. beforeEach(function () {
  330. this.rclient.publish = sinon.stub()
  331. return this.ChannelManager.publish(
  332. this.rclient,
  333. 'applied-ops',
  334. 'all',
  335. 'random-message'
  336. )
  337. })
  338. return it('should publish on the base channel', function () {
  339. return this.rclient.publish
  340. .calledWithExactly('applied-ops', 'random-message')
  341. .should.equal(true)
  342. })
  343. })
  344. describe('when the channel has an specific id', function () {
  345. describe('when the individual channel setting is false', function () {
  346. beforeEach(function () {
  347. this.rclient.publish = sinon.stub()
  348. this.settings.publishOnIndividualChannels = false
  349. return this.ChannelManager.publish(
  350. this.rclient,
  351. 'applied-ops',
  352. '1234567890abcdef',
  353. 'random-message'
  354. )
  355. })
  356. return it('should publish on the per-id channel', function () {
  357. this.rclient.publish
  358. .calledWithExactly('applied-ops', 'random-message')
  359. .should.equal(true)
  360. return this.rclient.publish.calledOnce.should.equal(true)
  361. })
  362. })
  363. return describe('when the individual channel setting is true', function () {
  364. beforeEach(function () {
  365. this.rclient.publish = sinon.stub()
  366. this.settings.publishOnIndividualChannels = true
  367. return this.ChannelManager.publish(
  368. this.rclient,
  369. 'applied-ops',
  370. '1234567890abcdef',
  371. 'random-message'
  372. )
  373. })
  374. return it('should publish on the per-id channel', function () {
  375. this.rclient.publish
  376. .calledWithExactly('applied-ops:1234567890abcdef', 'random-message')
  377. .should.equal(true)
  378. return this.rclient.publish.calledOnce.should.equal(true)
  379. })
  380. })
  381. })
  382. return describe('metrics', function () {
  383. beforeEach(function () {
  384. this.rclient.publish = sinon.stub()
  385. return this.ChannelManager.publish(
  386. this.rclient,
  387. 'applied-ops',
  388. 'all',
  389. 'random-message'
  390. )
  391. })
  392. return it('should track the payload size', function () {
  393. return this.metrics.summary
  394. .calledWithExactly(
  395. 'redis.publish.applied-ops',
  396. 'random-message'.length
  397. )
  398. .should.equal(true)
  399. })
  400. })
  401. })
  402. })