ProjectPersistenceManagerTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const assert = require('chai').assert
  16. const modulePath = require('node:path').join(
  17. __dirname,
  18. '../../../app/js/ProjectPersistenceManager'
  19. )
  20. const tk = require('timekeeper')
  21. describe('ProjectPersistenceManager', function () {
  22. beforeEach(function () {
  23. this.ProjectPersistenceManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. '@overleaf/metrics': (this.Metrics = { gauge: sinon.stub() }),
  26. './UrlCache': (this.UrlCache = {}),
  27. './CompileManager': (this.CompileManager = {}),
  28. diskusage: (this.diskusage = { check: sinon.stub() }),
  29. '@overleaf/settings': (this.settings = {
  30. project_cache_length_ms: 1000,
  31. path: {
  32. compilesDir: '/compiles',
  33. outputDir: '/output',
  34. clsiCacheDir: '/cache',
  35. },
  36. }),
  37. },
  38. })
  39. this.callback = sinon.stub()
  40. this.project_id = 'project-id-123'
  41. return (this.user_id = '1234')
  42. })
  43. describe('refreshExpiryTimeout', function () {
  44. it('should leave expiry alone if plenty of disk', function (done) {
  45. this.diskusage.check.resolves({
  46. available: 40,
  47. total: 100,
  48. })
  49. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  50. this.Metrics.gauge.should.have.been.calledWith(
  51. 'disk_available_percent',
  52. 40
  53. )
  54. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
  55. this.settings.project_cache_length_ms
  56. )
  57. done()
  58. })
  59. })
  60. it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
  61. this.diskusage.check.resolves({
  62. available: 5,
  63. total: 100,
  64. })
  65. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  66. this.Metrics.gauge.should.have.been.calledWith(
  67. 'disk_available_percent',
  68. 5
  69. )
  70. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
  71. done()
  72. })
  73. })
  74. it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
  75. this.diskusage.check.resolves({
  76. available: 5,
  77. total: 100,
  78. })
  79. this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
  80. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  81. this.Metrics.gauge.should.have.been.calledWith(
  82. 'disk_available_percent',
  83. 5
  84. )
  85. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
  86. done()
  87. })
  88. })
  89. it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
  90. this.diskusage.check.throws(new Error())
  91. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  92. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
  93. done()
  94. })
  95. })
  96. })
  97. describe('clearExpiredProjects', function () {
  98. beforeEach(function () {
  99. this.project_ids = ['project-id-1', 'project-id-2']
  100. this.ProjectPersistenceManager._findExpiredProjectIds = sinon
  101. .stub()
  102. .callsArgWith(0, null, this.project_ids)
  103. this.ProjectPersistenceManager.clearProjectFromCache = sinon
  104. .stub()
  105. .callsArg(2)
  106. this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
  107. return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
  108. })
  109. it('should clear each expired project', function () {
  110. return Array.from(this.project_ids).map(projectId =>
  111. this.ProjectPersistenceManager.clearProjectFromCache
  112. .calledWith(projectId)
  113. .should.equal(true)
  114. )
  115. })
  116. return it('should call the callback', function () {
  117. return this.callback.called.should.equal(true)
  118. })
  119. })
  120. return describe('clearProject', function () {
  121. beforeEach(function () {
  122. this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
  123. .stub()
  124. .callsArg(1)
  125. this.UrlCache.clearProject = sinon.stub().callsArg(2)
  126. this.CompileManager.clearProject = sinon.stub().callsArg(2)
  127. return this.ProjectPersistenceManager.clearProject(
  128. this.project_id,
  129. this.user_id,
  130. this.callback
  131. )
  132. })
  133. it('should clear the project from the database', function () {
  134. return this.ProjectPersistenceManager._clearProjectFromDatabase
  135. .calledWith(this.project_id)
  136. .should.equal(true)
  137. })
  138. it('should clear all the cached Urls for the project', function () {
  139. return this.UrlCache.clearProject
  140. .calledWith(this.project_id)
  141. .should.equal(true)
  142. })
  143. it('should clear the project compile folder', function () {
  144. return this.CompileManager.clearProject
  145. .calledWith(this.project_id, this.user_id)
  146. .should.equal(true)
  147. })
  148. return it('should call the callback', function () {
  149. return this.callback.called.should.equal(true)
  150. })
  151. })
  152. })