tryLockTests.js 4.8 KB

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