CheckingTheLock.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* eslint-disable
  2. camelcase,
  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 project_id = 1234
  18. const doc_id = 5678
  19. const blockingKey = `Blocking:${doc_id}`
  20. const SandboxedModule = require('sandboxed-module')
  21. describe('LockManager - checking the lock', function () {
  22. let Profiler
  23. const existsStub = sinon.stub()
  24. const mocks = {
  25. '@overleaf/redis-wrapper': {
  26. createClient() {
  27. return {
  28. auth() {},
  29. exists: existsStub,
  30. }
  31. },
  32. },
  33. './Metrics': { inc() {} },
  34. './Profiler': (Profiler = (function () {
  35. Profiler = class Profiler {
  36. static initClass() {
  37. this.prototype.log = sinon.stub().returns({ end: sinon.stub() })
  38. this.prototype.end = sinon.stub()
  39. }
  40. }
  41. Profiler.initClass()
  42. return Profiler
  43. })()),
  44. }
  45. const LockManager = SandboxedModule.require(modulePath, { requires: mocks })
  46. it('should return true if the key does not exists', function (done) {
  47. existsStub.yields(null, '0')
  48. return LockManager.checkLock(doc_id, (err, free) => {
  49. if (err) return done(err)
  50. free.should.equal(true)
  51. return done()
  52. })
  53. })
  54. return it('should return false if the key does exists', function (done) {
  55. existsStub.yields(null, '1')
  56. return LockManager.checkLock(doc_id, (err, free) => {
  57. if (err) return done(err)
  58. free.should.equal(false)
  59. return done()
  60. })
  61. })
  62. })