getLockTests.js 4.1 KB

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