tryLockTests.coffee 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. path = require('path')
  5. modulePath = path.join __dirname, '../../../../../app/js/infrastructure/LockManager.js'
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'LockManager - trying the lock', ->
  8. beforeEach ->
  9. @LockManager = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": log:->
  11. "./RedisWrapper":
  12. client: () =>
  13. auth:->
  14. set: @set = sinon.stub()
  15. "settings-sharelatex":{redis:{}}
  16. "metrics-sharelatex": inc:->
  17. @callback = sinon.stub()
  18. @key = "lock:web:lockName:project-id}"
  19. @namespace = "lockName"
  20. describe "when the lock is not set", ->
  21. beforeEach ->
  22. @set.callsArgWith(5, null, "OK")
  23. @LockManager.randomLock = sinon.stub().returns("random-lock-value")
  24. @LockManager._tryLock @key, @namespace, @callback
  25. it "should set the lock key with an expiry if it is not set", ->
  26. @set.calledWith(@key, "random-lock-value", "EX", 30, "NX")
  27. .should.equal true
  28. it "should return the callback with true", ->
  29. @callback.calledWith(null, true).should.equal true
  30. describe "when the lock is already set", ->
  31. beforeEach ->
  32. @set.callsArgWith(5, null, null)
  33. @LockManager._tryLock @key, @namespace, @callback
  34. it "should return the callback with false", ->
  35. @callback.calledWith(null, false).should.equal true