PubSubRace.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. import RealTimeClient from './helpers/RealTimeClient.js'
  12. import MockDocUpdaterServer from './helpers/MockDocUpdaterServer.js'
  13. import FixturesManager from './helpers/FixturesManager.js'
  14. import async from 'async'
  15. import settings from '@overleaf/settings'
  16. import redis from '@overleaf/redis-wrapper'
  17. const rclient = redis.createClient(settings.redis.pubsub)
  18. describe('PubSubRace', function () {
  19. before(function (done) {
  20. return MockDocUpdaterServer.run(done)
  21. })
  22. describe('when the client leaves a doc before joinDoc completes', function () {
  23. before(function (done) {
  24. return async.series(
  25. [
  26. cb => {
  27. return FixturesManager.setUpProject(
  28. {
  29. privilegeLevel: 'owner',
  30. project: {
  31. name: 'Test Project',
  32. },
  33. },
  34. (e, { project_id: projectId, user_id: userId }) => {
  35. this.project_id = projectId
  36. this.user_id = userId
  37. return cb()
  38. }
  39. )
  40. },
  41. cb => {
  42. this.clientA = RealTimeClient.connect(
  43. this.project_id,
  44. (error, project, privilegeLevel, protocolVersion) => {
  45. this.project = project
  46. this.privilegeLevel = privilegeLevel
  47. this.protocolVersion = protocolVersion
  48. return cb(error)
  49. }
  50. )
  51. },
  52. cb => {
  53. return FixturesManager.setUpDoc(
  54. this.project_id,
  55. { lines: this.lines, version: this.version, ops: this.ops },
  56. (e, { doc_id: docId }) => {
  57. this.doc_id = docId
  58. return cb(e)
  59. }
  60. )
  61. },
  62. cb => {
  63. this.clientA.emit('joinDoc', this.doc_id, () => {})
  64. // leave before joinDoc completes
  65. return this.clientA.emit('leaveDoc', this.doc_id, cb)
  66. },
  67. cb => {
  68. // wait for subscribe and unsubscribe
  69. return setTimeout(cb, 100)
  70. },
  71. ],
  72. done
  73. )
  74. })
  75. return it('should not subscribe to the applied-ops channels anymore', function (done) {
  76. rclient.pubsub('CHANNELS', (err, resp) => {
  77. if (err) {
  78. return done(err)
  79. }
  80. resp.should.not.include(`applied-ops:${this.doc_id}`)
  81. return done()
  82. })
  83. return null
  84. })
  85. })
  86. describe('when the client emits joinDoc and leaveDoc requests frequently and leaves eventually', function () {
  87. before(function (done) {
  88. return async.series(
  89. [
  90. cb => {
  91. return FixturesManager.setUpProject(
  92. {
  93. privilegeLevel: 'owner',
  94. project: {
  95. name: 'Test Project',
  96. },
  97. },
  98. (e, { project_id: projectId, user_id: userId }) => {
  99. this.project_id = projectId
  100. this.user_id = userId
  101. return cb()
  102. }
  103. )
  104. },
  105. cb => {
  106. this.clientA = RealTimeClient.connect(
  107. this.project_id,
  108. (error, project, privilegeLevel, protocolVersion) => {
  109. this.project = project
  110. this.privilegeLevel = privilegeLevel
  111. this.protocolVersion = protocolVersion
  112. return cb(error)
  113. }
  114. )
  115. },
  116. cb => {
  117. return FixturesManager.setUpDoc(
  118. this.project_id,
  119. { lines: this.lines, version: this.version, ops: this.ops },
  120. (e, { doc_id: docId }) => {
  121. this.doc_id = docId
  122. return cb(e)
  123. }
  124. )
  125. },
  126. cb => {
  127. this.clientA.emit('joinDoc', this.doc_id, () => {})
  128. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  129. this.clientA.emit('joinDoc', this.doc_id, () => {})
  130. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  131. this.clientA.emit('joinDoc', this.doc_id, () => {})
  132. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  133. this.clientA.emit('joinDoc', this.doc_id, () => {})
  134. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  135. this.clientA.emit('joinDoc', this.doc_id, () => {})
  136. return this.clientA.emit('leaveDoc', this.doc_id, cb)
  137. },
  138. cb => {
  139. // wait for subscribe and unsubscribe
  140. return setTimeout(cb, 100)
  141. },
  142. ],
  143. done
  144. )
  145. })
  146. return it('should not subscribe to the applied-ops channels anymore', function (done) {
  147. rclient.pubsub('CHANNELS', (err, resp) => {
  148. if (err) {
  149. return done(err)
  150. }
  151. resp.should.not.include(`applied-ops:${this.doc_id}`)
  152. return done()
  153. })
  154. return null
  155. })
  156. })
  157. describe('when the client emits joinDoc and leaveDoc requests frequently and remains in the doc', function () {
  158. before(function (done) {
  159. return async.series(
  160. [
  161. cb => {
  162. return FixturesManager.setUpProject(
  163. {
  164. privilegeLevel: 'owner',
  165. project: {
  166. name: 'Test Project',
  167. },
  168. },
  169. (e, { project_id: projectId, user_id: userId }) => {
  170. this.project_id = projectId
  171. this.user_id = userId
  172. return cb()
  173. }
  174. )
  175. },
  176. cb => {
  177. this.clientA = RealTimeClient.connect(
  178. this.project_id,
  179. (error, project, privilegeLevel, protocolVersion) => {
  180. this.project = project
  181. this.privilegeLevel = privilegeLevel
  182. this.protocolVersion = protocolVersion
  183. return cb(error)
  184. }
  185. )
  186. },
  187. cb => {
  188. return FixturesManager.setUpDoc(
  189. this.project_id,
  190. { lines: this.lines, version: this.version, ops: this.ops },
  191. (e, { doc_id: docId }) => {
  192. this.doc_id = docId
  193. return cb(e)
  194. }
  195. )
  196. },
  197. cb => {
  198. this.clientA.emit('joinDoc', this.doc_id, () => {})
  199. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  200. this.clientA.emit('joinDoc', this.doc_id, () => {})
  201. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  202. this.clientA.emit('joinDoc', this.doc_id, () => {})
  203. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  204. this.clientA.emit('joinDoc', this.doc_id, () => {})
  205. this.clientA.emit('leaveDoc', this.doc_id, () => {})
  206. return this.clientA.emit('joinDoc', this.doc_id, cb)
  207. },
  208. cb => {
  209. // wait for subscribe and unsubscribe
  210. return setTimeout(cb, 100)
  211. },
  212. ],
  213. done
  214. )
  215. })
  216. return it('should subscribe to the applied-ops channels', function (done) {
  217. rclient.pubsub('CHANNELS', (err, resp) => {
  218. if (err) {
  219. return done(err)
  220. }
  221. resp.should.include(`applied-ops:${this.doc_id}`)
  222. return done()
  223. })
  224. return null
  225. })
  226. })
  227. return describe('when the client disconnects before joinDoc completes', function () {
  228. before(function (done) {
  229. return async.series(
  230. [
  231. cb => {
  232. return FixturesManager.setUpProject(
  233. {
  234. privilegeLevel: 'owner',
  235. project: {
  236. name: 'Test Project',
  237. },
  238. },
  239. (e, { project_id: projectId, user_id: userId }) => {
  240. this.project_id = projectId
  241. this.user_id = userId
  242. return cb()
  243. }
  244. )
  245. },
  246. cb => {
  247. this.clientA = RealTimeClient.connect(
  248. this.project_id,
  249. (error, project, privilegeLevel, protocolVersion) => {
  250. this.project = project
  251. this.privilegeLevel = privilegeLevel
  252. this.protocolVersion = protocolVersion
  253. return cb(error)
  254. }
  255. )
  256. },
  257. cb => {
  258. return FixturesManager.setUpDoc(
  259. this.project_id,
  260. { lines: this.lines, version: this.version, ops: this.ops },
  261. (e, { doc_id: docId }) => {
  262. this.doc_id = docId
  263. return cb(e)
  264. }
  265. )
  266. },
  267. cb => {
  268. let joinDocCompleted = false
  269. this.clientA.emit(
  270. 'joinDoc',
  271. this.doc_id,
  272. () => (joinDocCompleted = true)
  273. )
  274. // leave before joinDoc completes
  275. return setTimeout(
  276. () => {
  277. if (joinDocCompleted) {
  278. return cb(new Error('joinDocCompleted -- lower timeout'))
  279. }
  280. this.clientA.on('disconnect', () => cb())
  281. return this.clientA.disconnect()
  282. },
  283. // socket.io processes joinDoc and disconnect with different delays:
  284. // - joinDoc goes through two process.nextTick
  285. // - disconnect goes through one process.nextTick
  286. // We have to inject the disconnect event into a different event loop
  287. // cycle.
  288. 1
  289. )
  290. },
  291. cb => {
  292. // wait for subscribe and unsubscribe
  293. return setTimeout(cb, 100)
  294. },
  295. ],
  296. done
  297. )
  298. })
  299. it('should not subscribe to the editor-events channels anymore', function (done) {
  300. rclient.pubsub('CHANNELS', (err, resp) => {
  301. if (err) {
  302. return done(err)
  303. }
  304. resp.should.not.include(`editor-events:${this.project_id}`)
  305. return done()
  306. })
  307. return null
  308. })
  309. return it('should not subscribe to the applied-ops channels anymore', function (done) {
  310. rclient.pubsub('CHANNELS', (err, resp) => {
  311. if (err) {
  312. return done(err)
  313. }
  314. resp.should.not.include(`applied-ops:${this.doc_id}`)
  315. return done()
  316. })
  317. return null
  318. })
  319. })
  320. })