2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:30:45 -04:00
|
|
|
const RealTimeClient = require('./helpers/RealTimeClient')
|
|
|
|
const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
|
|
|
|
const FixturesManager = require('./helpers/FixturesManager')
|
|
|
|
|
|
|
|
const async = require('async')
|
|
|
|
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-11-10 06:32:06 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-06-23 13:30:45 -04:00
|
|
|
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(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
|
|
|
project: {
|
2021-07-13 07:04:45 -04:00
|
|
|
name: 'Test Project',
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { project_id: projectId, user_id: userId }) => {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.user_id = userId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientA = RealTimeClient.connect(
|
|
|
|
this.project_id,
|
2020-06-23 13:30:45 -04:00
|
|
|
(error, project, privilegeLevel, protocolVersion) => {
|
|
|
|
this.project = project
|
|
|
|
this.privilegeLevel = privilegeLevel
|
|
|
|
this.protocolVersion = protocolVersion
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docId }) => {
|
|
|
|
this.doc_id = docId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientA.emit('joinDoc', this.doc_id, () => {})
|
|
|
|
// leave before joinDoc completes
|
|
|
|
return this.clientA.emit('leaveDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for subscribe and unsubscribe
|
|
|
|
return setTimeout(cb, 100)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
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(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
|
|
|
project: {
|
2021-07-13 07:04:45 -04:00
|
|
|
name: 'Test Project',
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { project_id: projectId, user_id: userId }) => {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.user_id = userId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientA = RealTimeClient.connect(
|
|
|
|
this.project_id,
|
2020-06-23 13:30:45 -04:00
|
|
|
(error, project, privilegeLevel, protocolVersion) => {
|
|
|
|
this.project = project
|
|
|
|
this.privilegeLevel = privilegeLevel
|
|
|
|
this.protocolVersion = protocolVersion
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docId }) => {
|
|
|
|
this.doc_id = docId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for subscribe and unsubscribe
|
|
|
|
return setTimeout(cb, 100)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
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(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
|
|
|
project: {
|
2021-07-13 07:04:45 -04:00
|
|
|
name: 'Test Project',
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { project_id: projectId, user_id: userId }) => {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.user_id = userId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientA = RealTimeClient.connect(
|
|
|
|
this.project_id,
|
2020-06-23 13:30:45 -04:00
|
|
|
(error, project, privilegeLevel, protocolVersion) => {
|
|
|
|
this.project = project
|
|
|
|
this.privilegeLevel = privilegeLevel
|
|
|
|
this.protocolVersion = protocolVersion
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docId }) => {
|
|
|
|
this.doc_id = docId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for subscribe and unsubscribe
|
|
|
|
return setTimeout(cb, 100)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
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(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
|
|
|
project: {
|
2021-07-13 07:04:45 -04:00
|
|
|
name: 'Test Project',
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { project_id: projectId, user_id: userId }) => {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.user_id = userId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientA = RealTimeClient.connect(
|
|
|
|
this.project_id,
|
2020-06-23 13:30:45 -04:00
|
|
|
(error, project, privilegeLevel, protocolVersion) => {
|
|
|
|
this.project = project
|
|
|
|
this.privilegeLevel = privilegeLevel
|
|
|
|
this.protocolVersion = protocolVersion
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docId }) => {
|
|
|
|
this.doc_id = docId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for subscribe and unsubscribe
|
|
|
|
return setTimeout(cb, 100)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|