2020-05-06 06:11:22 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/UpdateManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe('UpdateManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Profiler, Timer
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.projectHistoryId = 'history-id-123'
|
|
|
|
this.doc_id = 'document-id-123'
|
|
|
|
this.callback = sinon.stub()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager = SandboxedModule.require(modulePath, {
|
2020-05-06 06:11:36 -04:00
|
|
|
requires: {
|
|
|
|
'./LockManager': (this.LockManager = {}),
|
|
|
|
'./RedisManager': (this.RedisManager = {}),
|
|
|
|
'./RealTimeRedisManager': (this.RealTimeRedisManager = {}),
|
|
|
|
'./ShareJsUpdateManager': (this.ShareJsUpdateManager = {}),
|
|
|
|
'./HistoryManager': (this.HistoryManager = {}),
|
|
|
|
'./Metrics': (this.Metrics = {
|
2022-04-07 05:02:19 -04:00
|
|
|
inc: sinon.stub(),
|
2020-05-06 06:11:36 -04:00
|
|
|
Timer: (Timer = (function () {
|
|
|
|
Timer = class Timer {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.done = sinon.stub()
|
|
|
|
}
|
2020-05-06 06:10:51 -04:00
|
|
|
}
|
2020-05-06 06:11:36 -04:00
|
|
|
Timer.initClass()
|
|
|
|
return Timer
|
2021-07-13 07:04:42 -04:00
|
|
|
})()),
|
2020-05-06 06:11:36 -04:00
|
|
|
}),
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {}),
|
2020-05-06 06:11:36 -04:00
|
|
|
'./DocumentManager': (this.DocumentManager = {}),
|
|
|
|
'./RangesManager': (this.RangesManager = {}),
|
|
|
|
'./SnapshotManager': (this.SnapshotManager = {}),
|
|
|
|
'./Profiler': (Profiler = (function () {
|
|
|
|
Profiler = class Profiler {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
|
|
|
|
this.prototype.end = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Profiler.initClass()
|
|
|
|
return Profiler
|
2021-07-13 07:04:42 -04:00
|
|
|
})()),
|
|
|
|
},
|
2023-06-06 06:19:55 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('processOutstandingUpdates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.fetchAndApplyUpdates = sinon.stub().callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdates(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should apply the updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.fetchAndApplyUpdates
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should time the execution', function () {
|
|
|
|
this.Metrics.Timer.prototype.done.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('processOutstandingUpdatesWithLock', function () {
|
|
|
|
describe('when the lock is free', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.LockManager.tryLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, true, (this.lockValue = 'mock-lock-value'))
|
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArg(2)
|
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should acquire the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.tryLock.calledWith(this.doc_id).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should free the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should process the outstanding updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdates
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should do everything with the lock acquired', function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdates
|
|
|
|
.calledAfter(this.LockManager.tryLock)
|
|
|
|
.should.equal(true)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdates
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledBefore(this.LockManager.releaseLock)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should continue processing new updates that may have come in', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when processOutstandingUpdates returns an error', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, (this.error = new Error('Something went wrong')))
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should free the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the error in the callback', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when the lock is taken', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
|
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the callback', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should not process the updates', function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdates.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('continueProcessingUpdatesWithLock', function () {
|
|
|
|
describe('when there are outstanding updates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RealTimeRedisManager.getUpdatesLength = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, 3)
|
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should process the outstanding updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when there are no outstanding updates', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.RealTimeRedisManager.getUpdatesLength = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, 0)
|
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not try to process the outstanding updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdatesWithLock.called.should.equal(
|
2020-05-06 06:11:36 -04:00
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fetchAndApplyUpdates', function () {
|
|
|
|
describe('with updates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.updates = [{ p: 1, t: 'foo' }]
|
|
|
|
this.updatedDocLines = ['updated', 'lines']
|
|
|
|
this.version = 34
|
|
|
|
this.RealTimeRedisManager.getPendingUpdatesForDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.updates)
|
|
|
|
this.UpdateManager.applyUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.updatedDocLines, this.version)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.fetchAndApplyUpdates(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the pending updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.RealTimeRedisManager.getPendingUpdatesForDoc
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should apply the updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
Array.from(this.updates).map(update =>
|
2020-05-06 06:11:36 -04:00
|
|
|
this.UpdateManager.applyUpdate
|
|
|
|
.calledWith(this.project_id, this.doc_id, update)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when there are no updates', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.updates = []
|
|
|
|
this.RealTimeRedisManager.getPendingUpdatesForDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.updates)
|
|
|
|
this.UpdateManager.applyUpdate = sinon.stub()
|
|
|
|
this.RedisManager.setDocument = sinon.stub()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.fetchAndApplyUpdates(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call applyUpdate', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.applyUpdate.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('applyUpdate', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.updateMeta = { user_id: 'last-author-fake-id' }
|
|
|
|
this.update = { op: [{ p: 42, i: 'foo' }], meta: this.updateMeta }
|
|
|
|
this.updatedDocLines = ['updated', 'lines']
|
|
|
|
this.version = 34
|
|
|
|
this.lines = ['original', 'lines']
|
|
|
|
this.ranges = { entries: 'mock', comments: 'mock' }
|
|
|
|
this.updated_ranges = { entries: 'updated', comments: 'updated' }
|
|
|
|
this.appliedOps = [
|
|
|
|
{ v: 42, op: 'mock-op-42' },
|
2021-07-13 07:04:42 -04:00
|
|
|
{ v: 45, op: 'mock-op-45' },
|
2020-05-06 06:11:36 -04:00
|
|
|
]
|
|
|
|
this.project_ops_length = sinon.stub()
|
|
|
|
this.pathname = '/a/b/c.tex'
|
|
|
|
this.DocumentManager.getDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(
|
|
|
|
null,
|
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId
|
|
|
|
)
|
|
|
|
this.RangesManager.applyUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.updated_ranges, false)
|
|
|
|
this.ShareJsUpdateManager.applyUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.updatedDocLines, this.version, this.appliedOps)
|
|
|
|
this.RedisManager.updateDocument = sinon
|
|
|
|
.stub()
|
2023-06-06 06:19:55 -04:00
|
|
|
.yields(null, this.project_ops_length)
|
2020-05-06 06:11:36 -04:00
|
|
|
this.RealTimeRedisManager.sendData = sinon.stub()
|
|
|
|
this.UpdateManager._addProjectHistoryMetadataToOps = sinon.stub()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.recordAndFlushHistoryOps = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('normally', function () {
|
|
|
|
beforeEach(function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should apply the updates via ShareJS', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.ShareJsUpdateManager.applyUpdate
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
this.lines,
|
|
|
|
this.version
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update the ranges', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.RangesManager.applyUpdate
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.appliedOps,
|
|
|
|
this.updatedDocLines
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should save the document', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.RedisManager.updateDocument
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.updatedDocLines,
|
|
|
|
this.version,
|
|
|
|
this.appliedOps,
|
|
|
|
this.updated_ranges,
|
|
|
|
this.updateMeta
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add metadata to the ops', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager._addProjectHistoryMetadataToOps
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.appliedOps,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.lines
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the applied ops into the history queue', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.recordAndFlushHistoryOps
|
|
|
|
.calledWith(this.project_id, this.appliedOps, this.project_ops_length)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with UTF-16 surrogate pairs in the update', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.update = { op: [{ p: 42, i: '\uD835\uDC00' }] }
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should apply the update but with surrogate pairs removed', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.ShareJsUpdateManager.applyUpdate
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.update)
|
|
|
|
.should.equal(true)
|
|
|
|
|
|
|
|
// \uFFFD is 'replacement character'
|
2023-06-06 06:19:55 -04:00
|
|
|
this.update.op[0].i.should.equal('\uFFFD\uFFFD')
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an error', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('something went wrong')
|
|
|
|
this.ShareJsUpdateManager.applyUpdate = sinon.stub().yields(this.error)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call RealTimeRedisManager.sendData with the error', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.RealTimeRedisManager.sendData
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith({
|
|
|
|
project_id: this.project_id,
|
|
|
|
doc_id: this.doc_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
error: this.error.message,
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when ranges get collapsed', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.RangesManager.applyUpdate = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.updated_ranges, true)
|
|
|
|
this.SnapshotManager.recordSnapshot = sinon.stub().yields()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
it('should increment the doc-snapshot metric', function () {
|
|
|
|
this.Metrics.inc.calledWith('doc-snapshot').should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call SnapshotManager.recordSnapshot', function () {
|
|
|
|
this.SnapshotManager.recordSnapshot
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.pathname,
|
|
|
|
this.lines,
|
|
|
|
this.ranges
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_addProjectHistoryMetadataToOps', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should add projectHistoryId, pathname and doc_length metadata to the ops', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
const lines = ['some', 'test', 'data']
|
|
|
|
const appliedOps = [
|
|
|
|
{
|
|
|
|
v: 42,
|
|
|
|
op: [
|
|
|
|
{ i: 'foo', p: 4 },
|
2021-07-13 07:04:42 -04:00
|
|
|
{ i: 'bar', p: 6 },
|
|
|
|
],
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
v: 45,
|
|
|
|
op: [
|
|
|
|
{ d: 'qux', p: 4 },
|
2021-07-13 07:04:42 -04:00
|
|
|
{ i: 'bazbaz', p: 14 },
|
|
|
|
],
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
{ v: 49, op: [{ i: 'penguin', p: 18 }] },
|
2020-05-06 06:11:36 -04:00
|
|
|
]
|
|
|
|
this.UpdateManager._addProjectHistoryMetadataToOps(
|
|
|
|
appliedOps,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
lines
|
|
|
|
)
|
2023-06-06 06:19:55 -04:00
|
|
|
appliedOps.should.deep.equal([
|
2020-05-06 06:11:36 -04:00
|
|
|
{
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
|
|
|
v: 42,
|
|
|
|
op: [
|
|
|
|
{ i: 'foo', p: 4 },
|
2021-07-13 07:04:42 -04:00
|
|
|
{ i: 'bar', p: 6 },
|
2020-05-06 06:11:36 -04:00
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
pathname: this.pathname,
|
2021-07-13 07:04:42 -04:00
|
|
|
doc_length: 14,
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
|
|
|
v: 45,
|
|
|
|
op: [
|
|
|
|
{ d: 'qux', p: 4 },
|
2021-07-13 07:04:42 -04:00
|
|
|
{ i: 'bazbaz', p: 14 },
|
2020-05-06 06:11:36 -04:00
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
pathname: this.pathname,
|
2021-07-13 07:04:42 -04:00
|
|
|
doc_length: 20,
|
|
|
|
}, // 14 + 'foo' + 'bar'
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
|
|
|
v: 49,
|
|
|
|
op: [{ i: 'penguin', p: 18 }],
|
|
|
|
meta: {
|
|
|
|
pathname: this.pathname,
|
2021-07-13 07:04:42 -04:00
|
|
|
doc_length: 23,
|
|
|
|
}, // 14 - 'qux' + 'bazbaz'
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('lockUpdatesAndDo', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.method = sinon.stub().callsArgWith(3, null, this.response_arg1)
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.arg1 = 'argument 1'
|
|
|
|
this.response_arg1 = 'response argument 1'
|
|
|
|
this.lockValue = 'mock-lock-value'
|
|
|
|
this.LockManager.getLock = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.lockValue)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock = sinon.stub().callsArg(2)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock = sinon.stub()
|
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.method,
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.arg1,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should lock the doc', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.getLock.calledWith(this.doc_id).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should process any outstanding updates', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.processOutstandingUpdates
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the method', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.method
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id, this.arg1)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the method response to the callback', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.callback.calledWith(null, this.response_arg1).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should release the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should continue processing updates', function () {
|
|
|
|
this.UpdateManager.continueProcessingUpdatesWithLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when processOutstandingUpdates returns an error', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, (this.error = new Error('Something went wrong')))
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.method,
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.arg1,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should free the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the error in the callback', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when the method returns an error', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.UpdateManager.processOutstandingUpdates = sinon.stub().callsArg(2)
|
|
|
|
this.method = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
3,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
this.response_arg1
|
|
|
|
)
|
2023-06-06 06:19:55 -04:00
|
|
|
this.UpdateManager.lockUpdatesAndDo(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.method,
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.arg1,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should free the lock', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.LockManager.releaseLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.doc_id, this.lockValue)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return the error in the callback', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|