tryLockTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS206: Consider reworking classes to avoid initClass
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const modulePath = '../../../../app/js/LockManager.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('LockManager - trying the lock', function () {
  18. beforeEach(function () {
  19. let Profiler
  20. this.LockManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. '@overleaf/redis-wrapper': {
  23. createClient: () => {
  24. return {
  25. auth() {},
  26. set: (this.set = sinon.stub()),
  27. }
  28. },
  29. },
  30. './Metrics': { inc() {} },
  31. '@overleaf/settings': {
  32. redis: {
  33. lock: {
  34. key_schema: {
  35. blockingKey({ doc_id }) {
  36. return `Blocking:${doc_id}`
  37. },
  38. },
  39. },
  40. },
  41. },
  42. './Profiler':
  43. (this.Profiler = Profiler =
  44. (function () {
  45. Profiler = class Profiler {
  46. static initClass() {
  47. this.prototype.log = sinon
  48. .stub()
  49. .returns({ end: sinon.stub() })
  50. this.prototype.end = sinon.stub()
  51. }
  52. }
  53. Profiler.initClass()
  54. return Profiler
  55. })()),
  56. },
  57. })
  58. this.callback = sinon.stub()
  59. return (this.doc_id = 'doc-id-123')
  60. })
  61. describe('when the lock is not set', function () {
  62. beforeEach(function () {
  63. this.lockValue = 'mock-lock-value'
  64. this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
  65. this.set.callsArgWith(5, null, 'OK')
  66. return this.LockManager.tryLock(this.doc_id, this.callback)
  67. })
  68. it('should set the lock key with an expiry if it is not set', function () {
  69. return this.set
  70. .calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
  71. .should.equal(true)
  72. })
  73. return it('should return the callback with true and the lock value', function () {
  74. return this.callback
  75. .calledWith(null, true, this.lockValue)
  76. .should.equal(true)
  77. })
  78. })
  79. describe('when the lock is already set', function () {
  80. beforeEach(function () {
  81. this.set.callsArgWith(5, null, null)
  82. return this.LockManager.tryLock(this.doc_id, this.callback)
  83. })
  84. return it('should return the callback with false', function () {
  85. return this.callback.calledWith(null, false).should.equal(true)
  86. })
  87. })
  88. return describe('when it takes a long time for redis to set the lock', function () {
  89. beforeEach(function () {
  90. this.Profiler.prototype.end = () => 7000 // take a long time
  91. this.Profiler.prototype.log = sinon
  92. .stub()
  93. .returns({ end: this.Profiler.prototype.end })
  94. this.lockValue = 'mock-lock-value'
  95. this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
  96. this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
  97. return this.set.callsArgWith(5, null, 'OK')
  98. })
  99. describe('in all cases', function () {
  100. beforeEach(function () {
  101. return this.LockManager.tryLock(this.doc_id, this.callback)
  102. })
  103. it('should set the lock key with an expiry if it is not set', function () {
  104. return this.set
  105. .calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
  106. .should.equal(true)
  107. })
  108. return it('should try to release the lock', function () {
  109. return this.LockManager.releaseLock
  110. .calledWith(this.doc_id, this.lockValue)
  111. .should.equal(true)
  112. })
  113. })
  114. describe('if the lock is released successfully', function () {
  115. beforeEach(function () {
  116. this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
  117. return this.LockManager.tryLock(this.doc_id, this.callback)
  118. })
  119. return it('should return the callback with false', function () {
  120. return this.callback.calledWith(null, false).should.equal(true)
  121. })
  122. })
  123. return describe('if the lock has already timed out', function () {
  124. beforeEach(function () {
  125. this.LockManager.releaseLock = sinon
  126. .stub()
  127. .callsArgWith(2, new Error('tried to release timed out lock'))
  128. return this.LockManager.tryLock(this.doc_id, this.callback)
  129. })
  130. return it('should return the callback with an error', function () {
  131. return this.callback
  132. .calledWith(sinon.match.instanceOf(Error))
  133. .should.equal(true)
  134. })
  135. })
  136. })
  137. })