CheckingTheLock.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS206: Consider reworking classes to avoid initClass
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const assert = require('assert')
  14. const path = require('path')
  15. const modulePath = path.join(__dirname, '../../../../app/js/LockManager.js')
  16. const projectId = 1234
  17. const docId = 5678
  18. const blockingKey = `Blocking:${docId}`
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('LockManager - checking the lock', function () {
  21. let Profiler
  22. const existsStub = sinon.stub()
  23. const mocks = {
  24. '@overleaf/redis-wrapper': {
  25. createClient() {
  26. return {
  27. auth() {},
  28. exists: existsStub,
  29. }
  30. },
  31. },
  32. '@overleaf/metrics': { inc() {} },
  33. './Profiler': (Profiler = (function () {
  34. Profiler = class Profiler {
  35. static initClass() {
  36. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  37. this.prototype.end = sinon.stub()
  38. }
  39. }
  40. Profiler.initClass()
  41. return Profiler
  42. })()),
  43. }
  44. const LockManager = SandboxedModule.require(modulePath, { requires: mocks })
  45. it('should return true if the key does not exists', function (done) {
  46. existsStub.yields(null, '0')
  47. return LockManager.checkLock(docId, (err, free) => {
  48. if (err) return done(err)
  49. free.should.equal(true)
  50. return done()
  51. })
  52. })
  53. return it('should return false if the key does exists', function (done) {
  54. existsStub.yields(null, '1')
  55. return LockManager.checkLock(docId, (err, free) => {
  56. if (err) return done(err)
  57. free.should.equal(false)
  58. return done()
  59. })
  60. })
  61. })