ReleasingTheLock.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS206: Consider reworking classes to avoid initClass
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const assert = require('assert')
  15. const path = require('path')
  16. const modulePath = path.join(__dirname, '../../../../app/js/LockManager.js')
  17. const projectId = 1234
  18. const docId = 5678
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('LockManager - releasing the lock', function () {
  21. beforeEach(function () {
  22. let Profiler
  23. this.client = {
  24. auth() {},
  25. eval: sinon.stub(),
  26. }
  27. const mocks = {
  28. '@overleaf/redis-wrapper': {
  29. createClient: () => this.client,
  30. },
  31. '@overleaf/settings': {
  32. redis: {
  33. lock: {
  34. key_schema: {
  35. blockingKey({ doc_id: docId }) {
  36. return `Blocking:${docId}`
  37. },
  38. },
  39. },
  40. },
  41. },
  42. '@overleaf/metrics': { inc() {} },
  43. './Profiler': (Profiler = (function () {
  44. Profiler = class Profiler {
  45. static initClass() {
  46. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  47. this.prototype.end = sinon.stub()
  48. }
  49. }
  50. Profiler.initClass()
  51. return Profiler
  52. })()),
  53. }
  54. this.LockManager = SandboxedModule.require(modulePath, { requires: mocks })
  55. this.lockValue = 'lock-value-stub'
  56. return (this.callback = sinon.stub())
  57. })
  58. describe('when the lock is current', function () {
  59. beforeEach(function () {
  60. this.client.eval = sinon.stub().yields(null, 1)
  61. return this.LockManager.releaseLock(docId, this.lockValue, this.callback)
  62. })
  63. it('should clear the data from redis', function () {
  64. return this.client.eval
  65. .calledWith(
  66. this.LockManager.unlockScript,
  67. 1,
  68. `Blocking:${docId}`,
  69. this.lockValue
  70. )
  71. .should.equal(true)
  72. })
  73. return it('should call the callback', function () {
  74. return this.callback.called.should.equal(true)
  75. })
  76. })
  77. return describe('when the lock has expired', function () {
  78. beforeEach(function () {
  79. this.client.eval = sinon.stub().yields(null, 0)
  80. return this.LockManager.releaseLock(docId, this.lockValue, this.callback)
  81. })
  82. return it('should return an error if the lock has expired', function () {
  83. return this.callback
  84. .calledWith(sinon.match.instanceOf(Error))
  85. .should.equal(true)
  86. })
  87. })
  88. })