ReleasingTheLock.js 2.8 KB

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