2020-05-06 06:11:22 -04: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 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* 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/ProjectManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('ProjectManager - flushProject', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Timer
|
|
|
|
this.ProjectManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./RedisManager': (this.RedisManager = {}),
|
|
|
|
'./ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
|
|
|
|
'./DocumentManager': (this.DocumentManager = {}),
|
|
|
|
'./HistoryManager': (this.HistoryManager = {}),
|
|
|
|
'./Metrics': (this.Metrics = {
|
|
|
|
Timer: (Timer = (function () {
|
|
|
|
Timer = class Timer {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.done = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Timer.initClass()
|
|
|
|
return Timer
|
2021-07-13 07:04:42 -04:00
|
|
|
})()),
|
|
|
|
}),
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
|
|
|
|
this.RedisManager.getDocIdsInProject = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.doc_ids)
|
|
|
|
this.DocumentManager.flushDocIfLoadedWithLock = sinon.stub().callsArg(2)
|
|
|
|
return this.ProjectManager.flushProjectWithLocks(
|
|
|
|
this.project_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback(error)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should get the doc ids in the project', function () {
|
|
|
|
return this.RedisManager.getDocIdsInProject
|
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should flush each doc in the project', function () {
|
2021-07-13 07:04:42 -04:00
|
|
|
return Array.from(this.doc_ids).map(doc_id =>
|
2020-05-06 06:11:36 -04:00
|
|
|
this.DocumentManager.flushDocIfLoadedWithLock
|
|
|
|
.calledWith(this.project_id, doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should call the callback without error', function () {
|
|
|
|
return this.callback.calledWith(null).should.equal(true)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return it('should time the execution', function () {
|
|
|
|
return this.Metrics.Timer.prototype.done.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return describe('when a doc errors', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
|
|
|
|
this.RedisManager.getDocIdsInProject = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.doc_ids)
|
|
|
|
this.DocumentManager.flushDocIfLoadedWithLock = sinon.spy(
|
|
|
|
(project_id, doc_id, callback) => {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
if (doc_id === 'doc-id-1') {
|
|
|
|
return callback(
|
|
|
|
(this.error = new Error('oops, something went wrong'))
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return this.ProjectManager.flushProjectWithLocks(
|
|
|
|
this.project_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback(error)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should still flush each doc in the project', function () {
|
2021-07-13 07:04:42 -04:00
|
|
|
return Array.from(this.doc_ids).map(doc_id =>
|
2020-05-06 06:11:36 -04:00
|
|
|
this.DocumentManager.flushDocIfLoadedWithLock
|
|
|
|
.calledWith(this.project_id, doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should record the error', function () {
|
|
|
|
return this.logger.error
|
|
|
|
.calledWith(
|
2020-05-14 16:58:57 -04:00
|
|
|
{ err: this.error, projectId: this.project_id, docId: 'doc-id-1' },
|
2020-05-06 06:11:36 -04:00
|
|
|
'error flushing doc'
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback with an error', function () {
|
2020-05-15 14:29:49 -04:00
|
|
|
return this.callback
|
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
|
|
|
.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return it('should time the execution', function () {
|
|
|
|
return this.Metrics.Timer.prototype.done.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|