| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- import { expect } from 'chai'
- import sinon from 'sinon'
- import { strict as esmock } from 'esmock'
- const MODULE_PATH = '../../../../app/js/RedisManager.js'
- describe('RedisManager', function () {
- beforeEach(async function () {
- this.rclient = {
- auth: sinon.stub(),
- exec: sinon.stub().resolves(),
- lrange: sinon.stub(),
- lrem: sinon.stub(),
- srem: sinon.stub(),
- del: sinon.stub(),
- }
- this.rclient.multi = sinon.stub().returns(this.rclient)
- this.RedisWrapper = {
- createClient: sinon.stub().returns(this.rclient),
- }
- this.Settings = {
- redis: {
- project_history: {
- key_schema: {
- projectHistoryOps({ project_id: projectId }) {
- return `Project:HistoryOps:{${projectId}}`
- },
- projectHistoryFirstOpTimestamp({ project_id: projectId }) {
- return `ProjectHistory:FirstOpTimestamp:{${projectId}}`
- },
- },
- },
- },
- }
- this.Metrics = {
- timing: sinon.stub(),
- summary: sinon.stub(),
- globalGauge: sinon.stub(),
- }
- this.RedisManager = await esmock(MODULE_PATH, {
- '@overleaf/redis-wrapper': this.RedisWrapper,
- '@overleaf/settings': this.Settings,
- '@overleaf/metrics': this.Metrics,
- })
- this.project_id = 'project-id-123'
- this.batchSize = 100
- this.historyOpsKey = `Project:HistoryOps:{${this.project_id}}`
- this.firstOpTimestampKey = `ProjectHistory:FirstOpTimestamp:{${this.project_id}}`
- this.updates = [
- { v: 42, op: ['a', 'b', 'c', 'd'] },
- { v: 45, op: ['e', 'f', 'g', 'h'] },
- ]
- this.extraUpdates = [{ v: 100, op: ['i', 'j', 'k'] }]
- this.rawUpdates = this.updates.map(update => JSON.stringify(update))
- this.extraRawUpdates = this.extraUpdates.map(update =>
- JSON.stringify(update)
- )
- })
- describe('getOldestDocUpdates', function () {
- beforeEach(async function () {
- this.rclient.lrange.resolves(this.rawUpdates)
- this.batchSize = 3
- this.result = await this.RedisManager.promises.getOldestDocUpdates(
- this.project_id,
- this.batchSize
- )
- })
- it('should read the updates from redis', function () {
- this.rclient.lrange
- .calledWith(this.historyOpsKey, 0, this.batchSize - 1)
- .should.equal(true)
- })
- it('should call the callback with the unparsed ops', function () {
- this.result.should.equal(this.rawUpdates)
- })
- })
- describe('parseDocUpdates', function () {
- it('should return the parsed ops', function () {
- this.RedisManager.parseDocUpdates(this.rawUpdates).should.deep.equal(
- this.updates
- )
- })
- })
- describe('getUpdatesInBatches', function () {
- beforeEach(function () {
- this.runner = sinon.stub().resolves()
- })
- describe('single batch smaller than batch size', function () {
- beforeEach(async function () {
- this.rclient.lrange.resolves(this.rawUpdates)
- this.batchSize = 3
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- this.batchSize,
- this.runner
- )
- })
- it('requests a single batch of updates', function () {
- this.rclient.lrange.should.have.been.calledOnce
- this.rclient.lrange.should.have.been.calledWith(
- this.historyOpsKey,
- 0,
- this.batchSize - 1
- )
- })
- it('calls the runner once', function () {
- this.runner.callCount.should.equal(1)
- })
- it('calls the runner with the updates', function () {
- this.runner.calledWith(this.updates).should.equal(true)
- })
- it('deletes the applied updates', function () {
- for (const update of this.rawUpdates) {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- update
- )
- }
- })
- it('deletes the first op timestamp', function () {
- expect(this.rclient.del).to.have.been.calledWith(
- this.firstOpTimestampKey
- )
- })
- })
- describe('single batch at batch size', function () {
- beforeEach(async function () {
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves([])
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner once', function () {
- this.runner.callCount.should.equal(1)
- })
- it('calls the runner with the updates', function () {
- this.runner.calledWith(this.updates).should.equal(true)
- })
- it('deletes the applied updates', function () {
- for (const update of this.rawUpdates) {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- update
- )
- }
- })
- it('deletes the first op timestamp', function () {
- expect(this.rclient.del).to.have.been.calledWith(
- this.firstOpTimestampKey
- )
- })
- })
- describe('single batch exceeding size limit on updates', function () {
- beforeEach(async function () {
- // set the threshold below the size of the first update
- this.RedisManager.setRawUpdateSizeThreshold(
- this.rawUpdates[0].length - 1
- )
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.rawUpdates.slice(1))
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the first update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(0, 1))
- })
- it('deletes the first update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[0]
- )
- })
- it('calls the runner with the second update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(1))
- })
- it('deletes the second set of applied updates', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[1]
- )
- })
- })
- describe('two batches with first update below and second update above the size limit on updates', function () {
- beforeEach(async function () {
- // set the threshold above the size of the first update, but below the total size
- this.RedisManager.setRawUpdateSizeThreshold(
- this.rawUpdates[0].length + 1
- )
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.rawUpdates.slice(1))
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the first update', function () {
- this.runner.calledWith(this.updates.slice(0, 1)).should.equal(true)
- })
- it('deletes the first set applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[0]
- )
- })
- it('calls the runner with the second update', function () {
- this.runner.calledWith(this.updates.slice(1)).should.equal(true)
- })
- it('deletes the second applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[1]
- )
- })
- })
- describe('single batch exceeding op count limit on updates', function () {
- beforeEach(async function () {
- // set the threshold below the size of the first update
- this.RedisManager.setMaxUpdateOpLength(this.updates[0].op.length - 1)
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.rawUpdates.slice(1))
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the first updates', function () {
- this.runner.calledWith(this.updates.slice(0, 1)).should.equal(true)
- })
- it('deletes the first applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[0]
- )
- })
- it('calls the runner with the second updates', function () {
- this.runner.calledWith(this.updates.slice(1)).should.equal(true)
- })
- it('deletes the second applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[1]
- )
- })
- })
- describe('single batch exceeding doc content count', function () {
- beforeEach(async function () {
- this.updates = [{ resyncDocContent: 123 }, { resyncDocContent: 456 }]
- this.rawUpdates = this.updates.map(update => JSON.stringify(update))
- // set the threshold below the size of the first update
- this.RedisManager.setMaxNewDocContentCount(this.updates.length - 1)
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.rawUpdates.slice(1))
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the first update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(0, 1))
- })
- it('deletes the first applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[0]
- )
- })
- it('calls the runner with the second update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(1))
- })
- it('deletes the second set of applied updates', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[1]
- )
- })
- })
- describe('two batches with first update below and second update above the ops length limit on updates', function () {
- beforeEach(async function () {
- // set the threshold below the size of the first update
- this.RedisManager.setMaxUpdateOpLength(this.updates[0].op.length + 1)
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.rawUpdates.slice(1))
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the first update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(0, 1))
- })
- it('deletes the first applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[0]
- )
- })
- it('calls the runner with the second update', function () {
- this.runner.should.have.been.calledWith(this.updates.slice(1))
- })
- it('deletes the second applied update', function () {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- this.rawUpdates[1]
- )
- })
- })
- describe('two batches', function () {
- beforeEach(async function () {
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).resolves(this.extraRawUpdates)
- await this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- })
- it('requests a second batch of updates', function () {
- this.rclient.lrange.should.have.been.calledTwice
- })
- it('calls the runner twice', function () {
- this.runner.callCount.should.equal(2)
- })
- it('calls the runner with the updates', function () {
- this.runner.should.have.been.calledWith(this.updates)
- this.runner.should.have.been.calledWith(this.extraUpdates)
- })
- it('deletes the first set of applied updates', function () {
- for (const update of this.rawUpdates) {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- update
- )
- }
- })
- it('deletes the second set of applied updates', function () {
- for (const update of this.extraRawUpdates) {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- update
- )
- }
- })
- })
- describe('error when first reading updates', function () {
- beforeEach(async function () {
- this.error = new Error('error')
- this.rclient.lrange.rejects(this.error)
- await expect(
- this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- ).to.be.rejected
- })
- it('does not delete any updates', function () {
- expect(this.rclient.lrem).not.to.have.been.called
- })
- })
- describe('error when reading updates for a second batch', function () {
- beforeEach(async function () {
- this.error = new Error('error')
- this.rclient.lrange.onCall(0).resolves(this.rawUpdates)
- this.rclient.lrange.onCall(1).rejects(this.error)
- await expect(
- this.RedisManager.promises.getUpdatesInBatches(
- this.project_id,
- 2,
- this.runner
- )
- ).to.be.rejected
- })
- it('deletes the first set of applied updates', function () {
- for (const update of this.rawUpdates) {
- expect(this.rclient.lrem).to.have.been.calledWith(
- this.historyOpsKey,
- 1,
- update
- )
- }
- })
- it('deletes applied updates only once', function () {
- expect(this.rclient.lrem.callCount).to.equal(this.rawUpdates.length)
- })
- })
- })
- })
|