EarlyDisconnect.js 8.0 KB

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