getLockTests.js 4.0 KB

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