ProjectPersistenceManagerTests.js 4.9 KB

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