tryLockTests.coffee 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. "./RedisKeyBuilder":
  11. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  12. "./RedisBackend":
  13. createClient : () =>
  14. auth:->
  15. set: @set = sinon.stub()
  16. "./Metrics": {inc: () ->}
  17. @callback = sinon.stub()
  18. @doc_id = "doc-id-123"
  19. describe "when the lock is not set", ->
  20. beforeEach ->
  21. @lockValue = "mock-lock-value"
  22. @LockManager.randomLock = sinon.stub().returns @lockValue
  23. @set.callsArgWith(5, null, "OK")
  24. @LockManager.tryLock @doc_id, @callback
  25. it "should set the lock key with an expiry if it is not set", ->
  26. @set.calledWith("Blocking:#{@doc_id}", @lockValue, "EX", 30, "NX")
  27. .should.equal true
  28. it "should return the callback with true and the lock value", ->
  29. @callback.calledWith(null, true, @lockValue).should.equal true
  30. describe "when the lock is already set", ->
  31. beforeEach ->
  32. @set.callsArgWith(5, null, null)
  33. @LockManager.tryLock @doc_id, @callback
  34. it "should return the callback with false", ->
  35. @callback.calledWith(null, false).should.equal true