2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
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:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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 async = require('async')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
|
|
|
|
const RealTimeClient = require('./helpers/RealTimeClient')
|
|
|
|
const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
|
|
|
|
const MockWebServer = require('./helpers/MockWebServer')
|
|
|
|
const FixturesManager = require('./helpers/FixturesManager')
|
|
|
|
|
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)
|
|
|
|
const rclientRT = redis.createClient(settings.redis.realtime)
|
|
|
|
const KeysRT = settings.redis.realtime.key_schema
|
|
|
|
|
|
|
|
describe('EarlyDisconnect', function () {
|
|
|
|
before(function (done) {
|
|
|
|
return MockDocUpdaterServer.run(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the client disconnects before joinProject completes', function () {
|
|
|
|
before(function () {
|
|
|
|
// slow down web-api requests to force the race condition
|
|
|
|
let joinProject
|
|
|
|
this.actualWebAPIjoinProject = joinProject = MockWebServer.joinProject
|
|
|
|
return (MockWebServer.joinProject = (project_id, user_id, cb) =>
|
|
|
|
setTimeout(() => joinProject(project_id, user_id, cb), 300))
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return (MockWebServer.joinProject = this.actualWebAPIjoinProject)
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(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
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientA = RealTimeClient.connect()
|
|
|
|
return this.clientA.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientA.emit(
|
|
|
|
'joinProject',
|
|
|
|
{ project_id: this.project_id },
|
|
|
|
() => {}
|
|
|
|
)
|
|
|
|
// disconnect before joinProject completes
|
|
|
|
this.clientA.on('disconnect', () => cb())
|
|
|
|
return this.clientA.disconnect()
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for joinDoc and subscribe
|
|
|
|
return setTimeout(cb, 500)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// we can force the race condition, there is no need to repeat too often
|
|
|
|
return Array.from(Array.from({ length: 5 }).map((_, i) => i + 1)).map(
|
2021-07-13 07:04:45 -04:00
|
|
|
attempt =>
|
2020-06-23 13:30:45 -04:00
|
|
|
it(`should not subscribe to the pub/sub channel anymore (race ${attempt})`, function (done) {
|
|
|
|
rclient.pubsub('CHANNELS', (err, resp) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(resp).to.not.include(`editor-events:${this.project_id}`)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the client disconnects before joinDoc completes', function () {
|
|
|
|
beforeEach(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
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientA = RealTimeClient.connect()
|
|
|
|
return this.clientA.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
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 },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
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, () => {})
|
|
|
|
// disconnect before joinDoc completes
|
|
|
|
this.clientA.on('disconnect', () => cb())
|
|
|
|
return this.clientA.disconnect()
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// we can not force the race condition, so we have to try many times
|
|
|
|
return Array.from(Array.from({ length: 20 }).map((_, i) => i + 1)).map(
|
2021-07-13 07:04:45 -04:00
|
|
|
attempt =>
|
2020-06-23 13:30:45 -04:00
|
|
|
it(`should not subscribe to the pub/sub channels anymore (race ${attempt})`, function (done) {
|
|
|
|
rclient.pubsub('CHANNELS', (err, resp) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(resp).to.not.include(`editor-events:${this.project_id}`)
|
|
|
|
|
|
|
|
return rclient.pubsub('CHANNELS', (err, resp) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(resp).to.not.include(`applied-ops:${this.doc_id}`)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when the client disconnects before clientTracking.updatePosition starts', function () {
|
|
|
|
beforeEach(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
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientA = RealTimeClient.connect()
|
|
|
|
return this.clientA.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
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 },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
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
|
|
|
this.clientA.emit(
|
|
|
|
'clientTracking.updatePosition',
|
|
|
|
{
|
|
|
|
row: 42,
|
|
|
|
column: 36,
|
2021-07-13 07:04:45 -04:00
|
|
|
doc_id: this.doc_id,
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
() => {}
|
|
|
|
)
|
|
|
|
// disconnect before updateClientPosition completes
|
|
|
|
this.clientA.on('disconnect', () => cb())
|
|
|
|
return this.clientA.disconnect()
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
// wait for updateClientPosition
|
|
|
|
return setTimeout(cb, 100)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// we can not force the race condition, so we have to try many times
|
|
|
|
return Array.from(Array.from({ length: 20 }).map((_, i) => i + 1)).map(
|
2021-07-13 07:04:45 -04:00
|
|
|
attempt =>
|
2020-06-23 13:30:45 -04:00
|
|
|
it(`should not show the client as connected (race ${attempt})`, function (done) {
|
|
|
|
rclientRT.smembers(
|
|
|
|
KeysRT.clientsInProject({ project_id: this.project_id }),
|
|
|
|
(err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
expect(results).to.deep.equal([])
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|