2020-05-06 10:11:22 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 10:10:51 +00: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-05-06 10:11:36 +00:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/RealTimeRedisManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Errors = require('../../../../app/js/Errors')
|
|
|
|
|
|
|
|
describe('RealTimeRedisManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient = {
|
|
|
|
auth() {},
|
2021-07-13 11:04:42 +00:00
|
|
|
exec: sinon.stub(),
|
2020-05-06 10:11:36 +00:00
|
|
|
}
|
|
|
|
this.rclient.multi = () => this.rclient
|
|
|
|
this.pubsubClient = { publish: sinon.stub() }
|
|
|
|
this.RealTimeRedisManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-11-10 11:32:04 +00:00
|
|
|
'@overleaf/redis-wrapper': {
|
2021-07-13 11:04:42 +00:00
|
|
|
createClient: config =>
|
|
|
|
config.name === 'pubsub' ? this.pubsubClient : this.rclient,
|
2020-05-06 10:11:36 +00:00
|
|
|
},
|
2021-07-12 16:47:15 +00:00
|
|
|
'@overleaf/settings': {
|
2020-05-06 10:11:36 +00:00
|
|
|
redis: {
|
|
|
|
documentupdater: (this.settings = {
|
|
|
|
key_schema: {
|
|
|
|
pendingUpdates({ doc_id }) {
|
|
|
|
return `PendingUpdates:${doc_id}`
|
2021-07-13 11:04:42 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-06 10:11:36 +00:00
|
|
|
}),
|
|
|
|
pubsub: {
|
2021-07-13 11:04:42 +00:00
|
|
|
name: 'pubsub',
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 10:11:36 +00:00
|
|
|
},
|
|
|
|
crypto: (this.crypto = {
|
|
|
|
randomBytes: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(4)
|
2021-07-13 11:04:42 +00:00
|
|
|
.returns(Buffer.from([0x1, 0x2, 0x3, 0x4])),
|
2020-05-06 10:11:36 +00:00
|
|
|
}),
|
|
|
|
os: (this.os = { hostname: sinon.stub().returns('somehost') }),
|
2021-07-13 11:04:42 +00:00
|
|
|
'./Metrics': (this.metrics = { summary: sinon.stub() }),
|
|
|
|
},
|
2020-05-06 10:11:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
this.doc_id = 'doc-id-123'
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPendingUpdatesForDoc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.lrange = sinon.stub()
|
|
|
|
return (this.rclient.ltrim = sinon.stub())
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.updates = [
|
|
|
|
{ op: [{ i: 'foo', p: 4 }] },
|
2021-07-13 11:04:42 +00:00
|
|
|
{ op: [{ i: 'foo', p: 4 }] },
|
2020-05-06 10:11:36 +00:00
|
|
|
]
|
2021-07-13 11:04:42 +00:00
|
|
|
this.jsonUpdates = this.updates.map(update => JSON.stringify(update))
|
2020-05-06 10:11:36 +00:00
|
|
|
this.rclient.exec = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, [this.jsonUpdates])
|
|
|
|
return this.RealTimeRedisManager.getPendingUpdatesForDoc(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the pending updates', function () {
|
|
|
|
return this.rclient.lrange
|
|
|
|
.calledWith(`PendingUpdates:${this.doc_id}`, 0, 7)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should delete the pending updates', function () {
|
|
|
|
return this.rclient.ltrim
|
|
|
|
.calledWith(`PendingUpdates:${this.doc_id}`, 8, -1)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback with the updates', function () {
|
|
|
|
return this.callback.calledWith(null, this.updates).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe("when the JSON doesn't parse", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.jsonUpdates = [
|
|
|
|
JSON.stringify({ op: [{ i: 'foo', p: 4 }] }),
|
2021-07-13 11:04:42 +00:00
|
|
|
'broken json',
|
2020-05-06 10:11:36 +00:00
|
|
|
]
|
|
|
|
this.rclient.exec = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, [this.jsonUpdates])
|
|
|
|
return this.RealTimeRedisManager.getPendingUpdatesForDoc(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return an error to the callback', function () {
|
|
|
|
return this.callback
|
2020-05-15 18:29:49 +00:00
|
|
|
.calledWith(sinon.match.has('name', 'SyntaxError'))
|
2020-05-06 10:11:36 +00:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getUpdatesLength', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.llen = sinon.stub().yields(null, (this.length = 3))
|
|
|
|
return this.RealTimeRedisManager.getUpdatesLength(
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should look up the length', function () {
|
|
|
|
return this.rclient.llen
|
|
|
|
.calledWith(`PendingUpdates:${this.doc_id}`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the length', function () {
|
|
|
|
return this.callback.calledWith(null, this.length).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('sendData', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.message_id = 'doc:somehost:01020304-0'
|
|
|
|
return this.RealTimeRedisManager.sendData({ op: 'thisop' })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send the op with a message id', function () {
|
|
|
|
return this.pubsubClient.publish
|
|
|
|
.calledWith(
|
|
|
|
'applied-ops',
|
|
|
|
JSON.stringify({ op: 'thisop', _id: this.message_id })
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should track the payload size', function () {
|
|
|
|
return this.metrics.summary
|
|
|
|
.calledWith(
|
|
|
|
'redis.publish.applied-ops',
|
|
|
|
JSON.stringify({ op: 'thisop', _id: this.message_id }).length
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|