ProjectPersistenceManagerTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. require('chai').should()
  17. const assert = require('chai').assert
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../app/js/ProjectPersistenceManager'
  21. )
  22. const tk = require('timekeeper')
  23. describe('ProjectPersistenceManager', function () {
  24. beforeEach(function () {
  25. this.ProjectPersistenceManager = SandboxedModule.require(modulePath, {
  26. requires: {
  27. './UrlCache': (this.UrlCache = {}),
  28. './CompileManager': (this.CompileManager = {}),
  29. diskusage: (this.diskusage = { check: sinon.stub() }),
  30. 'logger-sharelatex': (this.logger = {
  31. log: sinon.stub(),
  32. warn: sinon.stub(),
  33. err: sinon.stub()
  34. }),
  35. 'settings-sharelatex': (this.settings = {
  36. project_cache_length_ms: 1000
  37. }),
  38. './db': (this.db = {})
  39. }
  40. })
  41. this.callback = sinon.stub()
  42. this.project_id = 'project-id-123'
  43. return (this.user_id = '1234')
  44. })
  45. describe('refreshExpiryTimeout', function () {
  46. it('should leave expiry alone if plenty of disk', function (done) {
  47. this.diskusage.check.callsArgWith(1, null, {
  48. available: 40,
  49. total: 100
  50. })
  51. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  52. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(
  53. this.settings.project_cache_length_ms
  54. )
  55. done()
  56. })
  57. })
  58. it('should drop EXPIRY_TIMEOUT 10% if low disk usage', function (done) {
  59. this.diskusage.check.callsArgWith(1, null, {
  60. available: 5,
  61. total: 100
  62. })
  63. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  64. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(900)
  65. done()
  66. })
  67. })
  68. it('should not drop EXPIRY_TIMEOUT to below 50% of project_cache_length_ms', function (done) {
  69. this.diskusage.check.callsArgWith(1, null, {
  70. available: 5,
  71. total: 100
  72. })
  73. this.ProjectPersistenceManager.EXPIRY_TIMEOUT = 500
  74. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  75. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(500)
  76. done()
  77. })
  78. })
  79. it('should not modify EXPIRY_TIMEOUT if there is an error getting disk values', function (done) {
  80. this.diskusage.check.callsArgWith(1, 'Error', {
  81. available: 5,
  82. total: 100
  83. })
  84. this.ProjectPersistenceManager.refreshExpiryTimeout(() => {
  85. this.ProjectPersistenceManager.EXPIRY_TIMEOUT.should.equal(1000)
  86. done()
  87. })
  88. })
  89. })
  90. describe('clearExpiredProjects', function () {
  91. beforeEach(function () {
  92. this.project_ids = ['project-id-1', 'project-id-2']
  93. this.ProjectPersistenceManager._findExpiredProjectIds = sinon
  94. .stub()
  95. .callsArgWith(0, null, this.project_ids)
  96. this.ProjectPersistenceManager.clearProjectFromCache = sinon
  97. .stub()
  98. .callsArg(1)
  99. this.CompileManager.clearExpiredProjects = sinon.stub().callsArg(1)
  100. return this.ProjectPersistenceManager.clearExpiredProjects(this.callback)
  101. })
  102. it('should clear each expired project', function () {
  103. return Array.from(this.project_ids).map((project_id) =>
  104. this.ProjectPersistenceManager.clearProjectFromCache
  105. .calledWith(project_id)
  106. .should.equal(true)
  107. )
  108. })
  109. return it('should call the callback', function () {
  110. return this.callback.called.should.equal(true)
  111. })
  112. })
  113. return describe('clearProject', function () {
  114. beforeEach(function () {
  115. this.ProjectPersistenceManager._clearProjectFromDatabase = sinon
  116. .stub()
  117. .callsArg(1)
  118. this.UrlCache.clearProject = sinon.stub().callsArg(1)
  119. this.CompileManager.clearProject = sinon.stub().callsArg(2)
  120. return this.ProjectPersistenceManager.clearProject(
  121. this.project_id,
  122. this.user_id,
  123. this.callback
  124. )
  125. })
  126. it('should clear the project from the database', function () {
  127. return this.ProjectPersistenceManager._clearProjectFromDatabase
  128. .calledWith(this.project_id)
  129. .should.equal(true)
  130. })
  131. it('should clear all the cached Urls for the project', function () {
  132. return this.UrlCache.clearProject
  133. .calledWith(this.project_id)
  134. .should.equal(true)
  135. })
  136. it('should clear the project compile folder', function () {
  137. return this.CompileManager.clearProject
  138. .calledWith(this.project_id, this.user_id)
  139. .should.equal(true)
  140. })
  141. return it('should call the callback', function () {
  142. return this.callback.called.should.equal(true)
  143. })
  144. })
  145. })