ReleasingTheLock.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 assert = require('assert')
  16. const path = require('path')
  17. const modulePath = path.join(__dirname, '../../../../app/js/LockManager.js')
  18. const project_id = 1234
  19. const doc_id = 5678
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('LockManager - releasing the lock', function () {
  22. beforeEach(function () {
  23. let Profiler
  24. this.client = {
  25. auth() {},
  26. eval: sinon.stub()
  27. }
  28. const mocks = {
  29. '@overleaf/redis-wrapper': {
  30. createClient: () => this.client
  31. },
  32. 'settings-sharelatex': {
  33. redis: {
  34. lock: {
  35. key_schema: {
  36. blockingKey({ doc_id }) {
  37. return `Blocking:${doc_id}`
  38. }
  39. }
  40. }
  41. }
  42. },
  43. './Metrics': { inc() {} },
  44. './Profiler': (Profiler = (function () {
  45. Profiler = class Profiler {
  46. static initClass() {
  47. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  48. this.prototype.end = sinon.stub()
  49. }
  50. }
  51. Profiler.initClass()
  52. return Profiler
  53. })())
  54. }
  55. this.LockManager = SandboxedModule.require(modulePath, { requires: mocks })
  56. this.lockValue = 'lock-value-stub'
  57. return (this.callback = sinon.stub())
  58. })
  59. describe('when the lock is current', function () {
  60. beforeEach(function () {
  61. this.client.eval = sinon.stub().yields(null, 1)
  62. return this.LockManager.releaseLock(doc_id, this.lockValue, this.callback)
  63. })
  64. it('should clear the data from redis', function () {
  65. return this.client.eval
  66. .calledWith(
  67. this.LockManager.unlockScript,
  68. 1,
  69. `Blocking:${doc_id}`,
  70. this.lockValue
  71. )
  72. .should.equal(true)
  73. })
  74. return it('should call the callback', function () {
  75. return this.callback.called.should.equal(true)
  76. })
  77. })
  78. return describe('when the lock has expired', function () {
  79. beforeEach(function () {
  80. this.client.eval = sinon.stub().yields(null, 0)
  81. return this.LockManager.releaseLock(doc_id, this.lockValue, this.callback)
  82. })
  83. return it('should return an error if the lock has expired', function () {
  84. return this.callback
  85. .calledWith(sinon.match.instanceOf(Error))
  86. .should.equal(true)
  87. })
  88. })
  89. })