EarlyDisconnect.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * DS101: Remove unnecessary use of Array.from
  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 async = require('async')
  13. const { expect } = require('chai')
  14. const RealTimeClient = require('./helpers/RealTimeClient')
  15. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  16. const MockWebServer = require('./helpers/MockWebServer')
  17. const FixturesManager = require('./helpers/FixturesManager')
  18. const settings = require('@overleaf/settings')
  19. const redis = require('@overleaf/redis-wrapper')
  20. const rclient = redis.createClient(settings.redis.pubsub)
  21. const rclientRT = redis.createClient(settings.redis.realtime)
  22. const KeysRT = settings.redis.realtime.key_schema
  23. describe('EarlyDisconnect', function () {
  24. before(function (done) {
  25. return MockDocUpdaterServer.run(done)
  26. })
  27. describe('when the client disconnects before joinProject completes', function () {
  28. before(function () {
  29. // slow down web-api requests to force the race condition
  30. let joinProject
  31. this.actualWebAPIjoinProject = joinProject = MockWebServer.joinProject
  32. return (MockWebServer.joinProject = (projectId, userId, cb) =>
  33. setTimeout(() => joinProject(projectId, userId, cb), 300))
  34. })
  35. after(function () {
  36. return (MockWebServer.joinProject = this.actualWebAPIjoinProject)
  37. })
  38. beforeEach(function (done) {
  39. return async.series(
  40. [
  41. cb => {
  42. return FixturesManager.setUpProject(
  43. {
  44. privilegeLevel: 'owner',
  45. project: {
  46. name: 'Test Project',
  47. },
  48. },
  49. (e, { project_id: projectId, user_id: userId }) => {
  50. this.project_id = projectId
  51. this.user_id = userId
  52. return cb()
  53. }
  54. )
  55. },
  56. cb => {
  57. this.clientA = RealTimeClient.connect()
  58. return this.clientA.on('connectionAccepted', cb)
  59. },
  60. cb => {
  61. this.clientA.emit(
  62. 'joinProject',
  63. { project_id: this.project_id },
  64. () => {}
  65. )
  66. // disconnect before joinProject completes
  67. this.clientA.on('disconnect', () => cb())
  68. return this.clientA.disconnect()
  69. },
  70. cb => {
  71. // wait for joinDoc and subscribe
  72. return setTimeout(cb, 500)
  73. },
  74. ],
  75. done
  76. )
  77. })
  78. // we can force the race condition, there is no need to repeat too often
  79. return Array.from(Array.from({ length: 5 }).map((_, i) => i + 1)).map(
  80. attempt =>
  81. it(`should not subscribe to the pub/sub channel anymore (race ${attempt})`, function (done) {
  82. rclient.pubsub('CHANNELS', (err, resp) => {
  83. if (err) {
  84. return done(err)
  85. }
  86. expect(resp).to.not.include(`editor-events:${this.project_id}`)
  87. return done()
  88. })
  89. return null
  90. })
  91. )
  92. })
  93. describe('when the client disconnects before joinDoc completes', function () {
  94. beforeEach(function (done) {
  95. return async.series(
  96. [
  97. cb => {
  98. return FixturesManager.setUpProject(
  99. {
  100. privilegeLevel: 'owner',
  101. project: {
  102. name: 'Test Project',
  103. },
  104. },
  105. (e, { project_id: projectId, user_id: userId }) => {
  106. this.project_id = projectId
  107. this.user_id = userId
  108. return cb()
  109. }
  110. )
  111. },
  112. cb => {
  113. this.clientA = RealTimeClient.connect()
  114. return this.clientA.on('connectionAccepted', cb)
  115. },
  116. cb => {
  117. return this.clientA.emit(
  118. 'joinProject',
  119. { project_id: this.project_id },
  120. (error, project, privilegeLevel, protocolVersion) => {
  121. this.project = project
  122. this.privilegeLevel = privilegeLevel
  123. this.protocolVersion = protocolVersion
  124. return cb(error)
  125. }
  126. )
  127. },
  128. cb => {
  129. return FixturesManager.setUpDoc(
  130. this.project_id,
  131. { lines: this.lines, version: this.version, ops: this.ops },
  132. (e, { doc_id: docId }) => {
  133. this.doc_id = docId
  134. return cb(e)
  135. }
  136. )
  137. },
  138. cb => {
  139. this.clientA.emit('joinDoc', this.doc_id, () => {})
  140. // disconnect before joinDoc completes
  141. this.clientA.on('disconnect', () => cb())
  142. return this.clientA.disconnect()
  143. },
  144. cb => {
  145. // wait for subscribe and unsubscribe
  146. return setTimeout(cb, 100)
  147. },
  148. ],
  149. done
  150. )
  151. })
  152. // we can not force the race condition, so we have to try many times
  153. return Array.from(Array.from({ length: 20 }).map((_, i) => i + 1)).map(
  154. attempt =>
  155. it(`should not subscribe to the pub/sub channels anymore (race ${attempt})`, function (done) {
  156. rclient.pubsub('CHANNELS', (err, resp) => {
  157. if (err) {
  158. return done(err)
  159. }
  160. expect(resp).to.not.include(`editor-events:${this.project_id}`)
  161. return rclient.pubsub('CHANNELS', (err, resp) => {
  162. if (err) {
  163. return done(err)
  164. }
  165. expect(resp).to.not.include(`applied-ops:${this.doc_id}`)
  166. return done()
  167. })
  168. })
  169. return null
  170. })
  171. )
  172. })
  173. return describe('when the client disconnects before clientTracking.updatePosition starts', function () {
  174. beforeEach(function (done) {
  175. return async.series(
  176. [
  177. cb => {
  178. return FixturesManager.setUpProject(
  179. {
  180. privilegeLevel: 'owner',
  181. project: {
  182. name: 'Test Project',
  183. },
  184. },
  185. (e, { project_id: projectId, user_id: userId }) => {
  186. this.project_id = projectId
  187. this.user_id = userId
  188. return cb()
  189. }
  190. )
  191. },
  192. cb => {
  193. this.clientA = RealTimeClient.connect()
  194. return this.clientA.on('connectionAccepted', cb)
  195. },
  196. cb => {
  197. return this.clientA.emit(
  198. 'joinProject',
  199. { project_id: this.project_id },
  200. (error, project, privilegeLevel, protocolVersion) => {
  201. this.project = project
  202. this.privilegeLevel = privilegeLevel
  203. this.protocolVersion = protocolVersion
  204. return cb(error)
  205. }
  206. )
  207. },
  208. cb => {
  209. return FixturesManager.setUpDoc(
  210. this.project_id,
  211. { lines: this.lines, version: this.version, ops: this.ops },
  212. (e, { doc_id: docId }) => {
  213. this.doc_id = docId
  214. return cb(e)
  215. }
  216. )
  217. },
  218. cb => {
  219. return this.clientA.emit('joinDoc', this.doc_id, cb)
  220. },
  221. cb => {
  222. this.clientA.emit(
  223. 'clientTracking.updatePosition',
  224. {
  225. row: 42,
  226. column: 36,
  227. doc_id: this.doc_id,
  228. },
  229. () => {}
  230. )
  231. // disconnect before updateClientPosition completes
  232. this.clientA.on('disconnect', () => cb())
  233. return this.clientA.disconnect()
  234. },
  235. cb => {
  236. // wait for updateClientPosition
  237. return setTimeout(cb, 100)
  238. },
  239. ],
  240. done
  241. )
  242. })
  243. // we can not force the race condition, so we have to try many times
  244. return Array.from(Array.from({ length: 20 }).map((_, i) => i + 1)).map(
  245. attempt =>
  246. it(`should not show the client as connected (race ${attempt})`, function (done) {
  247. rclientRT.smembers(
  248. KeysRT.clientsInProject({ project_id: this.project_id }),
  249. (err, results) => {
  250. if (err) {
  251. return done(err)
  252. }
  253. expect(results).to.deep.equal([])
  254. return done()
  255. }
  256. )
  257. return null
  258. })
  259. )
  260. })
  261. })