tryLockTests.coffee 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/LockManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe 'LockManager - trying the lock', ->
  7. beforeEach ->
  8. @LockManager = SandboxedModule.require modulePath, requires:
  9. "logger-sharelatex": log:->
  10. "redis-sharelatex":
  11. createClient : () =>
  12. auth:->
  13. set: @set = sinon.stub()
  14. "./Metrics": {inc: () ->}
  15. @callback = sinon.stub()
  16. @doc_id = "doc-id-123"
  17. describe "when the lock is not set", ->
  18. beforeEach ->
  19. @lockValue = "mock-lock-value"
  20. @LockManager.randomLock = sinon.stub().returns @lockValue
  21. @set.callsArgWith(5, null, "OK")
  22. @LockManager.tryLock @doc_id, @callback
  23. it "should set the lock key with an expiry if it is not set", ->
  24. @set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
  25. .should.equal true
  26. it "should return the callback with true and the lock value", ->
  27. @callback.calledWith(null, true, @lockValue).should.equal true
  28. describe "when the lock is already set", ->
  29. beforeEach ->
  30. @set.callsArgWith(5, null, null)
  31. @LockManager.tryLock @doc_id, @callback
  32. it "should return the callback with false", ->
  33. @callback.calledWith(null, false).should.equal true