tryLockTests.js 4.7 KB

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