getLockTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * DS206: Consider reworking classes to avoid initClass
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const modulePath = '../../../../app/js/LockManager.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('LockManager - getting the lock', function () {
  19. beforeEach(function () {
  20. let Profiler
  21. this.LockManager = SandboxedModule.require(modulePath, {
  22. requires: {
  23. '@overleaf/redis-wrapper': {
  24. createClient: () => {
  25. return { auth() {} }
  26. },
  27. },
  28. '@overleaf/metrics': { inc() {} },
  29. './Profiler': (Profiler = (function () {
  30. Profiler = class Profiler {
  31. static initClass() {
  32. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  33. this.prototype.end = sinon.stub()
  34. }
  35. }
  36. Profiler.initClass()
  37. return Profiler
  38. })()),
  39. },
  40. })
  41. this.callback = sinon.stub()
  42. return (this.doc_id = 'doc-id-123')
  43. })
  44. describe('when the lock is not set', function () {
  45. beforeEach(function (done) {
  46. this.lockValue = 'mock-lock-value'
  47. this.LockManager.tryLock = sinon
  48. .stub()
  49. .callsArgWith(1, null, true, this.lockValue)
  50. return this.LockManager.getLock(this.doc_id, (...args) => {
  51. this.callback(...Array.from(args || []))
  52. return done()
  53. })
  54. })
  55. it('should try to get the lock', function () {
  56. return this.LockManager.tryLock.calledWith(this.doc_id).should.equal(true)
  57. })
  58. it('should only need to try once', function () {
  59. return this.LockManager.tryLock.callCount.should.equal(1)
  60. })
  61. return it('should return the callback with the lock value', function () {
  62. return this.callback.calledWith(null, this.lockValue).should.equal(true)
  63. })
  64. })
  65. describe('when the lock is initially set', function () {
  66. beforeEach(function (done) {
  67. this.lockValue = 'mock-lock-value'
  68. const startTime = Date.now()
  69. let tries = 0
  70. this.LockManager.LOCK_TEST_INTERVAL = 5
  71. this.LockManager.tryLock = (docId, callback) => {
  72. if (callback == null) {
  73. callback = function () {}
  74. }
  75. if (Date.now() - startTime < 20 || tries < 2) {
  76. tries = tries + 1
  77. return callback(null, false)
  78. } else {
  79. return callback(null, true, this.lockValue)
  80. }
  81. }
  82. sinon.spy(this.LockManager, 'tryLock')
  83. return this.LockManager.getLock(this.doc_id, (...args) => {
  84. this.callback(...Array.from(args || []))
  85. return done()
  86. })
  87. })
  88. it('should call tryLock multiple times until free', function () {
  89. return (this.LockManager.tryLock.callCount > 1).should.equal(true)
  90. })
  91. return it('should return the callback with the lock value', function () {
  92. return this.callback.calledWith(null, this.lockValue).should.equal(true)
  93. })
  94. })
  95. return describe('when the lock times out', function () {
  96. beforeEach(function (done) {
  97. const time = Date.now()
  98. this.LockManager.MAX_LOCK_WAIT_TIME = 5
  99. this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false)
  100. return this.LockManager.getLock(this.doc_id, (...args) => {
  101. this.callback(...Array.from(args || []))
  102. return done()
  103. })
  104. })
  105. return it('should return the callback with an error', function () {
  106. return this.callback
  107. .calledWith(
  108. sinon.match
  109. .instanceOf(Error)
  110. .and(sinon.match.has('doc_id', this.doc_id))
  111. )
  112. .should.equal(true)
  113. })
  114. })
  115. })