ReleasingTheLock.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 'logger-sharelatex': {
  30. log() {},
  31. error() {}
  32. },
  33. 'redis-sharelatex': {
  34. createClient: () => this.client
  35. },
  36. 'settings-sharelatex': {
  37. redis: {
  38. lock: {
  39. key_schema: {
  40. blockingKey({ doc_id }) {
  41. return `Blocking:${doc_id}`
  42. }
  43. }
  44. }
  45. }
  46. },
  47. './Metrics': { inc() {} },
  48. './Profiler': (Profiler = (function () {
  49. Profiler = class Profiler {
  50. static initClass() {
  51. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  52. this.prototype.end = sinon.stub()
  53. }
  54. }
  55. Profiler.initClass()
  56. return Profiler
  57. })())
  58. }
  59. this.LockManager = SandboxedModule.require(modulePath, { requires: mocks })
  60. this.lockValue = 'lock-value-stub'
  61. return (this.callback = sinon.stub())
  62. })
  63. describe('when the lock is current', function () {
  64. beforeEach(function () {
  65. this.client.eval = sinon.stub().yields(null, 1)
  66. return this.LockManager.releaseLock(doc_id, this.lockValue, this.callback)
  67. })
  68. it('should clear the data from redis', function () {
  69. return this.client.eval
  70. .calledWith(
  71. this.LockManager.unlockScript,
  72. 1,
  73. `Blocking:${doc_id}`,
  74. this.lockValue
  75. )
  76. .should.equal(true)
  77. })
  78. return it('should call the callback', function () {
  79. return this.callback.called.should.equal(true)
  80. })
  81. })
  82. return describe('when the lock has expired', function () {
  83. beforeEach(function () {
  84. this.client.eval = sinon.stub().yields(null, 0)
  85. return this.LockManager.releaseLock(doc_id, this.lockValue, this.callback)
  86. })
  87. return it('should return an error if the lock has expired', function () {
  88. return this.callback
  89. .calledWith(sinon.match.instanceOf(Error))
  90. .should.equal(true)
  91. })
  92. })
  93. })