2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
mocha/no-identical-title,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05: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-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { assert, expect } = require('chai')
|
2020-02-17 12:35:16 -05:00
|
|
|
const modulePath = '../../../../app/js/PackManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-09-10 07:58:06 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-17 12:35:16 -05:00
|
|
|
const _ = require('underscore')
|
|
|
|
|
|
|
|
const tk = require('timekeeper')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('PackManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
tk.freeze(new Date())
|
|
|
|
this.PackManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-09-10 09:23:04 -04:00
|
|
|
bson: require('bson'),
|
2020-09-10 07:58:06 -04:00
|
|
|
'./mongodb': { db: (this.db = {}), ObjectId },
|
2020-02-17 12:35:16 -05:00
|
|
|
'./LockManager': {},
|
|
|
|
'./MongoAWS': {},
|
2020-11-25 06:57:20 -05:00
|
|
|
'@overleaf/metrics': { inc() {} },
|
2020-02-17 12:35:16 -05:00
|
|
|
'./ProjectIterator': require('../../../../app/js/ProjectIterator.js'), // Cache for speed
|
|
|
|
'settings-sharelatex': {
|
|
|
|
redis: { lock: { key_schema: {} } }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.doc_id = ObjectId().toString()
|
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
return (this.PackManager.MAX_COUNT = 512)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
afterEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return tk.reset()
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('insertCompressedUpdates', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.lastUpdate = {
|
|
|
|
_id: '12345',
|
|
|
|
pack: [
|
|
|
|
{ op: 'op-1', meta: 'meta-1', v: 1 },
|
|
|
|
{ op: 'op-2', meta: 'meta-2', v: 2 }
|
|
|
|
],
|
|
|
|
n: 2,
|
|
|
|
sz: 100
|
|
|
|
}
|
|
|
|
this.newUpdates = [
|
|
|
|
{ op: 'op-3', meta: 'meta-3', v: 3 },
|
|
|
|
{ op: 'op-4', meta: 'meta-4', v: 4 }
|
|
|
|
]
|
|
|
|
return (this.db.docHistory = {
|
2020-09-10 07:58:06 -04:00
|
|
|
insertOne: sinon.stub().yields(),
|
2020-02-17 12:35:16 -05:00
|
|
|
insert: sinon.stub().callsArg(1),
|
2020-09-10 09:18:14 -04:00
|
|
|
updateOne: sinon.stub().yields(),
|
2020-02-17 12:35:16 -05:00
|
|
|
findAndModify: sinon.stub().callsArg(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with no last update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.PackManager.insertUpdatesIntoNewPack = sinon.stub().callsArg(4)
|
|
|
|
return this.PackManager.insertCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
null,
|
|
|
|
this.newUpdates,
|
|
|
|
true,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('for a small update', function () {
|
|
|
|
it('should insert the update into a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.newUpdates, true)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for many small updates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.newUpdates = __range__(0, 2048, true).map((i) => ({
|
2020-02-17 12:35:16 -05:00
|
|
|
op: `op-${i}`,
|
|
|
|
meta: `meta-${i}`,
|
|
|
|
v: i
|
|
|
|
}))
|
|
|
|
return this.PackManager.insertCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
null,
|
|
|
|
this.newUpdates,
|
|
|
|
false,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should append the initial updates to the existing pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(0, 512),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the first set remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(512, 1024),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the second set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(1024, 1536),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the third set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(1536, 2048),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the final set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(2048, 2049),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with an existing pack as the last update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.PackManager.appendUpdatesToExistingPack = sinon.stub().callsArg(5)
|
|
|
|
this.PackManager.insertUpdatesIntoNewPack = sinon.stub().callsArg(4)
|
|
|
|
return this.PackManager.insertCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
false,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('for a small update', function () {
|
|
|
|
it('should append the update to the existing pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.appendUpdatesToExistingPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should not insert any new packs', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack.called.should.equal(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('for many small updates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.newUpdates = __range__(0, 2048, true).map((i) => ({
|
2020-02-17 12:35:16 -05:00
|
|
|
op: `op-${i}`,
|
|
|
|
meta: `meta-${i}`,
|
|
|
|
v: i
|
|
|
|
}))
|
|
|
|
return this.PackManager.insertCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
false,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should append the initial updates to the existing pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.appendUpdatesToExistingPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates.slice(0, 510),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the first set remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(510, 1022),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the second set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(1022, 1534),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the third set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(1534, 2046),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the final set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(2046, 2049),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for many big updates', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const longString = __range__(
|
|
|
|
0,
|
|
|
|
0.75 * this.PackManager.MAX_SIZE,
|
|
|
|
true
|
|
|
|
)
|
2020-06-04 04:24:21 -04:00
|
|
|
.map((j) => 'a')
|
2020-02-17 12:35:16 -05:00
|
|
|
.join('')
|
2020-06-04 04:24:21 -04:00
|
|
|
this.newUpdates = [0, 1, 2, 3, 4].map((i) => ({
|
2020-02-17 12:35:16 -05:00
|
|
|
op: `op-${i}-${longString}`,
|
|
|
|
meta: `meta-${i}`,
|
|
|
|
v: i
|
|
|
|
}))
|
|
|
|
return this.PackManager.insertCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
false,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should append the initial updates to the existing pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.appendUpdatesToExistingPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates.slice(0, 1),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the first set remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(1, 2),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the second set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(2, 3),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the third set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(3, 4),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert the final set of remaining updates as a new pack', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.insertUpdatesIntoNewPack
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.newUpdates.slice(4, 5),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('flushCompressedUpdates', function () {
|
|
|
|
return describe('when there is no previous update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.PackManager.flushCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
null,
|
|
|
|
this.newUpdates,
|
|
|
|
true,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for a small update that will expire', function () {
|
|
|
|
it('should insert the update into mongo', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWithMatch({
|
|
|
|
pack: this.newUpdates,
|
|
|
|
project_id: ObjectId(this.project_id),
|
|
|
|
doc_id: ObjectId(this.doc_id),
|
|
|
|
n: this.newUpdates.length,
|
|
|
|
v: this.newUpdates[0].v,
|
|
|
|
v_end: this.newUpdates[this.newUpdates.length - 1].v
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should set an expiry time in the future', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWithMatch({
|
|
|
|
expiresAt: new Date(Date.now() + 7 * 24 * 3600 * 1000)
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when there is a recent previous update in mongo that expires', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.lastUpdate = {
|
|
|
|
_id: '12345',
|
|
|
|
pack: [
|
|
|
|
{ op: 'op-1', meta: 'meta-1', v: 1 },
|
|
|
|
{ op: 'op-2', meta: 'meta-2', v: 2 }
|
|
|
|
],
|
|
|
|
n: 2,
|
|
|
|
sz: 100,
|
|
|
|
meta: { start_ts: Date.now() - 6 * 3600 * 1000 },
|
|
|
|
expiresAt: new Date(Date.now())
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.PackManager.flushCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
true,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for a small update that will expire', function () {
|
|
|
|
it('should append the update in mongo', function () {
|
2020-09-10 09:18:14 -04:00
|
|
|
return this.db.docHistory.updateOne
|
|
|
|
.calledWithMatch(
|
|
|
|
{ _id: this.lastUpdate._id },
|
|
|
|
{
|
2020-02-17 12:35:16 -05:00
|
|
|
$push: { pack: { $each: this.newUpdates } },
|
|
|
|
$set: { v_end: this.newUpdates[this.newUpdates.length - 1].v }
|
|
|
|
}
|
2020-09-10 09:18:14 -04:00
|
|
|
)
|
2020-02-17 12:35:16 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should set an expiry time in the future', function () {
|
2020-09-10 09:18:14 -04:00
|
|
|
return this.db.docHistory.updateOne
|
|
|
|
.calledWithMatch(sinon.match.any, {
|
|
|
|
$set: { expiresAt: new Date(Date.now() + 7 * 24 * 3600 * 1000) }
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when there is a recent previous update in mongo that expires', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.PackManager.updateIndex = sinon.stub().callsArg(2)
|
|
|
|
|
|
|
|
this.lastUpdate = {
|
|
|
|
_id: '12345',
|
|
|
|
pack: [
|
|
|
|
{ op: 'op-1', meta: 'meta-1', v: 1 },
|
|
|
|
{ op: 'op-2', meta: 'meta-2', v: 2 }
|
|
|
|
],
|
|
|
|
n: 2,
|
|
|
|
sz: 100,
|
|
|
|
meta: { start_ts: Date.now() - 6 * 3600 * 1000 },
|
|
|
|
expiresAt: new Date(Date.now())
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.PackManager.flushCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
false,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for a small update that will not expire', function () {
|
|
|
|
it('should insert the update into mongo', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWithMatch({
|
|
|
|
pack: this.newUpdates,
|
|
|
|
project_id: ObjectId(this.project_id),
|
|
|
|
doc_id: ObjectId(this.doc_id),
|
|
|
|
n: this.newUpdates.length,
|
|
|
|
v: this.newUpdates[0].v,
|
|
|
|
v_end: this.newUpdates[this.newUpdates.length - 1].v
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should not set any expiry time', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.neverCalledWithMatch(sinon.match.has('expiresAt'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when there is an old previous update in mongo', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.lastUpdate = {
|
|
|
|
_id: '12345',
|
|
|
|
pack: [
|
|
|
|
{ op: 'op-1', meta: 'meta-1', v: 1 },
|
|
|
|
{ op: 'op-2', meta: 'meta-2', v: 2 }
|
|
|
|
],
|
|
|
|
n: 2,
|
|
|
|
sz: 100,
|
|
|
|
meta: { start_ts: Date.now() - 30 * 24 * 3600 * 1000 },
|
|
|
|
expiresAt: new Date(Date.now() - 30 * 24 * 3600 * 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.PackManager.flushCompressedUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.lastUpdate,
|
|
|
|
this.newUpdates,
|
|
|
|
true,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('for a small update that will expire', function () {
|
|
|
|
it('should insert the update into mongo', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWithMatch({
|
|
|
|
pack: this.newUpdates,
|
|
|
|
project_id: ObjectId(this.project_id),
|
|
|
|
doc_id: ObjectId(this.doc_id),
|
|
|
|
n: this.newUpdates.length,
|
|
|
|
v: this.newUpdates[0].v,
|
|
|
|
v_end: this.newUpdates[this.newUpdates.length - 1].v
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should set an expiry time in the future', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne
|
2020-02-17 12:35:16 -05:00
|
|
|
.calledWithMatch({
|
|
|
|
expiresAt: new Date(Date.now() + 7 * 24 * 3600 * 1000)
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getOpsByVersionRange', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('loadPacksByVersionRange', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('fetchPacksIfNeeded', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('makeProjectIterator', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getPackById', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('increaseTTL', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getIndex', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getPackFromIndex', function () {})
|
2020-02-17 12:35:16 -05:00
|
|
|
// getLastPackFromIndex:
|
|
|
|
// getIndexWithKeys
|
|
|
|
// initialiseIndex
|
|
|
|
// updateIndex
|
|
|
|
// findCompletedPacks
|
|
|
|
// findUnindexedPacks
|
|
|
|
// insertPacksIntoIndexWithLock
|
|
|
|
// _insertPacksIntoIndex
|
|
|
|
// archivePack
|
|
|
|
// checkArchivedPack
|
|
|
|
// processOldPack
|
|
|
|
// updateIndexIfNeeded
|
|
|
|
// findUnarchivedPacks
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('checkArchiveNotInProgress', function () {
|
|
|
|
describe('when an archive is in progress', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.db.docHistoryIndex = {
|
|
|
|
findOne: sinon.stub().callsArgWith(2, null, { inS3: false })
|
|
|
|
}
|
|
|
|
return this.PackManager.checkArchiveNotInProgress(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.pack_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.has('message'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when an archive is completed', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.db.docHistoryIndex = {
|
|
|
|
findOne: sinon.stub().callsArgWith(2, null, { inS3: true })
|
|
|
|
}
|
|
|
|
return this.PackManager.checkArchiveNotInProgress(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.pack_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.has('message'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the archive has not started or completed', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.db.docHistoryIndex = {
|
|
|
|
findOne: sinon.stub().callsArgWith(2, null, {})
|
|
|
|
}
|
|
|
|
return this.PackManager.checkArchiveNotInProgress(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.pack_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call the callback with no error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should return with no error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return (typeof this.callback.lastCall.args[0]).should.equal('undefined')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// describe "setTTLOnArchivedPack", ->
|
|
|
|
// beforeEach ->
|
|
|
|
// @pack_id = "somepackid"
|
|
|
|
// @onedayinms = 86400000
|
|
|
|
// @db.docHistory =
|
|
|
|
// findAndModify : sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
|
// it "should set expires to 1 day", (done)->
|
|
|
|
// #@PackManager._getOneDayInFutureWithRandomDelay = sinon.stub().returns(@onedayinms)
|
|
|
|
// @PackManager.setTTLOnArchivedPack @project_id, @doc_id, @pack_id, =>
|
|
|
|
// args = @db.docHistory.findAndModify.args[0][0]
|
|
|
|
// args.query._id.should.equal @pack_id
|
|
|
|
// args.update['$set'].expiresAt.should.equal @onedayinms
|
|
|
|
// done()
|
|
|
|
|
|
|
|
// describe "_getOneDayInFutureWithRandomDelay", ->
|
|
|
|
// beforeEach ->
|
|
|
|
// @onedayinms = 86400000
|
|
|
|
// @thirtyMins = 1000 * 60 * 30
|
|
|
|
|
|
|
|
// it "should give 1 day + 30 mins random time", (done)->
|
|
|
|
// loops = 10000
|
|
|
|
// while --loops > 0
|
|
|
|
// randomDelay = @PackManager._getOneDayInFutureWithRandomDelay() - new Date(Date.now() + @onedayinms)
|
|
|
|
// randomDelay.should.be.above(0)
|
|
|
|
// randomDelay.should.be.below(@thirtyMins + 1)
|
|
|
|
// done()
|
2020-02-17 12:34:50 -05:00
|
|
|
|
|
|
|
function __range__(left, right, inclusive) {
|
2020-02-17 12:35:16 -05:00
|
|
|
const range = []
|
|
|
|
const ascending = left < right
|
|
|
|
const end = !inclusive ? right : ascending ? right + 1 : right - 1
|
2020-02-17 12:34:50 -05:00
|
|
|
for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
|
2020-02-17 12:35:16 -05:00
|
|
|
range.push(i)
|
2020-02-17 12:34:50 -05:00
|
|
|
}
|
2020-02-17 12:35:16 -05:00
|
|
|
return range
|
|
|
|
}
|