2020-02-19 06:15:25 -05: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-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2020-05-11 05:29:16 -04:00
|
|
|
const assert = require('chai').assert
|
2020-02-19 06:15:37 -05:00
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/ProjectPersistenceManager'
|
|
|
|
)
|
|
|
|
const tk = require('timekeeper')
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('ProjectPersistenceManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ProjectPersistenceManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./UrlCache': (this.UrlCache = {}),
|
|
|
|
'./CompileManager': (this.CompileManager = {}),
|
2020-05-11 05:29:16 -04:00
|
|
|
diskusage: (this.diskusage = { check: sinon.stub() }),
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2021-06-01 10:52:41 -04:00
|
|
|
project_cache_length_ms: 1000,
|
|
|
|
path: {
|
|
|
|
compilesDir: '/compiles',
|
|
|
|
outputDir: '/output',
|
2021-07-13 07:04:48 -04:00
|
|
|
clsiCacheDir: '/cache',
|
|
|
|
},
|
2020-05-11 05:29:16 -04:00
|
|
|
}),
|
2021-07-13 07:04:48 -04:00
|
|
|
'./db': (this.db = {}),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
return (this.user_id = '1234')
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('refreshExpiryTimeout', function () {
|
|
|
|
it('should leave expiry alone if plenty of disk', function (done) {
|
2021-06-01 10:52:41 -04:00
|
|
|
this.diskusage.check.resolves({
|
2020-05-11 05:29:16 -04:00
|
|
|
available: 40,
|
2021-07-13 07:04:48 -04:00
|
|
|
total: 100,
|
2020-05-11 05:29:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
|
|
|
|
this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
|
|
|
|
this.settings.project_cache_length_ms
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
|
2021-06-01 10:52:41 -04:00
|
|
|
this.diskusage.check.resolves({
|
2020-05-11 05:29:16 -04:00
|
|
|
available: 5,
|
2021-07-13 07:04:48 -04:00
|
|
|
total: 100,
|
2020-05-11 05:29:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
|
|
|
|
this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
|
2021-06-01 10:52:41 -04:00
|
|
|
this.diskusage.check.resolves({
|
2020-05-11 05:29:16 -04:00
|
|
|
available: 5,
|
2021-07-13 07:04:48 -04:00
|
|
|
total: 100,
|
2020-05-11 05:29:16 -04:00
|
|
|
})
|
|
|
|
this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
|
|
|
|
this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
|
|
|
|
this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
|
2021-06-01 10:52:41 -04:00
|
|
|
this.diskusage.check.throws(new Error())
|
2020-05-11 05:29:16 -04:00
|
|
|
this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
|
|
|
|
this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('clearExpiredProjects', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.project_ids = ['project-id-1', 'project-id-2']
|
|
|
|
this.ProjectPersistenceManager._findExpiredProjectIds = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, this.project_ids)
|
|
|
|
this.ProjectPersistenceManager.clearProjectFromCache = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(1)
|
|
|
|
this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
|
|
|
|
return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear each expired project', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
return Array.from(this.project_ids).map(project_id =>
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ProjectPersistenceManager.clearProjectFromCache
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('clearProject', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArg(1)
|
|
|
|
this.UrlCache.clearProject = sinon.stub().callsArg(1)
|
|
|
|
this.CompileManager.clearProject = sinon.stub().callsArg(2)
|
|
|
|
return this.ProjectPersistenceManager.clearProject(
|
|
|
|
this.project_id,
|
|
|
|
this.user_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear the project from the database', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ProjectPersistenceManager._clearProjectFromDatabase
|
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear all the cached Urls for the project', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache.clearProject
|
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should clear the project compile folder', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.CompileManager.clearProject
|
|
|
|
.calledWith(this.project_id, this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|