getLockTests.js 4.0 KB

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