2019-11-26 08:11:19 -05:00
|
|
|
const { expect } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const { Project } = require('../helpers/models/Project')
|
|
|
|
|
|
|
|
const MODULE_PATH =
|
|
|
|
'../../../../app/src/Features/ThirdPartyDataStore/TpdsProjectFlusher'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('TpdsProjectFlusher', function () {
|
|
|
|
beforeEach(function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.project = { _id: ObjectId() }
|
|
|
|
this.docs = {
|
|
|
|
'/doc/one': { _id: 'mock-doc-1', lines: ['one'], rev: 5 },
|
2021-04-27 03:52:58 -04:00
|
|
|
'/doc/two': { _id: 'mock-doc-2', lines: ['two'], rev: 6 },
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.files = {
|
|
|
|
'/file/one': { _id: 'mock-file-1', rev: 7 },
|
2021-04-27 03:52:58 -04:00
|
|
|
'/file/two': { _id: 'mock-file-2', rev: 8 },
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.DocumentUpdaterHandler = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
flushProjectToMongo: sinon.stub().resolves(),
|
|
|
|
},
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.ProjectGetter = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
getProject: sinon.stub().resolves(this.project),
|
|
|
|
},
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.ProjectEntityHandler = {
|
|
|
|
promises: {
|
2021-04-14 09:17:21 -04:00
|
|
|
getAllDocs: sinon.stub().withArgs(this.project._id).resolves(this.docs),
|
2019-11-26 08:11:19 -05:00
|
|
|
getAllFiles: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.project._id)
|
2021-04-27 03:52:58 -04:00
|
|
|
.resolves(this.files),
|
|
|
|
},
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.TpdsUpdateSender = {
|
|
|
|
promises: {
|
|
|
|
addDoc: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
addFile: sinon.stub().resolves(),
|
|
|
|
},
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
this.ProjectMock = sinon.mock(Project)
|
|
|
|
|
|
|
|
this.TpdsProjectFlusher = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
2022-01-10 05:23:05 -05:00
|
|
|
'../DocumentUpdater/DocumentUpdaterHandler':
|
|
|
|
this.DocumentUpdaterHandler,
|
2019-11-26 08:11:19 -05:00
|
|
|
'../Project/ProjectGetter': this.ProjectGetter,
|
|
|
|
'../Project/ProjectEntityHandler': this.ProjectEntityHandler,
|
|
|
|
'../../models/Project': { Project },
|
2021-04-27 03:52:58 -04:00
|
|
|
'./TpdsUpdateSender': this.TpdsUpdateSender,
|
|
|
|
},
|
2019-11-26 08:11:19 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.ProjectMock.restore()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('flushProjectToTpds', function () {
|
|
|
|
describe('usually', function () {
|
|
|
|
beforeEach(async function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
await this.TpdsProjectFlusher.promises.flushProjectToTpds(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should flush the project from the doc updater', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
expect(
|
|
|
|
this.DocumentUpdaterHandler.promises.flushProjectToMongo
|
|
|
|
).to.have.been.calledWith(this.project._id)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should flush each doc to the TPDS', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
for (const [path, doc] of Object.entries(this.docs)) {
|
|
|
|
expect(this.TpdsUpdateSender.promises.addDoc).to.have.been.calledWith(
|
|
|
|
{
|
|
|
|
project_id: this.project._id,
|
|
|
|
doc_id: doc._id,
|
|
|
|
project_name: this.project.name,
|
|
|
|
rev: doc.rev,
|
2021-04-27 03:52:58 -04:00
|
|
|
path,
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should flush each file to the TPDS', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
for (const [path, file] of Object.entries(this.files)) {
|
|
|
|
expect(
|
|
|
|
this.TpdsUpdateSender.promises.addFile
|
|
|
|
).to.have.been.calledWith({
|
|
|
|
project_id: this.project._id,
|
|
|
|
file_id: file._id,
|
|
|
|
project_name: this.project.name,
|
|
|
|
rev: file.rev,
|
2021-04-27 03:52:58 -04:00
|
|
|
path,
|
2019-11-26 08:11:19 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when a TPDS flush is pending', function () {
|
|
|
|
beforeEach(async function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.project.deferredTpdsFlushCounter = 2
|
|
|
|
this.ProjectMock.expects('updateOne')
|
|
|
|
.withArgs(
|
|
|
|
{
|
|
|
|
_id: this.project._id,
|
2021-04-27 03:52:58 -04:00
|
|
|
deferredTpdsFlushCounter: { $lte: 2 },
|
2019-11-26 08:11:19 -05:00
|
|
|
},
|
|
|
|
{ $set: { deferredTpdsFlushCounter: 0 } }
|
|
|
|
)
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
await this.TpdsProjectFlusher.promises.flushProjectToTpds(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('resets the deferred flush counter', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.ProjectMock.verify()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('deferProjectFlushToTpds', function () {
|
|
|
|
beforeEach(async function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.ProjectMock.expects('updateOne')
|
|
|
|
.withArgs(
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: this.project._id,
|
2019-11-26 08:11:19 -05:00
|
|
|
},
|
|
|
|
{ $inc: { deferredTpdsFlushCounter: 1 } }
|
|
|
|
)
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
await this.TpdsProjectFlusher.promises.deferProjectFlushToTpds(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('increments the deferred flush counter', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.ProjectMock.verify()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('flushProjectToTpdsIfNeeded', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
let cases = [0, undefined]
|
|
|
|
cases.forEach(counterValue => {
|
2021-04-14 09:17:21 -04:00
|
|
|
describe(`when the deferred flush counter is ${counterValue}`, function () {
|
|
|
|
beforeEach(async function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.project.deferredTpdsFlushCounter = counterValue
|
|
|
|
await this.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("doesn't flush the project from the doc updater", function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
expect(this.DocumentUpdaterHandler.promises.flushProjectToMongo).not
|
|
|
|
.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("doesn't flush any doc", function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
expect(this.TpdsUpdateSender.promises.addDoc).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("doesn't flush any file", function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
expect(this.TpdsUpdateSender.promises.addFile).not.to.have.been.called
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
cases = [1, 2]
|
|
|
|
cases.forEach(counterValue => {
|
2021-04-14 09:17:21 -04:00
|
|
|
describe(`when the deferred flush counter is ${counterValue}`, function () {
|
|
|
|
beforeEach(async function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.project.deferredTpdsFlushCounter = counterValue
|
|
|
|
this.ProjectMock.expects('updateOne')
|
|
|
|
.withArgs(
|
|
|
|
{
|
|
|
|
_id: this.project._id,
|
2021-04-27 03:52:58 -04:00
|
|
|
deferredTpdsFlushCounter: { $lte: counterValue },
|
2019-11-26 08:11:19 -05:00
|
|
|
},
|
|
|
|
{ $set: { deferredTpdsFlushCounter: 0 } }
|
|
|
|
)
|
|
|
|
.chain('exec')
|
|
|
|
.resolves()
|
|
|
|
await this.TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(
|
|
|
|
this.project._id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('flushes the project from the doc updater', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
expect(
|
|
|
|
this.DocumentUpdaterHandler.promises.flushProjectToMongo
|
|
|
|
).to.have.been.calledWith(this.project._id)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('flushes each doc to the TPDS', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
for (const [path, doc] of Object.entries(this.docs)) {
|
|
|
|
expect(
|
|
|
|
this.TpdsUpdateSender.promises.addDoc
|
|
|
|
).to.have.been.calledWith({
|
|
|
|
project_id: this.project._id,
|
|
|
|
doc_id: doc._id,
|
|
|
|
project_name: this.project.name,
|
|
|
|
rev: doc.rev,
|
2021-04-27 03:52:58 -04:00
|
|
|
path,
|
2019-11-26 08:11:19 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('flushes each file to the TPDS', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
for (const [path, file] of Object.entries(this.files)) {
|
|
|
|
expect(
|
|
|
|
this.TpdsUpdateSender.promises.addFile
|
|
|
|
).to.have.been.calledWith({
|
|
|
|
project_id: this.project._id,
|
|
|
|
file_id: file._id,
|
|
|
|
project_name: this.project.name,
|
|
|
|
rev: file.rev,
|
2021-04-27 03:52:58 -04:00
|
|
|
path,
|
2019-11-26 08:11:19 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('resets the deferred flush counter', function () {
|
2019-11-26 08:11:19 -05:00
|
|
|
this.ProjectMock.verify()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|