ProjectPersistenceManagerTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.fsPromises = {
  24. statfs: sinon.stub(),
  25. }
  26. this.ProjectPersistenceManager = SandboxedModule.require(modulePath, {
  27. requires: {
  28. '@overleaf/metrics': (this.Metrics = { gauge: sinon.stub() }),
  29. './UrlCache': (this.UrlCache = {}),
  30. './CompileManager': (this.CompileManager = {}),
  31. fs: { promises: this.fsPromises },
  32. '@overleaf/settings': (this.settings = {
  33. project_cache_length_ms: 1000,
  34. path: {
  35. compilesDir: '/compiles',
  36. outputDir: '/output',
  37. clsiCacheDir: '/cache',
  38. },
  39. }),
  40. },
  41. })
  42. this.callback = sinon.stub()
  43. this.project_id = 'project-id-123'
  44. return (this.user_id = '1234')
  45. })
  46. describe('refreshExpiryTimeout', function () {
  47. it('should leave expiry alone if plenty of disk', function (done) {
  48. this.fsPromises.statfs.resolves({
  49. blocks: 100,
  50. bsize: 1,
  51. bavail: 40,
  52. })
  53. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  54. this.Metrics.gauge.should.have.been.calledWith(
  55. 'disk_available_percent',
  56. 40
  57. )
  58. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
  59. this.settings.project_cache_length_ms
  60. )
  61. done()
  62. })
  63. })
  64. it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
  65. this.fsPromises.statfs.resolves({
  66. blocks: 100,
  67. bsize: 1,
  68. bavail: 5,
  69. })
  70. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  71. this.Metrics.gauge.should.have.been.calledWith(
  72. 'disk_available_percent',
  73. 5
  74. )
  75. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
  76. done()
  77. })
  78. })
  79. it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
  80. this.fsPromises.statfs.resolves({
  81. blocks: 100,
  82. bsize: 1,
  83. bavail: 5,
  84. })
  85. this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
  86. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  87. this.Metrics.gauge.should.have.been.calledWith(
  88. 'disk_available_percent',
  89. 5
  90. )
  91. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
  92. done()
  93. })
  94. })
  95. it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
  96. this.fsPromises.statfs.rejects(new Error())
  97. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  98. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
  99. done()
  100. })
  101. })
  102. })
  103. describe('clearExpiredProjects', function () {
  104. beforeEach(function () {
  105. this.project_ids = ['project-id-1', 'project-id-2']
  106. this.ProjectPersistenceManager._findExpiredProjectIds = sinon
  107. .stub()
  108. .callsArgWith(0, null, this.project_ids)
  109. this.ProjectPersistenceManager.clearProjectFromCache = sinon
  110. .stub()
  111. .callsArg(2)
  112. this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
  113. return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
  114. })
  115. it('should clear each expired project', function () {
  116. return Array.from(this.project_ids).map(projectId =>
  117. this.ProjectPersistenceManager.clearProjectFromCache
  118. .calledWith(projectId)
  119. .should.equal(true)
  120. )
  121. })
  122. return it('should call the callback', function () {
  123. return this.callback.called.should.equal(true)
  124. })
  125. })
  126. return describe('clearProject', function () {
  127. beforeEach(function () {
  128. this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
  129. .stub()
  130. .callsArg(1)
  131. this.UrlCache.clearProject = sinon.stub().callsArg(2)
  132. this.CompileManager.clearProject = sinon.stub().callsArg(2)
  133. return this.ProjectPersistenceManager.clearProject(
  134. this.project_id,
  135. this.user_id,
  136. this.callback
  137. )
  138. })
  139. it('should clear the project from the database', function () {
  140. return this.ProjectPersistenceManager._clearProjectFromDatabase
  141. .calledWith(this.project_id)
  142. .should.equal(true)
  143. })
  144. it('should clear all the cached Urls for the project', function () {
  145. return this.UrlCache.clearProject
  146. .calledWith(this.project_id)
  147. .should.equal(true)
  148. })
  149. it('should clear the project compile folder', function () {
  150. return this.CompileManager.clearProject
  151. .calledWith(this.project_id, this.user_id)
  152. .should.equal(true)
  153. })
  154. return it('should call the callback', function () {
  155. return this.callback.called.should.equal(true)
  156. })
  157. })
  158. })