PubSubRace.js 11 KB

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