| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- /* eslint-disable
- camelcase,
- no-return-assign,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const RealTimeClient = require('./helpers/RealTimeClient')
- const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
- const FixturesManager = require('./helpers/FixturesManager')
- const async = require('async')
- const settings = require('settings-sharelatex')
- const redis = require('@overleaf/redis-wrapper')
- const rclient = redis.createClient(settings.redis.pubsub)
- describe('PubSubRace', function () {
- before(function (done) {
- return MockDocUpdaterServer.run(done)
- })
- describe('when the client leaves a doc before joinDoc completes', function () {
- before(function (done) {
- return async.series(
- [
- (cb) => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project'
- }
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb()
- }
- )
- },
- (cb) => {
- this.clientA = RealTimeClient.connect()
- return this.clientA.on('connect', cb)
- },
- (cb) => {
- return this.clientA.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- (cb) => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id }) => {
- this.doc_id = doc_id
- return cb(e)
- }
- )
- },
- (cb) => {
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- // leave before joinDoc completes
- return this.clientA.emit('leaveDoc', this.doc_id, cb)
- },
- (cb) => {
- // wait for subscribe and unsubscribe
- return setTimeout(cb, 100)
- }
- ],
- done
- )
- })
- return it('should not subscribe to the applied-ops channels anymore', function (done) {
- rclient.pubsub('CHANNELS', (err, resp) => {
- if (err) {
- return done(err)
- }
- resp.should.not.include(`applied-ops:${this.doc_id}`)
- return done()
- })
- return null
- })
- })
- describe('when the client emits joinDoc and leaveDoc requests frequently and leaves eventually', function () {
- before(function (done) {
- return async.series(
- [
- (cb) => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project'
- }
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb()
- }
- )
- },
- (cb) => {
- this.clientA = RealTimeClient.connect()
- return this.clientA.on('connect', cb)
- },
- (cb) => {
- return this.clientA.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- (cb) => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id }) => {
- this.doc_id = doc_id
- return cb(e)
- }
- )
- },
- (cb) => {
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- return this.clientA.emit('leaveDoc', this.doc_id, cb)
- },
- (cb) => {
- // wait for subscribe and unsubscribe
- return setTimeout(cb, 100)
- }
- ],
- done
- )
- })
- return it('should not subscribe to the applied-ops channels anymore', function (done) {
- rclient.pubsub('CHANNELS', (err, resp) => {
- if (err) {
- return done(err)
- }
- resp.should.not.include(`applied-ops:${this.doc_id}`)
- return done()
- })
- return null
- })
- })
- describe('when the client emits joinDoc and leaveDoc requests frequently and remains in the doc', function () {
- before(function (done) {
- return async.series(
- [
- (cb) => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project'
- }
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb()
- }
- )
- },
- (cb) => {
- this.clientA = RealTimeClient.connect()
- return this.clientA.on('connect', cb)
- },
- (cb) => {
- return this.clientA.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- (cb) => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id }) => {
- this.doc_id = doc_id
- return cb(e)
- }
- )
- },
- (cb) => {
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- this.clientA.emit('joinDoc', this.doc_id, () => {})
- this.clientA.emit('leaveDoc', this.doc_id, () => {})
- return this.clientA.emit('joinDoc', this.doc_id, cb)
- },
- (cb) => {
- // wait for subscribe and unsubscribe
- return setTimeout(cb, 100)
- }
- ],
- done
- )
- })
- return it('should subscribe to the applied-ops channels', function (done) {
- rclient.pubsub('CHANNELS', (err, resp) => {
- if (err) {
- return done(err)
- }
- resp.should.include(`applied-ops:${this.doc_id}`)
- return done()
- })
- return null
- })
- })
- return describe('when the client disconnects before joinDoc completes', function () {
- before(function (done) {
- return async.series(
- [
- (cb) => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project'
- }
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb()
- }
- )
- },
- (cb) => {
- this.clientA = RealTimeClient.connect()
- return this.clientA.on('connect', cb)
- },
- (cb) => {
- return this.clientA.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- (cb) => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id }) => {
- this.doc_id = doc_id
- return cb(e)
- }
- )
- },
- (cb) => {
- let joinDocCompleted = false
- this.clientA.emit(
- 'joinDoc',
- this.doc_id,
- () => (joinDocCompleted = true)
- )
- // leave before joinDoc completes
- return setTimeout(
- () => {
- if (joinDocCompleted) {
- return cb(new Error('joinDocCompleted -- lower timeout'))
- }
- this.clientA.on('disconnect', () => cb())
- return this.clientA.disconnect()
- },
- // socket.io processes joinDoc and disconnect with different delays:
- // - joinDoc goes through two process.nextTick
- // - disconnect goes through one process.nextTick
- // We have to inject the disconnect event into a different event loop
- // cycle.
- 3
- )
- },
- (cb) => {
- // wait for subscribe and unsubscribe
- return setTimeout(cb, 100)
- }
- ],
- done
- )
- })
- it('should not subscribe to the editor-events channels anymore', function (done) {
- rclient.pubsub('CHANNELS', (err, resp) => {
- if (err) {
- return done(err)
- }
- resp.should.not.include(`editor-events:${this.project_id}`)
- return done()
- })
- return null
- })
- return it('should not subscribe to the applied-ops channels anymore', function (done) {
- rclient.pubsub('CHANNELS', (err, resp) => {
- if (err) {
- return done(err)
- }
- resp.should.not.include(`applied-ops:${this.doc_id}`)
- return done()
- })
- return null
- })
- })
- })
|