PubSubRace.js 11 KB

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