ChannelManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. 'metrics-sharelatex': (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('some redis error')
  89. this.ChannelManager.getClientMapEntry(this.rclient)
  90. .has('applied-ops:1234567890abcdef')
  91. .should.equal(false)
  92. this.ChannelManager.subscribe(
  93. this.rclient,
  94. 'applied-ops',
  95. '1234567890abcdef'
  96. )
  97. // subscribe is wrapped in Promise, delay other assertions
  98. return setTimeout(done)
  99. }
  100. )
  101. return null
  102. })
  103. it('should have recorded the error', function () {
  104. return expect(
  105. this.metrics.inc.calledWithExactly('subscribe.failed.applied-ops')
  106. ).to.equal(true)
  107. })
  108. it('should subscribe again', function () {
  109. return this.rclient.subscribe.callCount.should.equal(2)
  110. })
  111. return it('should cleanup', function () {
  112. return this.ChannelManager.getClientMapEntry(this.rclient)
  113. .has('applied-ops:1234567890abcdef')
  114. .should.equal(false)
  115. })
  116. })
  117. describe('when subscribe errors and the clientChannelMap entry was replaced', function () {
  118. beforeEach(function (done) {
  119. this.rclient.subscribe = sinon
  120. .stub()
  121. .onFirstCall()
  122. .rejects(new Error('some redis error'))
  123. .onSecondCall()
  124. .resolves()
  125. this.first = this.ChannelManager.subscribe(
  126. this.rclient,
  127. 'applied-ops',
  128. '1234567890abcdef'
  129. )
  130. // ignore error
  131. this.first.catch(() => {})
  132. expect(
  133. this.ChannelManager.getClientMapEntry(this.rclient).get(
  134. 'applied-ops:1234567890abcdef'
  135. )
  136. ).to.equal(this.first)
  137. this.rclient.unsubscribe = sinon.stub().resolves()
  138. this.ChannelManager.unsubscribe(
  139. this.rclient,
  140. 'applied-ops',
  141. '1234567890abcdef'
  142. )
  143. this.second = this.ChannelManager.subscribe(
  144. this.rclient,
  145. 'applied-ops',
  146. '1234567890abcdef'
  147. )
  148. // should get replaced immediately
  149. expect(
  150. this.ChannelManager.getClientMapEntry(this.rclient).get(
  151. 'applied-ops:1234567890abcdef'
  152. )
  153. ).to.equal(this.second)
  154. // let the first subscribe error -> unsubscribe -> subscribe
  155. return setTimeout(done)
  156. })
  157. return it('should cleanup the second subscribePromise', function () {
  158. return expect(
  159. this.ChannelManager.getClientMapEntry(this.rclient).has(
  160. 'applied-ops:1234567890abcdef'
  161. )
  162. ).to.equal(false)
  163. })
  164. })
  165. return describe('when there is an existing subscription for another redis client but not this one', function () {
  166. beforeEach(function (done) {
  167. this.other_rclient.subscribe = sinon.stub().resolves()
  168. this.ChannelManager.subscribe(
  169. this.other_rclient,
  170. 'applied-ops',
  171. '1234567890abcdef'
  172. )
  173. this.rclient.subscribe = sinon.stub().resolves() // discard the original stub
  174. this.ChannelManager.subscribe(
  175. this.rclient,
  176. 'applied-ops',
  177. '1234567890abcdef'
  178. )
  179. return setTimeout(done)
  180. })
  181. return it('should subscribe to the redis channel on this redis client', function () {
  182. return this.rclient.subscribe
  183. .calledWithExactly('applied-ops:1234567890abcdef')
  184. .should.equal(true)
  185. })
  186. })
  187. })
  188. describe('unsubscribe', function () {
  189. describe('when there is no existing subscription for this redis client', function () {
  190. beforeEach(function (done) {
  191. this.rclient.unsubscribe = sinon.stub().resolves()
  192. this.ChannelManager.unsubscribe(
  193. this.rclient,
  194. 'applied-ops',
  195. '1234567890abcdef'
  196. )
  197. return setTimeout(done)
  198. })
  199. return it('should unsubscribe from the redis channel', function () {
  200. return this.rclient.unsubscribe.called.should.equal(true)
  201. })
  202. })
  203. describe('when there is an existing subscription for this another redis client but not this one', function () {
  204. beforeEach(function (done) {
  205. this.other_rclient.subscribe = sinon.stub().resolves()
  206. this.rclient.unsubscribe = sinon.stub().resolves()
  207. this.ChannelManager.subscribe(
  208. this.other_rclient,
  209. 'applied-ops',
  210. '1234567890abcdef'
  211. )
  212. this.ChannelManager.unsubscribe(
  213. this.rclient,
  214. 'applied-ops',
  215. '1234567890abcdef'
  216. )
  217. return setTimeout(done)
  218. })
  219. return it('should still unsubscribe from the redis channel on this client', function () {
  220. return this.rclient.unsubscribe.called.should.equal(true)
  221. })
  222. })
  223. describe('when unsubscribe errors and completes', function () {
  224. beforeEach(function (done) {
  225. this.rclient.subscribe = sinon.stub().resolves()
  226. this.ChannelManager.subscribe(
  227. this.rclient,
  228. 'applied-ops',
  229. '1234567890abcdef'
  230. )
  231. this.rclient.unsubscribe = sinon
  232. .stub()
  233. .rejects(new Error('some redis error'))
  234. this.ChannelManager.unsubscribe(
  235. this.rclient,
  236. 'applied-ops',
  237. '1234567890abcdef'
  238. )
  239. setTimeout(done)
  240. return null
  241. })
  242. it('should have cleaned up', function () {
  243. return this.ChannelManager.getClientMapEntry(this.rclient)
  244. .has('applied-ops:1234567890abcdef')
  245. .should.equal(false)
  246. })
  247. return it('should not error out when subscribing again', function (done) {
  248. const p = this.ChannelManager.subscribe(
  249. this.rclient,
  250. 'applied-ops',
  251. '1234567890abcdef'
  252. )
  253. p.then(() => done()).catch(done)
  254. return null
  255. })
  256. })
  257. describe('when unsubscribe errors and another client subscribes at the same time', function () {
  258. beforeEach(function (done) {
  259. this.rclient.subscribe = sinon.stub().resolves()
  260. this.ChannelManager.subscribe(
  261. this.rclient,
  262. 'applied-ops',
  263. '1234567890abcdef'
  264. )
  265. let rejectSubscribe
  266. this.rclient.unsubscribe = () =>
  267. new Promise((resolve, reject) => (rejectSubscribe = reject))
  268. this.ChannelManager.unsubscribe(
  269. this.rclient,
  270. 'applied-ops',
  271. '1234567890abcdef'
  272. )
  273. setTimeout(() => {
  274. // delay, actualUnsubscribe should not see the new subscribe request
  275. this.ChannelManager.subscribe(
  276. this.rclient,
  277. 'applied-ops',
  278. '1234567890abcdef'
  279. )
  280. .then(() => setTimeout(done))
  281. .catch(done)
  282. return setTimeout(() =>
  283. // delay, rejectSubscribe is not defined immediately
  284. rejectSubscribe(new Error('redis error'))
  285. )
  286. })
  287. return null
  288. })
  289. it('should have recorded the error', function () {
  290. return expect(
  291. this.metrics.inc.calledWithExactly('unsubscribe.failed.applied-ops')
  292. ).to.equal(true)
  293. })
  294. it('should have subscribed', function () {
  295. return this.rclient.subscribe.called.should.equal(true)
  296. })
  297. return it('should have discarded the finished Promise', function () {
  298. return this.ChannelManager.getClientMapEntry(this.rclient)
  299. .has('applied-ops:1234567890abcdef')
  300. .should.equal(false)
  301. })
  302. })
  303. return describe('when there is an existing subscription for this redis client', function () {
  304. beforeEach(function (done) {
  305. this.rclient.subscribe = sinon.stub().resolves()
  306. this.rclient.unsubscribe = sinon.stub().resolves()
  307. this.ChannelManager.subscribe(
  308. this.rclient,
  309. 'applied-ops',
  310. '1234567890abcdef'
  311. )
  312. this.ChannelManager.unsubscribe(
  313. this.rclient,
  314. 'applied-ops',
  315. '1234567890abcdef'
  316. )
  317. return setTimeout(done)
  318. })
  319. return it('should unsubscribe from the redis channel', function () {
  320. return this.rclient.unsubscribe
  321. .calledWithExactly('applied-ops:1234567890abcdef')
  322. .should.equal(true)
  323. })
  324. })
  325. })
  326. return describe('publish', function () {
  327. describe("when the channel is 'all'", function () {
  328. beforeEach(function () {
  329. this.rclient.publish = sinon.stub()
  330. return this.ChannelManager.publish(
  331. this.rclient,
  332. 'applied-ops',
  333. 'all',
  334. 'random-message'
  335. )
  336. })
  337. return it('should publish on the base channel', function () {
  338. return this.rclient.publish
  339. .calledWithExactly('applied-ops', 'random-message')
  340. .should.equal(true)
  341. })
  342. })
  343. describe('when the channel has an specific id', function () {
  344. describe('when the individual channel setting is false', function () {
  345. beforeEach(function () {
  346. this.rclient.publish = sinon.stub()
  347. this.settings.publishOnIndividualChannels = false
  348. return this.ChannelManager.publish(
  349. this.rclient,
  350. 'applied-ops',
  351. '1234567890abcdef',
  352. 'random-message'
  353. )
  354. })
  355. return it('should publish on the per-id channel', function () {
  356. this.rclient.publish
  357. .calledWithExactly('applied-ops', 'random-message')
  358. .should.equal(true)
  359. return this.rclient.publish.calledOnce.should.equal(true)
  360. })
  361. })
  362. return describe('when the individual channel setting is true', function () {
  363. beforeEach(function () {
  364. this.rclient.publish = sinon.stub()
  365. this.settings.publishOnIndividualChannels = true
  366. return this.ChannelManager.publish(
  367. this.rclient,
  368. 'applied-ops',
  369. '1234567890abcdef',
  370. 'random-message'
  371. )
  372. })
  373. return it('should publish on the per-id channel', function () {
  374. this.rclient.publish
  375. .calledWithExactly('applied-ops:1234567890abcdef', 'random-message')
  376. .should.equal(true)
  377. return this.rclient.publish.calledOnce.should.equal(true)
  378. })
  379. })
  380. })
  381. return describe('metrics', function () {
  382. beforeEach(function () {
  383. this.rclient.publish = sinon.stub()
  384. return this.ChannelManager.publish(
  385. this.rclient,
  386. 'applied-ops',
  387. 'all',
  388. 'random-message'
  389. )
  390. })
  391. return it('should track the payload size', function () {
  392. return this.metrics.summary
  393. .calledWithExactly(
  394. 'redis.publish.applied-ops',
  395. 'random-message'.length
  396. )
  397. .should.equal(true)
  398. })
  399. })
  400. })
  401. })