tryLockTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. const tk = require('timekeeper')
  18. describe('LockManager - trying 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 {
  26. auth() {},
  27. set: (this.set = sinon.stub()),
  28. }
  29. },
  30. },
  31. './Metrics': { inc() {} },
  32. '@overleaf/settings': {
  33. redis: {
  34. lock: {
  35. key_schema: {
  36. blockingKey({ doc_id }) {
  37. return `Blocking:${doc_id}`
  38. },
  39. },
  40. },
  41. },
  42. },
  43. './Profiler':
  44. (this.Profiler = Profiler =
  45. (function () {
  46. Profiler = class Profiler {
  47. static initClass() {
  48. this.prototype.log = sinon
  49. .stub()
  50. .returns({ end: sinon.stub() })
  51. this.prototype.end = sinon.stub()
  52. }
  53. }
  54. Profiler.initClass()
  55. return Profiler
  56. })()),
  57. },
  58. })
  59. this.callback = sinon.stub()
  60. return (this.doc_id = 'doc-id-123')
  61. })
  62. describe('when the lock is not set', function () {
  63. beforeEach(function () {
  64. this.lockValue = 'mock-lock-value'
  65. this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
  66. this.set.callsArgWith(5, null, 'OK')
  67. return this.LockManager.tryLock(this.doc_id, this.callback)
  68. })
  69. it('should set the lock key with an expiry if it is not set', function () {
  70. return this.set
  71. .calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
  72. .should.equal(true)
  73. })
  74. return it('should return the callback with true and the lock value', function () {
  75. return this.callback
  76. .calledWith(null, true, this.lockValue)
  77. .should.equal(true)
  78. })
  79. })
  80. describe('when the lock is already set', function () {
  81. beforeEach(function () {
  82. this.set.callsArgWith(5, null, null)
  83. return this.LockManager.tryLock(this.doc_id, this.callback)
  84. })
  85. return it('should return the callback with false', function () {
  86. return this.callback.calledWith(null, false).should.equal(true)
  87. })
  88. })
  89. return describe('when it takes a long time for redis to set the lock', function () {
  90. beforeEach(function () {
  91. tk.freeze(Date.now())
  92. this.lockValue = 'mock-lock-value'
  93. this.LockManager.randomLock = sinon.stub().returns(this.lockValue)
  94. this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
  95. this.set.callsFake((_key, _v, _ex, _ttl, _nx, cb) => {
  96. tk.freeze(Date.now() + 7000)
  97. cb(null, 'OK')
  98. })
  99. })
  100. after(function () {
  101. tk.reset()
  102. })
  103. describe('in all cases', function () {
  104. beforeEach(function () {
  105. return this.LockManager.tryLock(this.doc_id, this.callback)
  106. })
  107. it('should set the lock key with an expiry if it is not set', function () {
  108. return this.set
  109. .calledWith(`Blocking:${this.doc_id}`, this.lockValue, 'EX', 30, 'NX')
  110. .should.equal(true)
  111. })
  112. return it('should try to release the lock', function () {
  113. return this.LockManager.releaseLock
  114. .calledWith(this.doc_id, this.lockValue)
  115. .should.equal(true)
  116. })
  117. })
  118. describe('if the lock is released successfully', function () {
  119. beforeEach(function () {
  120. this.LockManager.releaseLock = sinon.stub().callsArgWith(2, null)
  121. return this.LockManager.tryLock(this.doc_id, this.callback)
  122. })
  123. return it('should return the callback with false', function () {
  124. return this.callback.calledWith(null, false).should.equal(true)
  125. })
  126. })
  127. return describe('if the lock has already timed out', function () {
  128. beforeEach(function () {
  129. this.LockManager.releaseLock = sinon
  130. .stub()
  131. .callsArgWith(2, new Error('tried to release timed out lock'))
  132. return this.LockManager.tryLock(this.doc_id, this.callback)
  133. })
  134. return it('should return the callback with an error', function () {
  135. return this.callback
  136. .calledWith(sinon.match.instanceOf(Error))
  137. .should.equal(true)
  138. })
  139. })
  140. })
  141. })